Various parameter binding methods of SPRINGMVC

Source: Internet
Author: User

1. Basic data Types (in int, for example, others are similar):
Controller code:

@RequestMapping ("saysth.do") public void test (int Count) {}

Form Code:

<form action= "saysth.do" method= "post" ><input name= "count" value= "ten" type= "text"/>......</form>

The name value of input in the form is consistent with the Controller's parameter variable name to complete data binding, and @requestparam annotations can be used if inconsistent. It should be noted that if the controller method parameter is defined in the basic data type, but the data submitted from the page is null or "", the data conversion exception Occurs. That is, you must ensure that the data passed by the table only son cannot be null or "", so in the development process, for potentially empty data, it is best to define the parameter data type as a wrapper type, see the example BELOW.

2. Package Type (in The case of integer, other similar):
Controller code:

@RequestMapping ("saysth.do") public void test (Integer Count) {}

Form Code:

<form action= "saysth.do" method= "post" ><input name= "count" value= "ten" type= "text"/>......</form>

Basically the same as the basic data type, except that the data passed by the table only son can be null or "", for example, if num in the form is "" or there is no num in the form input, then the NUM value in the Controller method parameter is Null.

3. Custom Object Types:
Model Code:

public class User {    private String firstName;    Private String lastName;    Public String getfirstname () {        return firstName;    }    public void Setfirstname (String firstName) {        this.firstname = firstName;    }    Public String getlastname () {        return lastName;    }    public void Setlastname (String lastName) {        this.lastname = lastName;    }}

Controller code:

@RequestMapping ("saysth.do") public void test (user User) {}

Form Code:

<form action= "saysth.do" method= "post" ><input name= "firstName" value= "zhang" type= "text"/><input name= " LastName "value=" three "type=" text "/>......</form>

Very simple, just match the Object's property name to the name value of input one by one.

4. Custom Composite Object Types:
Model Code:

public class ContactInfo {    private String tel;    Private String address;    Public String Gettel () {        return tel;    }    public void Settel (String tel) {        this.tel = tel;    }    Public String getaddress () {        return address;    }    public void setaddress (String address) {        this.address = address;    }} public class User {    private String firstName;    Private String lastName;    Private ContactInfo contactinfo;    Public String getfirstname () {        return firstName;    }    public void Setfirstname (String firstName) {        this.firstname = firstName;    }    Public String getlastname () {        return lastName;    }    public void Setlastname (String lastName) {        this.lastname = lastName;    }    Public ContactInfo getcontactinfo () {        return contactinfo;    }    public void Setcontactinfo (contactinfo Contactinfo) {        this.contactinfo = contactinfo;    }}

Controller code:

@RequestMapping ("saysth.do") public void test (user user) {    System.out.println (user.getfirstname ());    System.out.println (user.getlastname ());    System.out.println (user.getcontactinfo (). Gettel ());    System.out.println (user.getcontactinfo (). getaddress ());}

Form Code:

<form action= "saysth.do" method= "post" ><input name= "firstName" value= "zhang"/><br><input name= " LastName "value=" three "/><br><input name=" contactinfo.tel "value=" 13809908909 "/><br><input Name= "contactinfo.address" value= "beijing haidian"/><br><input type= "submit" value= "Save"/></form>

The ContactInfo attribute is in the user object, and the code in the controller is consistent with the 3rd, but in the form code, you need to use the property name (property of the object type). name of the input Name.

5. List Bindings:
The list needs to be bound to the object, not directly in the parameters of the controller Method.
Model Code:

public class User {    private String firstName;    Private String lastName;    Public String getfirstname () {        return firstName;    }    public void Setfirstname (String firstName) {        this.firstname = firstName;    }    Public String getlastname () {        return lastName;    }    public void Setlastname (String lastName) {        this.lastname = lastName;    }} public class Userlistform {    private list<user> users;    Public list<user> getusers () {        return users;    }    public void Setusers (list<user> users) {        this.users = users;    }}

Controller code:

@RequestMapping ("saysth.do") public void test (userlistform UserForm) {for    (User user:userForm.getUsers ()) {        System.out.println (user.getfirstname () + "-" + user.getlastname ());}    }

Form Code:

<form action= "saysth.do" method= "post" ><table><thead><tr><th>first Name</th> <th>last name</th></tr></thead><tfoot><tr><td colspan= "2" ><input Type= "submit" value= "Save"/></td></tr></tfoot><tbody><tr><td><input Name= "users[0].firstname" value= "aaa"/></td><td><input name= "users[0].lastname" value= "bbb"/ ></td></tr><tr><td><input name= "users[1].firstname" value= "ccc"/></td> <td><input name= "users[1].lastname" value= "ddd"/></td></tr><tr><td><input Name= "users[2].firstname" value= "eee"/></td><td><input name= "users[2].lastname" value= "fff"/ ></td></tr></tbody></table></form>

In fact, this is somewhat similar to the binding of the Contantinfo data in the 4th user object, but the properties inside the Userlistform object are defined as lists, not normal custom Objects. therefore, you need to specify the subscript for the list in the Form. It is worth mentioning that spring creates a list object with a maximum subscript value of size, so if there is a dynamic addition of rows, delete rows in the form, you need to pay special attention to it, such as a table, after the user has been using the process to delete rows, increase the row operation, The subscript value will be inconsistent with the actual size, when the object in the list will have a value only in the form of those that should be subscript, otherwise it will be null, see an example:

Form Code:

<form action= "saysth.do" method= "post" ><table><thead><tr><th>first Name</th> <th>last name</th></tr></thead><tfoot><tr><td colspan= "2" ><input Type= "submit" value= "Save"/></td></tr></tfoot><tbody><tr><td><input Name= "users[0].firstname" value= "aaa"/></td><td><input name= "users[0].lastname" value= "bbb"/ ></td></tr><tr><td><input name= "users[1].firstname" value= "ccc"/></td> <td><input name= "users[1].lastname" value= "ddd"/></td></tr><tr><td><input Name= "users[20].firstname" value= "eee"/></td><td><input name= "users[20].lastname" value= "fff"/ ></td></tr></tbody></table></form>

At this time, userform.getusers () in the controller gets the size of the list to 21, and the 21 user objects are not null, but Both FirstName and LastName in the user object 2nd through 19th are Null. Printing results:

Aaa-bbbccc-dddnull-nullnull-nullnull-nullnull-nullnull-nullnull-nullnull-nullnull-nullnull-nullnull -nullnull-nullnull-nullnull-nullnull-nullnull-nullnull-nullnull-nullnull-nulleee-fff

6. Set Binding:
A set is similar to a list and needs to be bound to an object, not directly in the parameters of the controller Method. however, when binding set data, you must first add the corresponding number of model objects in the set Object.
Model Code:

public class User {    private String firstName;    Private String lastName;    Public String getfirstname () {        return firstName;    }    public void Setfirstname (String firstName) {        this.firstname = firstName;    }    Public String getlastname () {        return lastName;    }    public void Setlastname (String lastName) {        this.lastname = lastName;    }} public class Usersetform {    private set<user> users = new hashset<user> ();    Public usersetform () {        users.add (new User ());        Users.add (new User ());        Users.add (new User ());    }    Public set<user> getusers () {        return users;    }    public void Setusers (set<user> users) {        this.users = users;    }}

Controller code:

@RequestMapping ("saysth.do") public void test (usersetform UserForm) {for    (User user:userForm.getUsers ()) {        System.out.println (user.getfirstname () + "-" + user.getlastname ());}    }

Form Code:

<form action= "saysth.do" method= "post" ><table><thead><tr><th>first Name</th> <th>last name</th></tr></thead><tfoot><tr><td colspan= "2" ><input Type= "submit" value= "Save"/></td></tr></tfoot><tbody><tr><td><input Name= "users[0].firstname" value= "aaa"/></td><td><input name= "users[0].lastname" value= "bbb"/ ></td></tr><tr><td><input name= "users[1].firstname" value= "ccc"/></td> <td><input name= "users[1].lastname" value= "ddd"/></td></tr><tr><td><input Name= "users[2].firstname" value= "eee"/></td><td><input name= "users[2].lastname" value= "fff"/ ></td></tr></tbody></table></form>

Basic and list bindings are similar.
It is important to note that if the maximum subscript value is greater than the set size, the org.springframework.beans.InvalidPropertyException exception is Thrown. therefore, There is some inconvenience when using.

7. Map Bindings:
Map is the most flexible, it also needs to be bound to the object, but not directly written in the Controller method Parameters.
Model Code:

public class User {    private String firstName;    Private String lastName;    Public String getfirstname () {        return firstName;    }    public void Setfirstname (String firstName) {        this.firstname = firstName;    }    Public String getlastname () {        return lastName;    }    public void Setlastname (String lastName) {        this.lastname = lastName;    }} public class Usermapform {    private map<string, user> users;    Public map<string, user> getusers () {        return users;    }    public void Setusers (map<string, user> users) {        this.users = users;    }}

Controller code:

@RequestMapping ("saysth.do") public void Test (usermapform UserForm) {    for (map.entry<string, user> Entry: Userform.getusers (). entryset ()) {        System.out.println (entry.getkey () + ":" + entry.getvalue (). getfirstname () + "-" +        entry.getvalue (). getlastname ());}    }

Form Code:

<form action= "saysth.do" method= "post" ><table><thead><tr><th>first Name</th> <th>last name</th></tr></thead><tfoot><tr><td colspan= "2" ><input Type= "submit" value= "Save"/></td></tr></tfoot><tbody><tr><td><input Name= "users[' x '].firstname" value= "aaa"/></td><td><input name= "users[' x '].lastname" value= "bbb" /></td></tr><tr><td><input name= "users[' y '].firstname" value= "ccc"/></td> <td><input name= "users[' y '].lastname" value= "ddd"/></td></tr><tr><td>< Input name= "users[' z '].firstname" value= "eee"/></td><td><input name= "users[' z '].lastname" value= "fff"/></td></tr></tbody></table></form>

Printing results:

X:aaa-bbby:ccc-dddz:eee-fff

Various parameter binding methods of SPRINGMVC

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.