What does Spring MVC controller do with the 301 jump?
This article mainly explains "Spring MVC controller how to handle 301 jump", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Spring MVC controller how to handle 301 jump"!
SpringMVC301, 302 Jump
The spring mvc project is a subproject of spring that handles view-level requests.
302 jump is very simple:
@RequestMapping(value = "blog.html")public String rindex() { return "redirect:/blog";}
According to the concept of not repeating the wheel, in fact, 301 jump is also very simple:
@RequestMapping(value = "blog.html")public RedirectView rindex(HttpServletRequest request) { RedirectView redirectView = new RedirectView("/blog"); redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); return redirectView; }
Spring mvc provides a view view specifically designed to handle various redirects.
The jump results are as follows:
Other implementations include:
response.setStatus(301);
response.setHeader("Location", "https://www.xxxx");
Second, Redorect jump to pass parameters
Redorect 3 ways to pass reference
The first one: manual URL splicing
new ModelAndView("redirect:/toList?param1="+value1+"¶m2="+value2);
This has a drawback, that is, the transmission of Chinese may have garbled code problems, need to deal with their own.
Category 2: RedirectAttributes
Here it uses its addAttribute method, this actually redirects you to look at the url after the past, it automatically spells your url for you.
How to use:
public String test(RedirectAttributes attributes)
{
attributes.addAttribute("test", "hello");
return "redirect:/test/test2";
}
ModelAndView with attributes in model
ModelAndView model = new ModelAndView("redirect:/main/index");
model.addObject("userName", userName); //bring userName parameter into controller's RedirectAttributes
return model;
At this point, I believe that everyone has a deeper understanding of "Spring MVC controller how to handle 301 jumps", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!