2015-09-06 80 views
0

我想實現URL編碼就像我的春天web應用程序http://www.host.abc/action?view=jobs,但不能把工作通過我的策略,這是做無法將URL重定向春季

@Controller 
public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 


    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale);  
     return "home"; 
    } 

    @RequestMapping(value = "/home/action?view=jobs", method = RequestMethod.GET) 
    public String showJobs(Model model) { 
     //some stuff goes here 
     return ("/home/action?view=jobs"); 
    }  
} 

回到Home.jsp是

<c:if test="${param.view == 'jobs' }"> 
    <!-- List of Jobs --> 
</c:if> 

這給我警告

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/jobsnetwork/home/action] in DispatcherServlet with name 'springDispatcher' 

,最後我說馬平到WebApplicationInitializer CLAS S作爲

public class AppInit implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext context) { 
     XmlWebApplicationContext rootContext = 
       new XmlWebApplicationContext(); 
     rootContext.setConfigLocation("/WEB-INF/spring/root-context.xml"); 

     context.addListener(new ContextLoaderListener(rootContext)); 

     // Create the dispatcher servlet's Spring application context 
     XmlWebApplicationContext servletContext = 
       new XmlWebApplicationContext(); 
     servletContext.setConfigLocation("/WEB-INF/spring/appServlet/servlet-context.xml"); 

     // add the dispatcher servlet and map it to/
     ServletRegistration.Dynamic dispatcher = 
       context.addServlet("springDispatcher", new DispatcherServlet(servletContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/"); 
     dispatcher.addMapping("/home/action");// added mapping here 

    } 

} 

上面的東西是不工作

回答

0

你的映射應包含路徑只("/jobsnetwork/home/action"),而不是請求參數("?view=jobs"):

@RequestMapping(value = "/jobsnetwork/home/action", method = RequestMethod.GET) 
public String showJobs(@RequestParam("view") String view, Model model) { 
    if (view.equals("jobs")) { 
     // do stuff if ?view=jobs 
    } else { 
     // do stuff if not ?view=jobs 
    } 
} 
+0

仍然相同的警告和HTTP狀態404 - –

+0

I edi特德。應該是@RequestMapping(value =「/ home/action」,',不是'@RequestMapping(value =「/ home/action?view = jobs」,'。 – acdcjunior

+0

Man!仍然是相同的警告和HTTP狀態404 –

-1

剛剛嘗試重定向: -

@Controller 
public class HomeController { 
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale);  
     return "home"; 
    } 

    @RequestMapping(value = "/action", method = RequestMethod.GET) 
    public String showJobs(@RequestParam("view") String view,Model model) { 
     //some stuff goes here 
     return "redirect:/action?view=jobs"; 
    }  
} 
+1

感謝您的回答,但這會導致服務器被反覆重定向到該網址而被吊死;-( –