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

发布于 2021-02-02 11:21:25

我是Spring 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但在浏览器URL中,我可以看到该请求URL为
http://localhost:8080/Online_Fund_Trading/log/Customer/Searchform.spring。如logCustomer/Searchform.spring我得到404-The requested resource is not available错误之前被添加。

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

关注者
0
被浏览
102
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    一个简单的斜杠/是必需的

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

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看