和基本数据类型基本一样,不同之处在于,JSP表单传递过来的数据可以为null或"",以上面代码为例,如果jsp中num为""或者表单中无num这个input,那么,Controller方法参数中的num值则为null。
3. 自定义对象类型:
Model代码:
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代码:
@RequestMapping("test.do") public void test(User user) { }
JSP表单代码:
<form action="test.do" method="post"> <input value="张" type="text"/> <input value="三" type="text"/> ...... </form>
非常简单,只需将对象的属性名和input的name值一一对应即可。
4. 自定义复合对象类型:
Model代码:
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代码:
@RequestMapping("test.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()); }
JSP表单代码:
<form action="test.do" method="post"> <input value="张" /><br> <input value="三" /><br> <input value="13809908909" /><br> <input value="北京海淀" /><br> <input type="submit" value="Save" /> </form>
User对象中有ContactInfo属性,Controller中的代码和第3点说的一致,但是,在jsp代码中,需要使用“属性名(对象类型的属性).属性名”来命名input的name。
5. List绑定:
List需要绑定在对象上,而不能直接写在Controller方法的参数中。
Model代码:
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代码:
@RequestMapping("test.do") public void test(UserListForm userForm) { for (User user : userForm.getUsers()) { System.out.println(user.getFirstName() + " - " + user.getLastName()); } }
JSP表单代码:
<form action="test.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 value="aaa" /></td> <td><input value="bbb" /></td> </tr> <tr> <td><input value="ccc" /></td> <td><input value="ddd" /></td> </tr> <tr> <td><input value="eee" /></td> <td><input value="fff" /></td> </tr> </tbody> </table> </form>