Home > Java > javaTutorial > body text

What is the difference between SpringMVC forwarding and redirection?

零下一度
Release: 2017-06-28 10:13:37
Original
1829 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template