SpringMvc jump problem
SpringMvc's Controller returns a logical view and model every time it processes data.
So we will see that the native Controller returns a ModelAndView (with view and model inside).
Under normal circumstances (unless the method annotated by @ModelAttribute), ModelAndView will eventually be returned.
Of course, sometimes a function processing method does not necessarily need to return a logical view, but can also be redirected to another function method
Forwarding internally to a logical view or another functional method.
---
The power of SpringMvc is that it encapsulates a large amount of underlying code for Servlets, but does it completely block the user from the Servlet API
Use of Therefore, page jumps in SpringMvc are also divided into two categories:
Use ServletAPI to implement page jump
Redirect method: Since the essence of redirection is to require the browser to resend a request, the pages in SpringMvc are usually placed under WEB-INF, and the browser cannot directly access them)
So the redirection here is essentially redirecting to another functional method.
Request forwarding within the server: Request forwarding is an internal behavior of the server, so you can directly jump to access a jsp page or jump to another function processing method.
```text
//Use native ServletApi for page redirection @RequestMapping("/c")public String test(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {System.out.println("testC");//Skip to a page inside the server//request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request,response); //Skip to a function processing method inside the server//request.getRequestDispatcher("/dispather/b").forward(request,response); //Redirect a function method response.sendRedirect(request.getContextPath()+"/dispather/b"); return null; }Use SpringMvc's API to implement page jump
Return the logical view name directly
text @RequestMapping("/b") public String testB(){ System.out.println("testB"); //Return directly to a view return "index"; }Returns a custom ModelAndView: When customizing ModelAndView, you can redirect or request forwarding.
```text
//Controller uses ModelAndView to jump and redirect @RequestMapping("/e")public ModelAndView testE(){System.out.println("testE");//Skip to a page inside the server //return "index"; //Skip to a function processing method inside the server //return new ModelAndView("forward:/dispather/b"); //Redirect a function method return new ModelAndView("redirect:/dispather/b");}Summarize
The above is all about this article discussing the page jump problem in Springmvc. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!
refer to:
Java programming implementation of springMVC simple login example
SpringMVC interceptor implements single sign-on
Springmvc Rest style introduction and implementation code example