Spring MVC accepts multi-object processing
Spring MVC feels great, especially when it's self-binding for receiving object parameters, but it's a bit confusing to pass multiple objects at the same time.
At the same time, the project did not directly use spring's formtag.
A multi-object value learned from the Internet. I optimized the next, the original text can not find the source of the record here.
First, declare a note class for the declaration of the Value object
/** * Processing Spring MVC Object Binding Annotations * */@Target (Elementtype.parameter) @Retention (retentionpolicy.runtime) @Documentedpublic @ Interface Requestbean {String value () default "_def_param_name";}
then there is an implementation class for the Webargumentresolver, and the object parameter binding is primarily this class to handle
/** * Object value of the parameter binding processing * */public class Beanargumentresolver implements Webargumentresolver {@SuppressWarnings ("rawtypes") Public Object resolveargument (methodparameter param, nativewebrequest request) {Requestbean Requestbean = Param.getparameterannotation (Requestbean.class); Try{if (Requestbean! = null) {String _param = Requestbean.value (); if ( _param.equals ("_def_param_name")) {_param = Param.getparametername ();} Class clazz = Param.getparametertype (); Object object = Clazz.newinstance ();//Instantiation of object by reference type Hashmap<string, string[] > paramsmap = new hashmap<string, string[]> ();iterator<string> itor = Request.getparameternames ();// Gets the full number of request references while (Itor.hasnext ()) {String Webparam = (string) itor.next ();//Iteration GET request Reference string[] Webvalue = Request.getparametervalues (Webparam);//Get all values corresponding to the request parameters list<string> Webvaluelist = new Arraylist<string> () ; for (int i = 0;i<webvalue.length;i++) {if (webvalue[i]!=null&&! "". Equals (Webvalue[i])) {Webvaluelist.add (webvalue[i]);//Gets a value that is not empty}}if (webParam.startswith (_param) &&!webvaluelist.isempty ()/* Infer if the parameter starts with the method parameter name */{paramsmap.put (Webparam, Webvaluelist.toarray (New String[webvaluelist.size ()));}} Beanwrapper obj = new Beanwrapperimpl (object);//This class enables the ability to set and get properties. Obj.registercustomeditor (Date.class, NULL, new Customdateeditor (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"), true)) ;//system.out.println (Obj.findcustomeditor (Date.class, null). Getastext ());
The following is an assignment of a value for the object property for (String PropName:paramsMap.keySet ()) {Object propvals = Paramsmap.get (propname); string[] props = propname.split ("\ \"); if (props.length = = 2) {obj.setpropertyvalue (props[1], propvals);} else if (props.length = = 3) {Object tmpobj = Obj.getpro Pertyvalue (props[1]); if (tmpobj = = null) obj.setpropertyvalue (props[1], Obj.getpropertytype (Props[1]). NewInstance () ); Obj.setpropertyvalue (Props[1] + "." + props[2], propvals);}} return object;}} catch (Exception e) {e.printstacktrace ();} return webargumentresolver.unresolved;}}
Two classes are written, configure the MVC configuration file
<mvc:annotation-driven><mvc:argument-resolvers><bean class= "Xx.xx.xx.xx.BeanArgumentResolver"/ ></mvc:argument-resolvers><mvc:message-converters register-defaults= "true" ><!-- Set the default encoding for Stringhttpmessageconverter to UTF-8--><bean class= " Org.springframework.http.converter.StringHttpMessageConverter "> <constructor-arg value=" UTF-8 "/> </bean><!--Set the default formatted output of Jackson2httpmessageconverter to True--><bean class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter "> <property name=" Prettyprint "value=" true "/> </bean> </mvc:message-converters></mvc:annotation-driven >
The next step is to use the MVC controller method such as the following
@RequestMapping (value= "/saveevent") @ResponseBodypublic ajaxresult saveevent (@RequestBean event event) {event = Eventservice.savetemporaryevent (event); return Ajaxresult.objectresult (event);}
page Form form code
< Span style= "Color:rgb (51,51,51); font-family: Microsoft Ya hei, song body; font-size:14px; Line-height:24px ">
<TD align= "Right" > Customer code:</td> <td><input type= "hidden" name= "Event.submitUser.userId" Value= "${event.submituser.userid}"/></td> <td align= "right" > Customer phone:</td> <td> <input class= "Inpname_" type= "text" id= "Submittel" name= "Event.submittel" value= "${event.submittel}"/></td >
Spring MVC accepts multi-object disposition