[Spring mvc]-detailed springmvc of various parameter bindings _java

Source: Internet
Author: User
Tags null null

Various parameter bindings for SPRINGMVC

1. Basic data type (for example int, other 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 parameter variable name, and data binding can be completed, and @requestparam annotations can be used if the inconsistency is inconsistent. Note that if the basic data type is defined in the controller method parameter, 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 conveys cannot be null or "", so in the development process, it is best to define the parameter data type as a wrapper type for data that may be empty, as described in the following example.

2. Packing type (for example in 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, the difference is that the data in the table conveys can be null or "", as in the preceding code, if the form has num "" or the form does not have num this input, then the NUM value in the Controller method parameter is null.

3. Custom Object Type:

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 with the name value of input one by one.

4. Custom Composite Object Type:

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 user object has the ContactInfo attribute, the code in controller is consistent with the 3rd, but in the form code, the property name (the property of the object type) needs to be named. Property name to name the input.

5. List binding:

The list needs to be bound to an object and not written 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 similar to the binding of the Contantinfo data in the 4th User object, but the attributes in the Userlistform object here are defined as list, not as a generic custom object. So, 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 of size, so if you have dynamic add rows and delete rows in your form, you need to pay special attention, such as a table, after the user has repeatedly deleted rows and added rows after using the procedure, The subscript value will be inconsistent with the actual size, at which point the object in the list will have a value only in the form for those that should be subscript, or NULL, for 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 point, the Userform.getusers () in controller gets to the size of the list of 21, and the 21 user objects are not NULL, but The FirstName and LastName in the user object from 2nd to 19th are null. Print results:

AAA-BBB
ccc-ddd
null-null
null-null
null-null
null-null null-null Null-null
   null-null
null-null
null-null
null-null
null-null
null-null null-null nu Ll-null
null-null
null-null
null-null
null-null
eee-fff

6. Set Binding:

A set and a list are similar and need to be bound to an object, not directly in the parameters of the Controller method. However, when you bind 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 ()) { C3/>system.out.println (User.getfirstname () + "-" + user.getlastname ());
  }


Form code:

<form action= "saysth.do" method= "POST" >
<table>
<thead>
<tr>
<th> 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.

A special reminder is that if the maximum subscript value is greater than the set size, a Org.springframework.beans.InvalidPropertyException exception is thrown. Therefore, in the use of some inconvenience.

7. Map Binding:

Map is the most flexible, it also needs to bind to the object, but 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 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> 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>

Print results:

X:AAA-BBB
y:ccc-ddd
z:eee-fff

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.