无法从一个控制器重定向到另一个控制器 - Spring MVC

时间:2014-10-11 12:44:27

标签: java spring-mvc model-view-controller

我是初级MVC并面临一些错误。
我有两个控制器如下 1)LoginController.java

@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但是在 浏览器网址我可以看到请求网址是 http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring。 在log之前添加Customer/Searchform.spring时,我收到404-The requested resource is not available错误。

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

1 个答案:

答案 0 :(得分:8)

需要一个简单的斜杠/

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

否则,路径将被视为相对于您当前正在处理的请求的路径。