【问题标题】:Unable to redirect from one controller to another controller-Spring MVC无法从一个控制器重定向到另一个控制器-Spring MVC
【发布时间】:2014-10-11 22:12:31
【问题描述】:

我是 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 错误。

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

【问题讨论】:

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


    【解决方案1】:

    需要一个简单的斜线/

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多