I. Overview
Second, PropertyEditor
1, Basic introduction
PropertyEditor is used to convert between string<--->object, and some common propertyeditor are built in spring, such as:
Classeditor: string<-->classfileeditor: string<-->filepatterneditor: string<--> Patternurleditor: string<-->urlresourceeditor: string<-->resource
The custom propertyeditor must inherit from PropertyEditorSupport, as shown in the following example:
Entity class
Public classUserInfo {PrivateString name; PrivateInteger age; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; }}
Conversion class
Public classUsereditorextendsPropertyEditorSupport { PublicUsereditor () {System.out.println ("Usereditor constructed"); } @Override PublicString Getastext () {UserInfo UserInfo= (UserInfo) This. GetValue (); returnjson.tojsonstring (UserInfo); } @Override Public voidSetastext (String text)throwsjava.lang.IllegalArgumentException {if(Stringutils.isempty (text)) {return; } UserInfo UserInfo= Json.parseobject (text, UserInfo.class); This. SetValue (UserInfo); }}
@Controller Public classTestController5 {@RequestMapping ("/type1") @ResponseBody PublicString TestType1 (@RequestParam ("User") UserInfo UserInfo) {System.out.println (Userinfo.getname () + "" + userinfo.getage ()); return"TestType1"; } @RequestMapping ("/type2") @ResponseBody PublicString testType2 () {
System.out.println ("void"); return"TestType2"; }
Register Usereditor @InitBinder Public voidInitbinder (Webdatabinder binder) {usereditor usereditor=NewUsereditor (); Binder.registercustomeditor (UserInfo.class, Usereditor); System.out.println ("Initbinder invoked"); }}
Web. XML and Spring-mvc.xml configuration slightly
After starting, visit:
http://localhost:8080/myweb/type1?user={"name": "Matt", "Age": 30}http://localhost:8080/myweb/type2http:// localhost:8080/myweb/type1?user={"name": "Matt", "Age": 30}
Output:
Usereditor constructedinitbinder Invokedmatt 30voidUserEditor constructedinitbinder Invokedmatt 30
As can be seen from the output, if the processing method contains parameters that require a type conversion, each request invokes the registration method, and the processing method, if not included, does not invoke the
2. Registration of Custom PropertyEditor
There are three levels of registration for custom PropertyEditor:
- Controller level, using @initbinder
- Web level, using Webbindinginitializer
- Application level, custom PropertyEditor and entity similar package, named "Entity name + Editor"
i) Controller level
The controller level is implemented in the Controller class using @initbinder annotations, with a single controller registered, as shown in the example above
II) web-level
The web level is implemented using Webbindinginitializer, which registers all the controllers (that is, the entire Web tier) for the application, as an example:
Webbindinginitializer Implementation Class
public class Mywebbindinginitializer implements Webbindinginitializer {public void Initbinder (Webdatabinder binder, WebRequest request) {Binder.registercustomeditor (Userinfo.class, New Usereditor ()); System.out.println ("Initbinder invoked!");}}
Registered
<Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> < Propertyname= "Webbindinginitializer"> <Beanclass= "Cn.matt.convertor.MyWebBindingInitializer"/> </ Property> </Bean>
Note the registration method in the previous example controller to test
The registration method is called the same as the controller level, that is, if the processing method contains parameters that require type conversion, each request invokes the registration method, and the processing method, if not included, does not invoke the
III) application level (recommended)
Application level registration scope is the spring layer, using: custom PropertyEditor and entity similar package, and named "Entity name + Editor", Example: Userinfoeditor
The registration method creates a new instance each time the type is converted
Introduction to Spring MVC usage (eight)--annotated Controller (III): type conversion