2014-10-11 186 views
1

我是Spring MVC的新手,面临一些错误。
我有两个控制器如下
1)LoginController.java无法从一个控制器重定向到另一个控制器 - Spring MVC

@Controller 
@RequestMapping("/log") 
public class LoginController { 
    @Autowired 
    private LoginService service; 

    @RequestMapping(value="login.spring",method=RequestMethod.GET) 
    public ModelAndView prepareLoginForm() 
    { 
     System.out.println("In get"); 
     return new ModelAndView("Login", "login", new Login()); 
    } 

    @RequestMapping(value="login.spring",method=RequestMethod.POST) 
    public ModelAndView processLogin(@ModelAttribute("login") Login login,BindingResult result) 
    { 
     int i=service.validateLogin(login); 
     if(i==0){ 
      return new ModelAndView("redirect:login.spring"); 
     } 

     ModelAndView view=new ModelAndView("redirect:Customer/Searchform.spring"); 


     return view; 
    } 

} 

2)CustomerController.java

@Controller 
@RequestMapping("/Customer") 
public class CustomerController { 

    @Autowired 
    private CustomerService customerService; 


    @RequestMapping(value="Searchform.spring",method=RequestMethod.GET) 
    public ModelAndView prepareCustomer() 
    { 
     System.out.println("In customer controller"); 
     CustomerSearchForm customerSearchForm=new CustomerSearchForm(); 
     return new ModelAndView("CustomerSearch","customerSearchForm",customerSearchForm); 

    } 


    @RequestMapping(value="Search.spring",method=RequestMethod.POST) 
    public ModelAndView searchCustomer(@ModelAttribute("customer") CustomerSearchForm customerSearchForm,BindingResult result) 
    { 
     int i=customerService.serachCustomer(customerSearchForm); 
     if(i==1) 
     return new ModelAndView("Holdings"); 

     return new ModelAndView("redirect:Customer"); 
    } 
} 

所以后成功登录我试图重定向到CustomerController但 浏览器的URL我可以看到,请求网址是 http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring。 由于log之前添加Customer/Searchform.spring我得到404-The requested resource is not available错误。

将请求url作为http://localhost:8080/Online_Fund_Trading/Customer/Searchform.spring需要进行哪些更改。

回答

8

一个简单的斜线/需要

ModelAndView view=new ModelAndView("redirect:/Customer/Searchform.spring"); 

否则路径将相对于当前正在处理请求的路径加以考虑。

+0

非常感谢您的回答。它的工作原理:-) – 2014-10-11 14:16:12

+0

谢谢你....它的工作.... – 2015-05-03 04:34:00

相关问题