In servlet, forwarding and redirection are completed by request and response. Please see my previous article for the difference between the two. So how is it done in springMVC?
/**Forward**/
@RequestMapping("/login.do")
public String login(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("message", "hello");
return "forward:/index.do"; //forward can get the message value after the jump
}
index.do is another path, which is the value in RequestMapping.
@RequestMapping("/index.do")
public String index(HttpServletRequest request,HttpServletResponse response){
return "welcome";
}
/**Redirect**/
@RequestMapping("/logout.do")
public String logout(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("message", "hello");
return "redirect:/register.do"; //redirect cannot get the message value after the jump
}
register.do is another path, which is the value in RequestMapping.
@RequestMapping("/register.do")
public String register(HttpServletRequest request,HttpServletResponse response){
return "register";
}
In addition, the address bar URL will not change after the forward jump but will change with redirect
When we use redirection, all parameters passed to the page in the background are lost unless they are placed in the session. . So there is a solution in springMVC as follows:
@RequestMapping("/update.do")
public String update( RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute(" message", "Operation successful");
return "redirect:/admin/user";
}
In the redirected page, you can get the message value, this is because spring puts the message into the session.
The above is the detailed content of What is the difference between SpringMVC forwarding and redirection?. For more information, please follow other related articles on the PHP Chinese website!