Several methods springMVC distal pass value (basic data type, package type, custom type, set)

1. The basic data types (int In an example, other 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>

The name form input values ​​and Controller parameter variable names consistent data binding can be done, and if not you can use @RequestParam comment. Note that, if the Controller method parameter is defined in the basic data types, but the data submitted from the pages over to null or "", then, would appear abnormal data conversion.

The form must ensure that data is not transmitted over the type of packaging to null or "" Therefore, in the development process, the data may be empty, it is preferable to define the parameters of the data type, see the following specific examples.

2. Packaging type (Integer, for example, other similar):
the 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>

And essentially the same basic data types, except that the data transfer from the sheet may be null or "", the code to the above example, if the form num is "" or no num this form input, so, the Controller method parameters num values ​​was null.

3. The custom object type:
the Model Code:

public class User {
 private String firstName;
 private String lastName;
  
 省略set,get 方法
}

Controller Code:

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

Form code:

<form action="saysth.do" method="post">
<input name="firstName" value="张" type="text"/>
<input name="lastName" value="三" type="text"/>
......
</form>

4. Custom composite object types:
the Model Code:

public class ContactInfo {
 private String tel;
 private String address;
 
省略set ,get 
 
}
 
public class User {
 private String firstName;
 private String lastName;
 private ContactInfo contactInfo;
  
 省略set ,get 
 
}

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:





User object has ContactInfo property, Controller code and the third point that is consistent, however, in the form code, require the use of "property name (object type attributes). Attribute name" to name input the name.

5. List Binding:
List need to bind on the object, but not directly write the parameter Controller method.

Model Code:

public class User {
 private String firstName;
 private String lastName;
省略set ,get 
 
}
 
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">
<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>
  
</form>

In fact, this and the binding point 4 User object contantInfo data is somewhat similar, but here UserListForm objects inside the property is defined as a List, rather than ordinary custom objects.

So, you need to specify List subscript in the form. It is worth mentioning that, spring will create a List object size to a maximum value index, so if there is a form to dynamically add rows, delete rows of situation, you need to pay special attention to, such as a table, the user during use after several delete rows, increase the operating line, index values ​​will be inconsistent with the actual size,

At this time, List of objects, there is only correspondence in the form of those suffixes will have a value, otherwise is null, look at an example:

Form code:

<form action="saysth.do" method="post">
  
<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>
  
</form>

This time, Controller of userForm.getUsers () to get a List size is 21, and it will not be 21 User object is null, however, the User object 2 through 19 in the firstName and lastName are null. Print result:
aaa - bbb
ccc - ddd
null - null
null - null
...
null - null
eee - FFF

6. Set Binding:
the Set List and similar, also need to bind on the object, but not directly write the parameter Controller method. However, when binding Set data, you must first add the appropriate number of model objects in Set object.
Model Code:

public class User {
 private String firstName;
 private String lastName;
省略set ,get 
 
}
 
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">
 <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>
  
</form>

List of basic and similar bind.

Need to remind that, if the maximum index value is greater than the Set of size, will be thrown org.springframework.beans.InvalidPropertyException exception. Therefore, when using some inconvenience.

7. Map Binding:
Model Code:

public class User {
 private String firstName;
 private String lastName;
省略set ,get 
 
}
 
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">
 <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>
  
</form>

Ssm get started early integration projects, reproduced for reference.
Original Address: https://www.jb51.net/article/145185.htm

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/89288504