SpringMVC (2) -- process data models, ModelAndView, Model, Map, redirection, @ ModelAttribute ,,

Source: Internet
Author: User

SpringMVC (2) -- process data models, ModelAndView, Model, Map, redirection, @ ModelAttribute ,,

1. Process Model Data

Spring MVC provides the following ways to output Model data:-ModelAndView: when the return value type of the processing method is ModelAndView, the method body can use this object to add Model data-Map, Model, and ModelMap: the input parameter is org. springframework. ui. model, org. springframework. ui. modelMap or java. uti. when Map is returned, the data in Map is automatically added to the model. Whether the return value is String or ModelAndView, SpringMVC parses the return value into a ModelAndView object after the Handle method is executed. Data in Map, Model, and ModelMap will be placed into the ModelAndView object, use-@ SessionAttributes as the Model: save an attribute in the Model to HttpSession, so that multiple requests can share this attribute-@ ModelAttribute: after adding the annotation to the method input parameter, the object of the input parameter will be placed in the data model. 2. if the return value of the processing method of the ModelAndView controller is ModelAndView, it contains both view information and model data information. Add model data:-MoelAndView addObject (String attributeName, Object attributeValue)-ModelAndView addAllObject (Map <String,?> ModelMap) set View:-void setView (view View)
-Void setViewName (String viewName)
@ RequestMapping (value = "/testModelAndView") public ModelAndView testModelAndView () {ModelAndView mv = new ModelAndView (); mv. setViewName (SUCCESS); // The logical view is converted to the physical view. addObject ("user", "Michael"); // corresponds to the request. setAttribute ("user", "zhangsan") works the same way as return mv ;}

3. Model and Map

Spring MVC uses an org. springframework. ui. specific steps to store Model data through the Model interface-Spring MVC creates an implicit Model object before calling the method as the storage container for Model data. -If the input parameters of the method are Map or Model, Spring MVC will pass the reference of the implicit Model to these input parameters. In the method body, developers can access all data in the model through this input parameter object, or add new attribute data to the model.
@ RequestMapping (value = "/testMap") public String testMap (Map <String, Object> map) {map. put ("user", new Student ("", "asd@asd.com"); return SUCCESS;} @ RequestMapping (value = "/testModelMap") public String testModelMap (ModelMap model) {model. addattriess ("user", "hah"); return SUCCESS ;}

4. Redirection

Generally, the value returned by the Controller method to a string type is treated as a logic view name. If the returned string contains a forward: or redirect: prefix, SpringMVC will perform special processing on it: use forward: and redirect: as the indicator and the subsequent string as the URL to process-redirect:/success. jsp: it will complete one to success. jsp redirection operation-forward:/success. jsp: it will complete one to success. the following code can be configured in the springmvc configuration file for jsp forwarding operations. Our request can go directly to another page without going through the controller.
<mvc:view-controller path="/redirect" view-name="success"/>

However, the original @ RequestMapping URL cannot be accessed normally.

<mvc:annotation-driven></mvc:annotation-driven>

5. @ ModelAttribute [Application Scenario]

Use the @ ModelAttribute annotation in method definition: Spring MVC calls the @ ModelAttribute method at the method level one by one before calling the target processing method. Use the @ ModelAttribute annotation before the input parameter of the method:-You can obtain the object from the implicit model data from the implicit object, and then bind the request parameter to the object, input the input parameter again-Add the method input parameter object to the model. We will execute the method obtained before each request.
@ModelAttributepublic void getStudent(Map<String, Object> map){Student student = new Student("zhangsan","zhangsan@163.com");map.put("stu", student);}@RequestMapping(value="/toUpdatePage",method=RequestMethod.POST)public String toUpdatePage(@ModelAttribute(value="stu") Student stu){System.out.println(stu);eturn "success";}

6. SpringMVC determines the POJO type input parameter of the target method.

1. determine a key: 1 ). if the parameter of the POJO type of the target method does not use @ ModelAttribute as the modifier and it also exists by default, the key is the lower case of the first letter of the POJO class name. 2 ). if @ ModelAttribute is used for modification, the key is the value attribute value of the @ ModelAttribute annotation 2. search for the object corresponding to the key in implicitModel [request field]. If the object exists, it is passed as an input parameter. If the Map is saved in the @ ModelAttribute flag method, and the key is consistent with the key identified by 1, it will be obtained! 3. if the object corresponding to the key does not exist in implicitModel, check whether the current Handler is decorated with the @ SessionAttribute annotation. If this annotation is used, and the value Attribute value of @ SessionAttributes annotation contains the key, the value corresponding to the key is obtained from HttpSession. If the value exists, it is directly passed into the input parameter of the target method. If the value does not exist, an exception is thrown. 4. if the Handler does not identify the @ SessionAttributes annotation or the value of @ SessionAttributes annotation does not contain the key, a POJO-type parameter is created by reflecting [A no-argument constructor is required, the parameter passed in as the target method. 5. SpringMVC will save the key and POJO objects to the implicitModel and save them to the request domain!
Note: 1. @ ModelAttribute indicates that the method will be called by SpringMVC before each target method is executed! 2. @ ModelAttribute annotation can be used to modify the input parameters of the POJO type of the target method. The value attribute value has the following functions: 1) SpringMVC will use the value attribute value to find the corresponding object in implicitModel, if yes, it will be passed directly to the input parameter of the target method. 2) SpringMVC will store the object of the value type as key and POJO as value in the request domain. 7. if you want to share a model attribute data among multiple requests, you can mark @ SessionAttribute on the Controller class, springMVC saves the corresponding attributes in the model to HttpSession-it can only be marked on the class, so that multiple requests can share a part of the data values: Specifies the key types placed in the session domain: specify the byte code of the object in the session domain. The code below will save the modified stu to the session, and you can call it through the EL expression on the page.

It can also be implemented using the types attribute, but it is generally not needed, so it is easy to make mistakes

 

 

Related Article

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.