Spring 3.0 forwarding request to different controller

后端 未结 3 429
广开言路
广开言路 2021-01-31 17:44

What is the proper way to forward a request in spring to a different controller?

@RequestMapping({\"/someurl\"})
public ModelAndView execute(Model model) {
    i         


        
相关标签:
3条回答
  • 2021-01-31 17:53

    You can use Spring RedirectView to dispatch request from one controller to other controller. It will be by default Request type "GET"

    RedirectView redirectView = new RedirectView("/controllerRequestMapping/methodmapping.do", true);
    
    0 讨论(0)
  • 2021-01-31 17:54

    You can use view name like "redirect:controllerName" or "forward:controllerName". The latter will reroute request to another controller and former will tell browser to redirect request to another url.

    docs: https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-redirecting-redirect-prefix

    0 讨论(0)
  • 2021-01-31 18:06

    Try returning a String instead, and the String being the forward url.

    @RequestMapping({"/someurl"})
    public String execute(Model model) {
        if (someCondition) {
            return "forward:/someUrlA";
        } else {
            return "forward:/someUrlB";
        }
    }
    
    0 讨论(0)
提交回复
热议问题