SPRINGMVC---Processing model data methods *

Source: Internet
Author: User
Tags domian

Undertake one or two chapters

index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Model Modification Operations1, raw data is 1 Tom 123456 2, the password can not be modified3, the form is echoed, and the simulation operation directly fills in the corresponding property values in the form--><form action= "Springmvc/testmodelattribute" method= "get" > <input type= "hidden" name= "id" value= "1"/ > <br>name<input type= "text" name= "username" value= "Tom"/> <br> <!--pw<input type= "text" name= "password" Value= "123456"/>-<input type= "Submit" value= "submit"/></form><br><br><form Act ion= "Springmvc1/testmodelattribute" method= "get" > <input type= "hidden" name= "id" value= "1"/> <br>name<input type= "text" name= "username" value= "Tom"/> <br> <!--pw<input type= "text" name= "password" Value= "123456"/>-<input type= "Submit" value= "Submit"/></form></body>

Test.java

 PackageCom.hdxy.domian;Importjava.util.Arrays;Importjava.util.Date;ImportJava.util.Map;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.hdxy.pojo.User; @RequestMapping ("Springmvc") @Controller Public classTest {/*Process Model Data methods: * @1 Modelandview (can contain view and model information) * Processing method when the return value type is Modelandview: The method body can add model data through the object * @2map and model * Entry:   Org.springframework.ui.model,org.springframework.ui.modelmap * or JAVA.UI.MAP when the processing method returns, the data in the map is automatically added to the model. * @3 sessionattributes: * A property in the model is temporarily present in httpsession so that the parameter can be shared between multiple requests * @4 Modelattribute: * method when the annotation is annotated, the object of the entry is placed into the data model * SPRINGMVC determines the process by which the target method Pojo type into the parameter: * 1. Determine a key * 1). If the parameter of the Pojo type of the target method does not use @modeleattribute as a modifier then the key amount is Pojo class name first letter lowercase * 2). If @modelattribute is used to decorate, the key is the value of the @modeleattribute annotation for the values of * 2. In Implicitmodel, find the object that corresponds to the key, and if present, pass in as input * 1). If the @modelattribute tag method is stored in the map, and key and 1 determine the key one to, then get to * 3. If there is no object of key corresponding to the Implicitmodel, Check that the current handler is a Buddha using sessionattrbute annotation decoration * If the annotation is used, the value of the @sessionattributes annotation contains key, and the corresponding amount of key * is obtained from httpsession The value, if present, is passed directly into the target parameter method, and throws an exception if it does not exist * 4. If handler does not identify @sessionattributes annotations or @sessionattributes annotations that do not contain a key, the * will pass Reflection to create a parameter of type Pojo, the parameter of the incoming target method * 5.SpringMVC will protect the key and valuestored in Implicitmodel, and then saved to request * *  */   Final  PublicString success= "Loginsuccess"; @RequestMapping (Value= "/modelandview")    PublicModelandview Testmodelandview () {String viewName=SUCCESS; Modelandview Modelv=NewModelandview (viewName); Modelv.addobject ("Time",NewDate ()); returnModelv; } @RequestMapping ("/map")    PublicString TestMap (map<string,object>map) {       /*System.out.println (Map.getclass (). GetName ());//test Modelmap*/Map.put ("Names", Arrays.aslist ("Tom", "Jerry", "2")); returnSUCCESS; } @RequestMapping ("/testmodelattribute")    PublicString testmodelattribute (user user) {//This method has a new new user class to assign the modified password to be empty. (Does not meet the need to change passwords)//(Test2.java) and Modelattribute is getting the user class from the database (assigning the modified property to the original value without modification) and assigning the valueSystem.out.println ("Modify" +user); returnSUCCESS; }}

Test2.java

 PackageCom.hdxy.domian;ImportJava.util.Map;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportCom.hdxy.pojo.User; @RequestMapping ("/springmvc1") @Controller Public classTest2 {Final  PublicString success= "Loginsuccess"; @ModelAttribute Public voidGetUser (@RequestParam (value= "id", required=false) Integer ID, Map<String,Object>map) {        if(id! =NULL){            //To simulate getting an object from the databaseUser user=NewUser (); User.setid (1); User.setusername ("Tom"); User.setpassword ("123456"); System.out.println ("Get user from database" +user); /*map.put ("ADC", user);*/Map.put ("User", user); }    }/*** Operation process * 1. Execution: @ModelAttribute annotation Decoration Method: Remove the object from the database, put the key value in the map: User * 2.springMVC Remove the user object from the map, and assign the request parameter of the form to the user object corresponding to the property * 3.SpringMVc to pass the above object into the parameters of the target method * Note: In the method of @modelattribute annotation decoration, (can only modify the class) * when placed in the map of the key value needs and the target method into the parameter type of the first Letter Lowercase String Consistent * * Parma ("abc") User User **/@RequestMapping ("/testmodelattribute")        PublicString testmodelattribute (user user) {System.out.println ("Test SpringMVC1"); System.out.println ("Modify" +user); returnSUCCESS; }             /*** Source Code Analysis: * * The method of calling @modelattribute annotation decoration, actually put the data in the map in the Modelattribute method in Implicitmodel. The target parameter of the parse request processor is derived from The target property of the Webdatabinder object (*). Create a Webdatabinder object: *--determines the ObjectName property (if the value of the Attrname attribute passed in is: "", ObjectName is the first letter of the class name lowercase)       。 * Note: attrname if the target violation Pojo attribute is decorated with modelattribute, then the value of the value of Modelattribute is attrname values *---Determine the target property: * in Implic Itmodel find Attrname Corresponding property value, if present, Ok * > If not present: Verify that the current Handel stone Buddha used @sessionattributes to decorate, if used, try to get attrname from the session Expected property Value * If the session does not throw an exception * > If handle is not decorated with @sessionattributes, or @sessionattributtes does not use the value specified in the key and Attrname is matched, the Pojo class * 2 is created by reflection, SPRINGMVC assigns the request of the form to the property of the Webdatabinder target * 3), SPRINGMVC will webdatabinder the tar Get and Attrname to Implicitmodel * 4), pass the target of the Webdatabinder to the parameter of the destination method **/}

SPRINGMVC---Processing model data methods *

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.