Spring MVC to do form form function when submitting, to prevent the user client back or refresh the repeated submission of the problem, you need to redirect the server-side jump, which redirect is a direct jump to other pages, there are the following three methods for redirection.
redirect redirect process
The client sends a request to the server, the server matches the servlet, which are the same as the request forwarding, servlet processing after the call sendRedirect () this method, this method is the response method, so when the servlet processing is complete, see () method, immediately return to the client this response, response line tell the client that you must send another request to access, immediately after the client by the request, immediately send a new request to request, here the two requests do not interfere with each other, independent of each other, in front of the request inside the setAttribute () of anything, in the back of the request can not be obtained. Can be seen in the sendRedirect () inside the two requests, two responses.
1. Redirection jumps
@RequestMapping(value="/testredirect",method = { , }) public ModelAndView testredirect(HttpServletResponse response){ ("/index"); return null; }
2. ViewResolver direct jumps
without parameters
@RequestMapping(value="/testredirect",method = { , }) public String testredirect(HttpServletResponse response){ return "redirect:/index"; }
parametric
@RequestMapping("/testredirect") public String testredirect(Model model, RedirectAttributes attr) { ("test", "51gjie");//Jump address with test parameter ("u2", "51gjie");//Jump address without the u2 parameter return "redirect:/user/users"; }
- Use RedirectAttributes addAttribute method to pass parameters will follow the URL, such as the above code that is http:/?test=51gjie
- Use addFlashAttribute will not follow the URL, the value of the parameter will be temporarily saved in the session, to be redirected to the url to get the parameter removed from the session, here the redirect must be the method mapping path, jsp is invalid. You will find after redirect jsp page b will only appear once, after refresh b will never appear, which verifies the above, b will be removed from the session after access. For repeat submissions you can use this to accomplish.
- spring mvc set under RequestMappingHandlerAdapter ignoreDefaultModelOnRedirect=true, so that you can improve efficiency and avoid unnecessary retrieval.
3. ModelAndView redirection
without parameters
@RequestMapping(value="/restredirect",method = { , }) public ModelAndView restredirect(String userName){ ModelAndView model = new ModelAndView("redirect:/main/index"); return model; }
parametric
@RequestMapping(value="/toredirect",method = { , }) public ModelAndView toredirect(String userName){ ModelAndView model = new ModelAndView("/main/index"); ("userName", userName); // bring the userName parameter into the controller's RedirectAttributes return model; }
4. Jumping directly to a web page
import ; HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; ("/#/admins")
summarize
1, redirect redirection can jump to any server, can be used in the system between the jump.
2, Spring MVC redirect redirection, parameter passing can be directly spliced url can also use RedirectAttributes to deal with, as a result of a different request, redirection passed parameters will be displayed in the address bar, so the delivery of Chinese encoding should be processed.
to this article on SpringMVC redirect redirect (with parameters) of the three ways the article is introduced to this, more related SpringMVC redirect redirect content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!