Spring provides a number of JSP page commonly used form tags, to a large extent, improve the speed of our development, no more tags to bind properties, and the background to receive data is very simple, you can directly receive the object as a property. The official form label describes the URL for http://docs.spring.io/spring/docs/4.2.6.RELEASE/spring-framework-reference/htmlsingle/#spring-form-tld. The following tags are included:
Using the tags provided by spring on the JSP page requires the introduction of the relevant taglib:<%@ taglib prefix= "form" uri= "Http://www.springframework.org/tags/form"%>
Briefly say a few key tags.
- Form Label:
The form tag contains several unique attributes:
- Method: The request method, which can be set on the controller for the request mode. The default is "_method".
- CommandName: Equivalent to Modelattribute, sets the name of the exposed object. The object is the object stored in the model. The default name is "command".
- Methodparam: Sets the name of the hidden field. The default is "_method".
- Modelattribute: Equivalent to CommandName.
2. Option and Options tab
The option tag is used with the Form:select label for the drop-down list. If the option data is passed from the background to the collection, you can use the Options tab, which has an items property, available collections, map and array data. The Itemlabel label displays the label name. Itemvalue displays the value. The same is also the checkboxes and radiobuttons tags.
3. Error label
The error label is used to pass the message back to the page output. Need to be used in conjunction with Volidator.
How to bind data from the front end to the background, SPRINGMVC provides a @initbinder annotation that we can use to bind the form data to the background method. The annotated method has a parameter webdatabinder, which can be used for data binding and validation.
For example, we now have a form with the following form code:
1 <Form:form>2User:<inputname= "User"type= "text"/>3Address:<inputname= "Address"type= "text"/>4 <inputtype= "Submit"value= "Submit"/>5 </Form:form>
We need to submit the user and address to the background to receive, and there are two objects, namely the user and Userwarpper objects:
1 public class User { 2 3 private String UserName; 4 private int age; 5 Getter Setter ... 6 }
1 Public class Userwarpper {23 Private user user; 4 Private String address; 5 // Getter Setter ... 6 }
Binding the form data to the background requires creating a new property editor for the bound data propertyeditorsupport, where the user is bound to data:
1 Public classUserpropertyeditorextendspropertyeditorsupport{2 3 @Override4 Public voidSetastext (String text)throwsIllegalArgumentException {5 if(text!=NULL){6User User =NewUser ();7 user.setname (text);8 setValue (user);9 }Ten One } A}
The next step is to register in the requested controller:
1 @Controller2@RequestMapping ("/form")3 Public classMyformcontroller {4 5 @InitBinder6 Public voidinit (Webdatabinder binder) {7Binder.registercustomeditor (User.class,Newuserpropertyeditor ());8 }9 Ten@RequestMapping ("/submit") One Public voidSubmit (Userwarpper userwarpper) { A - } -}
Assuming that the front-end input is the user's Name property, when the form is requested, the user property in the form is wrapped into a user object and set to the Userwarpper object.
In addition, Webdatabinder also has a addvalidators (Validator ...) The errors method can also be used to validate data, and to output error messages to the front end with the label.
First establish the validator:
1 Public classMyvalidatorImplementsvalidator{2 3 @Override4 Public BooleanSupports (class<?>clazz) {5 returnUser.class. Equals (Clazz);6 }7 8 @Override9 Public voidValidate (Object target, Errors Errors) {TenValidationutils.rejectifempty (Errors, ' name ', ' name cannot be empty! "); One } A -}
Then add the validator in the Initbinder annotation method:
Binder.addvalidators (new myvalidator ());
The code that displays the error message in the front end is as follows:
1 < path= "name"/>
If you want to display all error messages, use "*".
Spring has built in a lot of basic property editors, such as time editor and so on for us to use:
1Binder.registercustomeditor (Boolean.class,NewCustombooleaneditor (true));2Binder.registercustomeditor (number.class,NewCustomnumbereditor (number.class,true));3Binder.registercustomeditor (ArrayList.class,NewCustomCollectionEditor (ArrayList.class,true));4Binder.registercustomeditor (Date.class,NewCustomdateeditor (NewSimpleDateFormat ("Yyyy-mm-dd"),true));
Spring binding form data