Spring 3.2 mvc, how to rewrite url within controller as part of redirectview with permanent status code sent

后端 未结 1 663
萌比男神i
萌比男神i 2021-01-22 08:03
 @RequestMapping(value = \"/Foo/{id}/{friendlyUrl:.*}\", method = RequestMethod.GET)
 public ModelAndView getFoo(@PathVariable final Long id, @PathVariable final String          


        
相关标签:
1条回答
  • 2021-01-22 08:34

    In your code you have

    foo = fooService.get(id); //use id from pathvariable
    redirectView = new RedirectView(foo.getCorrectUrl()); //set url to correct url, not that in path
    redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); //moved permanently
    modelAndView = new ModelAndView(redirectView);
    modelAndView.setViewName("myFoo.jsp");
    return modelAndView;
    

    The call to modelAndView.setViewName("myFoo.jsp"); effectively replaces the value of View (redirectView reference) that was passed to ModelAndView contructor. So you should not call setViewName in this case.

    0 讨论(0)
提交回复
热议问题