Various parameter binding methods of SpringMVC
1. Basic data type (taking int as an 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="10" type="text"/>......</form>
If the name value of input in the form is consistent with the parameter variable name of the Controller, data binding can be completed. If it is inconsistent, you can use the @RequestParam annotation. It should be noted that if the basic data type is defined in the Controller method parameter, but the data submitted from the page is null or "", data conversion exceptions will occur. That is to say, it is necessary to ensure that the data passed by the form cannot be null or "", so, during the development process, it is best to define the parameter data type as a packaging type for data that may be empty, see the following example.
2. Packaging type (taking Integer as an example, others are similar):
Controller code:
@RequestMapping("saysth.do")public void test(Integer count) {}Form code:
<form action="saysth.do" method="post"><input name="count" value="10" type="text"/>......</form>
It is basically the same as the basic data type. The difference is that the data passed by the form can be null or "". The above code is as an example. If num in the form is "" or there is no num input in the form, 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>
It's very simple, just match the object's property name and the input's name value one by one.
4. Customize compound 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>
There is a ContactInfo attribute in the User object. The code in Controller is consistent with what is said in point 3. However, in the form code, you need to use "attribute name (attribute of object type).attribute name" to name the input name.
5. List binding:
List needs to be bound to the object, and cannot be 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><td><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><td><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 User object in the 4th point, but the properties in the UserListForm object here are defined as List, rather than ordinary custom objects. Therefore, you need to specify the subscript of List in the form. It is worth mentioning that Spring will create a List object with the maximum subscript value as size. Therefore, if there are dynamic addition and deletion of rows in the form, special attention should be paid. For example, after a table is used, the subscript value will be inconsistent with the actual size after the user deletes rows and adds rows multiple times during use. At this time, only the objects in List will have values in the form that have subscripts, 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><td><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><td><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 Controller gets the size of the List to be 21, and none of these 21 User objects are null, but the firstName and lastName in the User objects from 2 to 19 are null. Print result:
aaa - bbbccc - dddnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullnull - nullee - fff
6. Set binding:
Set is similar to List, and it also needs to be bound to the object, and cannot be written directly in the parameters of the Controller method. However, when binding Set data, the corresponding number of model objects must be added first 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><td><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><td><td><input name="users[2].firstName" value="eee" /></td><td><input name="users[2].lastName" value="fff" /></td></tr></tbody></table></form>
Basically similar to List binding.
It should be noted that if the maximum subscript value is greater than the size of Set, an org.springframework.beans.InvalidPropertyException will be thrown. Therefore, it is a bit inconvenient when using it.
7. Map binding:
Map is the most flexible. It also needs to be bound to the object and cannot be 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 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><td><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><td><td><input name="users['z'].firstName" value="eee" /></td><td><input name="users['z'].lastName" value="ffff" /></td></tr></tbody></table></form>
Print result:
x: aaa - bbby: ccc - dddz: eee - fff
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.