Response Data Outgoing (SPRINGMVC)

Source: Internet
Author: User

1. SPRINGMVC Output Model Data overview

Provides the following 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: entry for Org.springframework.ui.Model,

Org.springframework.ui.ModelMap or JAVA.UTI.MAP, when the processing method returns, 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

2. Modelandview of processing model data

The return value of the ① controller processing method, if it is Modelandview, contains both view information and model data information.

② Add model data:

Moelandview AddObject (String attributename, Object AttributeValue)

Modelandview Addallobject (map<string,?> modelmap)

③ Setting the View:

void Setview (view view)

void Setviewname (String viewName)

/** * The return type of the target method can be Modelandview                 type * */ @RequestMapping ("/testmodelandview")  Public Modelandview Testmodelandview () {System.out.println ("Testmodelandview"= "Success"   New  Modelandview (viewName); Mv.addobject ("Time",new//   return  MV;}
Time: ${requestscope.time}
3. Map for processing model data

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

Specific Use steps

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

2) If the method's entry parameter is a MAP or model type , Spring MVC passes a reference to the implied model to those arguments.

3) in the body of the method, the developer can access all the data in the model through this entry object, or you can add new attribute data to the model

// The return type of the target method can also be a map type parameter (or model, or Modelmap type)@RequestMapping ("/testmap")public/ /"Focus"System.out.println (Map.getclass (). GetName ()); // org.springframework.validation.support.BindingAwareModelMapmap.put ("Names", Arrays.aslist ("Tom", "Jerry "," Kite ")); return "Success";}

<!--test Map as Process return results--

<a href= "Springmvc/testmap" >testMap</a>

① Add Success page, show results

Names: ${requestscope.names}

1. sessionattributes annotations for the processing of model data

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

@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

For example:

① @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})

Response Data Outgoing (SPRINGMVC)

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.