springMVC对简单对象、Set、List、Map的数据绑定和常见问题(二)

六、List绑定java

List须要绑定在对象上,而不能直接写在Controller方法的参数中。app

 

  1. public class User {  jsp

  2.   

  3.     private String firstName;  post

  4.   

  5.     private String lastName;  spa

  6.   

  7.     。。。  code

  8.   

  9. }  orm

  10.   

  11.        public class UserListForm {  对象

  12.   

  13.     private List users;  get

  14.   

  15.     。。。  input

  16.   

  17. }  

  18. @RequestMapping("test.do")  

  19. public void test(UserListForm userForm) {  

  20.     for (User user : userForm.getUsers()) {  

  21.         System.out.println(user.getFirstName() + " - " + user.getLastName());  

  22.     }  

  23. }  

  24. <</span>form action="test.do" method="post">  

  25.    <</span>table>  

  26.       <</span>thead>  

  27.          <</span>tr>  

  28.             <</span>th>First Name</</span>th>  

  29.             <</span>th>Last Name</</span>th>  

  30.          </</span>tr>  

  31.       </</span>thead>  

  32.       <</span>tfoot>  

  33.          <</span>tr>  

  34.             <</span>td colspan="2"><</span>input type="submit" value="Save" /></</span>td>  

  35.          </</span>tr>  

  36.       </</span>tfoot>  

  37.       <</span>tbody>  

  38.          <</span>tr>  

  39.             <</span>td><</span>input name="users[0].firstName" value="aaa" /></</span>td>  

  40.             <</span>td><</span>input name="users[0].lastName" value="bbb" /></</span>td>  

  41.          </</span>tr>  

  42.          <</span>tr>  

  43.             <</span>td><</span>input name="users[1].firstName" value="ccc" /></</span>td>  

  44.             <</span>td><</span>input name="users[1].lastName" value="ddd" /></</span>td>  

  45.          </</span>tr>  

  46.          <</span>tr>  

  47.             <</span>td><</span>input name="users[2].firstName" value="eee" /></</span>td>  

  48.             <</span>td><</span>input name="users[2].lastName" value="fff" /></</span>td>  

  49.          </</span>tr>  

  50.       </</span>tbody>  

  51.    </</span>table>  

  52. </</span>form>  

其实,这和第4点User对象中的contantInfo数据的绑定有点相似,可是这里的UserListForm对象里面的属性被定义成List,而不是普通自定义对象。因此,在JSP中须要指定List的下标。值得一提的是,Spring会建立一个以最大下标值为size的List对象,因此,若是JSP表单中有动态添加行、删除行的状况,就须要特别注意,譬如一个表格,用户在使用过程当中通过屡次删除行、增长行的操做以后,下标值就会与实际大小不一致,这时候,List中的对象,只有在jsp表单中对应有下标的那些才会有值,不然会为null,看个例子:

  1. <</span>form action="test.do" method="post">  

  2.    <</span>table>  

  3.       <</span>thead>  

  4.          <</span>tr>  

  5.             <</span>th>First Name</</span>th>  

  6.             <</span>th>Last Name</</span>th>  

  7.          </</span>tr>  

  8.       </</span>thead>  

  9.       <</span>tfoot>  

  10.          <</span>tr>  

  11.             <</span>td colspan="2"><</span>input type="submit" value="Save" /></</span>td>  

  12.          </</span>tr>  

  13.       </</span>tfoot>  

  14.       <</span>tbody>  

  15.          <</span>tr>  

  16.             <</span>td><</span>input name="users[0].firstName" value="aaa" /></</span>td>  

  17.             <</span>td><</span>input name="users[0].lastName" value="bbb" /></</span>td>  

  18.          </</span>tr>  

  19.          <</span>tr>  

  20.             <</span>td><</span>input name="users[1].firstName" value="ccc" /></</span>td>  

  21.             <</span>td><</span>input name="users[1].lastName" value="ddd" /></</span>td>  

  22.          </</span>tr>  

  23.          <</span>tr>  

  24.             <</span>td><</span>input name="users[20].firstName" value="eee" /></</span>td>  

  25.             <</span>td><</span>input name="users[20].lastName" value="fff" /></</span>td>  

  26.          </</span>tr>  

  27.       </</span>tbody>  

  28.    </</span>table>  

  29. </</span>form>  

这个时候,Controller中的userForm.getUsers()获取到List的size为21,并且这21个User对象都不会为null,可是,第2到第19的User对象中的firstName和lastName都为null。打印结果:

 

 

  1. aaa - bbb  

  2. ccc - ddd  

  3. null - null  

  4. null - null  

  5. null - null  

  6. null - null  

  7. null - null  

  8. null - null  

  9. null - null  

  10. null - null  

  11. null - null  

  12. null - null  

  13. null - null  

  14. null - null  

  15. null - null  

  16. null - null  

  17. null - null  

  18. null - null  

  19. null - null  

  20. null - null  

  21. eee - fff  

相关文章
相关标签/搜索