SpringMVC in SessionAttributes comment

SessionAttributes Note: By default Spring MVC data storage model to request domain. When the end of a request, the data on the failure. If you want to use across the page. We need to use the session. And @SessionAttributes annotation can make data storage model to a session in the domain.

  1. The annotations can only be used in class
  2. The annotation for temporary Map, the data ModelAndView, ModelMap Model HttpSession to or into it can be shared between a plurality of data requests (not act on the HttpServletRequest)

ModelAndView

Below, we found that the addition of this comment seems superfluous, because the handler method returns a value type is ModelAndView, which contains the view information, but also includes data information, but in fact the data mv in here is stored in the request, add the SessionAttributes comment after the property named ui data copy of
a domain into a session

Here Insert Picture Description

Here Insert Picture Description

Model

When Modelhandler method comprising Model parameter list, the method handler can simply return to the view, when a user makes a request, the server executes the method handler, Spring MVC automatically fill data in the Model to the request. Example UserInfoController the class code is equivalent to the following code:

Data copy above SessionAttributes notes also the model of the property named ui placed in the session domain

Here Insert Picture Description

ModelMap&Map

When the parameter list handler method comprising org.springframework.ui.ModelMap or java.uti.Map, handler method simply returns to the view, Spring MVC ModelMap or automatically add data to the model of the Map.

Method ModelMap or put addAttribute class object can output data to the display control layer layer, and invoked through the expression EL
Here Insert Picture Description
Here Insert Picture Description

result

Target page as follows
Here Insert Picture Description

Annotated class corresponding display page:
Here Insert Picture Description

No annotated classes corresponding display page:
Here Insert Picture Description

Published 101 original articles · won praise 3 · Views 2232

Guess you like

Origin blog.csdn.net/S_Tian/article/details/104505735