SPRINGMVC: Learning Notes (4)--working with model data

Source: Internet
Author: User
Tags class definition

springmvc-Processing Model Data Description

The SPRINGMVC provides several ways to output model data:
Modelandview: The method body can add model data through the object when the processing method returns a value of type Modelandview
Map and Model: when the processing method returns when the entry is Org.springframework.ui.Model, Org.springframework.ui.ModelMap, or JAVA.UTI.MAP, The data in the map is automatically added to the model.
@SessionAttributes: A property in the model is staged in httpsession so that the property can be shared between multiple requests
@ModelAttribute: When the annotation is annotated with a method in the parameter, the object of the entry is placed in the data model.

Modelandview description

Once the controller has finished processing the customer request, the Modelandview object is returned to the Dispatcherservlet front-end controller. The model and view are included in the Modelandview. From a macro perspective, Dispatcherservlet is the controller for the entire Web application, and from a micro point of view, the controller is a director in a single HTTP request process, while Modelandview is the model and view returned during the HTTP request.
That is, if the return value of the controller processing method is Modelandview, it contains both the view information and the model data information .

Example

1. Create Modelandview and pass in model data

  

  2. Write a JSP page to test

  

 Description

SPRINGMVC will take the data method from the model in Modelandview to the request domain object.

Map and model Description:

Spring MVC internally uses a Org.springframework.ui.Model interface to store model data.

  Spring MVC creates an implied model object as a storage container for model data before invoking the method.

If the method's incoming parameter is a MAP or model type, Spring MVC passes a reference to the implied model to those arguments .   in the body of the method, the developer can access all the data in the model through this entry object, or they can add new attribute data to the model.

Example:

  

Use model:

The map passed in here is actually bindingawaremodelmap, so we define the parameters with a map.

Description

And here we can also use the model type.
  

@SessionAttribute

  if you want to share a model property data across multiple requests, you can label a @SessionAttributes on the controller class, and Spring MVC will temporarily save the corresponding properties in the model to HttpSession.

 Description

Note that this annotation can only be placed on the top of the class

1. First use map to store the model data in the request domain and then use @sessionattributes at the class definition to copy to the session.

  

  2. Writing JSP pages and testing

  

  

Add

@SessionAttributes you can specify which model properties need to be placed in the session by the object type of the model property, except that you can specify the properties that need to be placed in the session through the property name
– @SessionAttributes (Types=user.class) adds all attributes of type User.class in the implied model to the session.
– @SessionAttributes (value={"user1", "User2"})
– @SessionAttributes (Types={user.class, Dept.class})
– @SessionAttributes (value={"user1", "User2"},types={dept.class})

@ModelAttribute Description:

The Modelattribute has two usage scenarios in the controller. When used as a method parameter, @ModelAttribute a method parameter that maps a model property to a specific annotation (see the Processsubmit () method below). This is how the controller obtains the object reference that holds the form data. In addition, this parameter can be declared as a specific type of form support object, rather than a generic java.lang.Object, which adds type safety.
@ModelAttribute is also used to provide reference data to the model at the method level (see the Populatepettypes () method below). In this usage, method writing can contain the same type as the @requestmapping annotation described above.
Note: The method of using the @modelattribute annotation will be executed before the selected method using the @requestmapping annotation. They effectively pre-populate the implicit model with specific attributes, which often come from a database. Such a property can also be accessed by using the handle method parameter of the @modelattribute annotation in the selected method, potentially allowing binding and validation to be applied.

Note: The method that is annotated by @modelattribute is executed before each method of the controller is executed, so use it sparingly for the use of a controller to map multiple URLs.

Basic usage:

  1. Using @modelattribute on the method

@Controller  Public class userdao{. @ModelAttribute   public user addUser (user user)    {    returnNew User (201702, "Mrsaber"); } ...

Description

This adduser method is executed before executing the method with @requestmapping in Userdao, and two objects are added to the model during the execution of the AddUser, and the object with the key "user" is first (the user of the AddUser method User, and then add an object with the key "User1" (caused by the annotation @modelattribute ("user1").

  2. Use @modelattribute on the method parameters.

@RequestMapping ("/save")  public  String Save (@ModelAttribute user user) {      User.setusername ("U Love Me");      Userservice.save (user);       return "Result";  

Description

This method first obtains the object with the key "user" from the model, and if the acquisition does not instantiate a user object by reflection, then takes the value set from the request to the object, and then adds the user object to the model (where key is "user"). Using the @modelattribute can modify this key, not necessarily "user", in this case, with the use of @modelattribute no difference.

  

SPRINGMVC: Learning Notes (4)--working with model data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.