There are four ways to pass values in a scope

  • Pass values through native HttpServletRequest
  • Pass values through ModelAndView
  • Transfer values through Map
  • Pass values through Model

Note: Both Map and Model will eventually be converted to ModelAndView objects

Pass values through native HttpServletRequest

img

Pass values through ModelAndView

img

Transfer values through Map

img

Pass values through Model

img

Two ways to jump to a page

img

Forward Two modes of forwarding a request (with parameters)

In Spring MVC, forward request forwarding only needs to request the server once, which can improve the access speed. At the same time, parameter transfer can be handled by HttpServletRequest. Since it is the same request, the request object is still the same, and there will be no garbled characters in Chinese. Use forward to send a request instead of a page.

The ViewResolver requests forwarding

img
img

ModelAndView requests forwarding

img
img

Forward Request forwarding process: The client first sends a request to the server, which finds the matching servlet and directs it to execute it. When the servlet is finished executing, it calls getRequestDispacther() to forward the request to the specified test.jsp. The whole process is done on the server side. The servlet and JSP share the same request, and everything put in the servlet can be retrieved in the JSP, so the JSP can get the result getAttribute() out. The whole process is a request, a response.

Redirect Three redirection modes (with parameters)

When submitting the form function in Spring MVC, you need to perform redirection on the server side to prevent the user client from submitting the form function repeatedly when it is backward or refreshed. Redirect means directly redirecting to other pages. There are three methods for redirection.

Redirect Redirection process

The client sends a request to the server, and the server matches the servlet, which is the same as request forwarding. After the servlet has processed the request, it calls sendRedirect(), which is the response method, so when the servlet has processed the request, Seeing the Response.senredirect () method, immediately return the response to the client. The response line tells the client that you must send another request to test.jsp. Immediately after receiving the request, the client sends a new request to test.jsp. Independent of each other, anything setAttribute() in the previous request is not available in the subsequent request. As you can see, inside sendRedirect() there are two requests and two responses.

Response. sendRedirect Redirects this redirect

img

The ViewResolver jumps directly

img

In the case of parameters, the parameters are concatenated at the end of the URL

img
img

ModelAndView redirect

img
img
  1. Redirect Redirect can be used to redirect data to any server and between systems.
  2. In Spring MVC, parameter transfer can be directly joined url or handled by RedirectAttributes. Since it is a different request, the parameters transferred by redirection will be displayed in the address bar, so Chinese encoding should be processed during transfer.

ModelAndView source debugging

ModelAndView class

ModelAndView contains a Model property and a View property. Model is actually a ModelMap type, which is a subclass of LinkedHashMap, and View contains some view information.

The ModelMap object is mainly used to transfer control method processing data to the result page, that is to say, we can put the data needed on the result page into the ModelMap object, which is similar to the function of the setAttribute method of the Request object (data is valid in a request forwarding). Used to pass processed data during a request.

img
The getModelInternal(), getModelMap(), and getModel() methods are all used to retrieve model data.

Copy the code

ModelAndView processes model data

Write test method, return statement interrupt point, start DeBug mode

img

Enter the doDispatch method in the DispatcherServlet

Call the request processing method in the request processor, and return the ModelAndView object after execution, which is received by MV

img

Go back to the method requested in the Controller, execute the return statement, and return the ModelAndView object

img

Go back to the doDispatch method in the DispatcherServlet and get ready to process the ModelAndView

img

Go to the ProcessDispatchResult method in the DispatcherServlet

Prepare to process view information and model data in ModelAndView

img

Go to the Render method in the DispatcherServlet

First, according to the View information in ModelAndView, a View object is obtained through the View parser

img

The view object then begins to process the model data

img

Enter the Render method in AbstractView, ready to integrate the output model data

img

The Render method is defined in the View interface and implemented in AbstractView. All concrete View classes use the Render method implemented in AbstractView.

Go to the renderMergedOutputModel method in InternalResourceView and prepare to set the model data to the Request scope

img

Enter into the AbstractView exposeModelAsRequestAttributes method, the model data is added to the request scope

img

Enter renderMergedOutputModel method in InternalResourceView, obtain forwarder and implement forwarding

img