博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC数据绑定大全 .
阅读量:5081 次
发布时间:2019-06-13

本文共 24932 字,大约阅读时间需要 83 分钟。

刚开始用spring mvc 做web开发时,经常会不知道如何合适绑定页面数据.用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便(本人最开始也是这么认为),经过一段时间的应用和测试,总结出以下几种情况,希望对刚接触spring mvc 的朋友有所帮助.

查看spring源码可以看出spring支持转换的数据类型已经很多了.

 

org.springframework.beans.PropertyEditorRegistrySupport.java

[java]
  1. /** 
  2.      * Actually register the default editors for this registry instance. 
  3.      */  
  4.     private void doRegisterDefaultEditors() {  
  5.         this.defaultEditors = new HashMap<Class, PropertyEditor>(64);  
  6.         // Simple editors, without parameterization capabilities.   
  7.         // The JDK does not contain a default editor for any of these target types.   
  8.         this.defaultEditors.put(Charset.classnew CharsetEditor());  
  9.         this.defaultEditors.put(Class.classnew ClassEditor());  
  10.         this.defaultEditors.put(Class[].classnew ClassArrayEditor());  
  11.         this.defaultEditors.put(Currency.classnew CurrencyEditor());  
  12.         this.defaultEditors.put(File.classnew FileEditor());  
  13.         this.defaultEditors.put(InputStream.classnew InputStreamEditor());  
  14.         this.defaultEditors.put(Locale.classnew LocaleEditor());  
  15.         this.defaultEditors.put(Pattern.classnew PatternEditor());  
  16.         this.defaultEditors.put(Properties.classnew PropertiesEditor());  
  17.         this.defaultEditors.put(Resource[].classnew ResourceArrayPropertyEditor());  
  18.         this.defaultEditors.put(TimeZone.classnew TimeZoneEditor());  
  19.         this.defaultEditors.put(URI.classnew URIEditor());  
  20.         this.defaultEditors.put(URL.classnew URLEditor());  
  21.         // Default instances of collection editors.   
  22.         // Can be overridden by registering custom instances of those as custom editors.   
  23.         this.defaultEditors.put(Collection.classnew CustomCollectionEditor(Collection.class));  
  24.         this.defaultEditors.put(Set.classnew CustomCollectionEditor(Set.class));  
  25.         this.defaultEditors.put(SortedSet.classnew CustomCollectionEditor(SortedSet.class));  
  26.         this.defaultEditors.put(List.classnew CustomCollectionEditor(List.class));  
  27.         this.defaultEditors.put(SortedMap.classnew CustomMapEditor(SortedMap.class));  
  28.         // Default editors for primitive arrays.   
  29.         this.defaultEditors.put(byte[].classnew ByteArrayPropertyEditor());  
  30.         this.defaultEditors.put(char[].classnew CharArrayPropertyEditor());  
  31.         // The JDK does not contain a default editor for char!   
  32.         this.defaultEditors.put(char.classnew CharacterEditor(false));  
  33.         this.defaultEditors.put(Character.classnew CharacterEditor(true));  
  34.         // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.   
  35.         this.defaultEditors.put(boolean.classnew CustomBooleanEditor(false));  
  36.         this.defaultEditors.put(Boolean.classnew CustomBooleanEditor(true));  
  37.         // The JDK does not contain default editors for number wrapper types!   
  38.         // Override JDK primitive number editors with our own CustomNumberEditor.   
  39.         this.defaultEditors.put(byte.classnew CustomNumberEditor(Byte.classfalse));  
  40.         this.defaultEditors.put(Byte.classnew CustomNumberEditor(Byte.classtrue));  
  41.         this.defaultEditors.put(short.classnew CustomNumberEditor(Short.classfalse));  
  42.         this.defaultEditors.put(Short.classnew CustomNumberEditor(Short.classtrue));  
  43.         this.defaultEditors.put(int.classnew CustomNumberEditor(Integer.classfalse));  
  44.         this.defaultEditors.put(Integer.classnew CustomNumberEditor(Integer.classtrue));  
  45.         this.defaultEditors.put(long.classnew CustomNumberEditor(Long.classfalse));  
  46.         this.defaultEditors.put(Long.classnew CustomNumberEditor(Long.classtrue));  
  47.         this.defaultEditors.put(float.classnew CustomNumberEditor(Float.classfalse));  
  48.         this.defaultEditors.put(Float.classnew CustomNumberEditor(Float.classtrue));  
  49.         this.defaultEditors.put(double.classnew CustomNumberEditor(Double.classfalse));  
  50.         this.defaultEditors.put(Double.classnew CustomNumberEditor(Double.classtrue));  
  51.         this.defaultEditors.put(BigDecimal.classnew CustomNumberEditor(BigDecimal.classtrue));  
  52.         this.defaultEditors.put(BigInteger.classnew CustomNumberEditor(BigInteger.classtrue));  
  53.         // Only register config value editors if explicitly requested.   
  54.         if (this.configValueEditorsActive) {  
  55.             StringArrayPropertyEditor sae = new StringArrayPropertyEditor();  
  56.             this.defaultEditors.put(String[].class, sae);  
  57.             this.defaultEditors.put(short[].class, sae);  
  58.             this.defaultEditors.put(int[].class, sae);  
  59.             this.defaultEditors.put(long[].class, sae);  
  60.         }  
  61.     }  

/** * Actually register the default editors for this registry instance. */ private void doRegisterDefaultEditors() { this.defaultEditors = new HashMap<Class, PropertyEditor>(64); // Simple editors, without parameterization capabilities. // The JDK does not contain a default editor for any of these target types. this.defaultEditors.put(Charset.class, new CharsetEditor()); this.defaultEditors.put(Class.class, new ClassEditor()); this.defaultEditors.put(Class[].class, new ClassArrayEditor()); this.defaultEditors.put(Currency.class, new CurrencyEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Pattern.class, new PatternEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); this.defaultEditors.put(TimeZone.class, new TimeZoneEditor()); this.defaultEditors.put(URI.class, new URIEditor()); this.defaultEditors.put(URL.class, new URLEditor()); // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); // Default editors for primitive arrays. this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor()); this.defaultEditors.put(char[].class, new CharArrayPropertyEditor()); // The JDK does not contain a default editor for char! this.defaultEditors.put(char.class, new CharacterEditor(false)); this.defaultEditors.put(Character.class, new CharacterEditor(true)); // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor. this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true)); // The JDK does not contain default editors for number wrapper types! // Override JDK primitive number editors with our own CustomNumberEditor. this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false)); this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true)); this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false)); this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true)); this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false)); this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true)); this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false)); this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true)); this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false)); this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true)); this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false)); this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true)); this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true)); this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); // Only register config value editors if explicitly requested. if (this.configValueEditorsActive) { StringArrayPropertyEditor sae = new StringArrayPropertyEditor(); this.defaultEditors.put(String[].class, sae); this.defaultEditors.put(short[].class, sae); this.defaultEditors.put(int[].class, sae); this.defaultEditors.put(long[].class, sae); } }  

 

 

1.基本数据类型

很简单,该怎么写就怎么写.

controller代码

[java]
  1. @RequestMapping  
  2.     public void test1(String name, Integer age, Double income, Boolean isMarried, String[] interests)  
  3.     {  
  4.         System.out.println("简单数据类型绑定=========");  
  5.         System.out.println("名字:" + name);  
  6.         System.out.println("年龄:" + age);  
  7.         System.out.println("收入:" + income);  
  8.         System.out.println("已结婚:" + isMarried);  
  9.         System.out.println("兴趣:");  
  10.         for (String interest : interests)  
  11.         {  
  12.             System.out.println(interest);  
  13.         }  
  14.         System.out.println("====================");  
  15.     }  

@RequestMapping public void test1(String name, Integer age, Double income, Boolean isMarried, String[] interests) { System.out.println("简单数据类型绑定========="); System.out.println("名字:" + name); System.out.println("年龄:" + age); System.out.println("收入:" + income); System.out.println("已结婚:" + isMarried); System.out.println("兴趣:"); for (String interest : interests) { System.out.println(interest); } System.out.println("===================="); } 

 

jsp代码

[xhtml]
  1. <form action="test1.do" method="post">  
  2.     <div class="f_left">姓名:</div>  
  3.     <div class="f_left"><input name="name" value="张三"/></div>  
  4.     <div class="clear"></div>  
  5.     <div class="f_left">年龄:</div>  
  6.     <div class="f_left"><input name="age" value="20"/></div>  
  7.     <div class="clear"></div>  
  8.     <div class="f_left">收入:</div>  
  9.     <div class="f_left"><input name="income" value="100000"/></div>  
  10.     <div class="clear"></div>  
  11.     <div class="f_left">结婚:</div>  
  12.     <div class="f_left">  
  13.     <input type="radio" name="isMarried" value="true" checked="checked"/>是  
  14.     <input type="radio" name="isMarried" value="false"/></div>  
  15.     <div class="clear"></div>  
  16.     <div class="f_left">兴趣:</div>  
  17.     <div class="f_left">  
  18.     <input type="checkbox" name="interests" value="听歌" checked="checked"/>听歌  
  19.     <input type="checkbox" name="interests" value="书法" checked="checked"/>书法  
  20.     <input type="checkbox" name="interests" value="看电影" checked="checked"/>看电影  
  21.     </div>  
  22.     <div class="clear"></div>  
  23.     <div><input type="submit" value="提交表单"/></div>  
  24. </form>  

<form action="test1.do" method="post"> <div class="f_left">姓名:</div> <div class="f_left"><input name="name" value="张三"/></div> <div class="clear"></div> <div class="f_left">年龄:</div> <div class="f_left"><input name="age" value="20"/></div> <div class="clear"></div> <div class="f_left">收入:</div> <div class="f_left"><input name="income" value="100000"/></div> <div class="clear"></div> <div class="f_left">结婚:</div> <div class="f_left"> <input type="radio" name="isMarried" value="true" checked="checked"/>是 <input type="radio" name="isMarried" value="false"/>否</div> <div class="clear"></div> <div class="f_left">兴趣:</div> <div class="f_left"> <input type="checkbox" name="interests" value="听歌" checked="checked"/>听歌 <input type="checkbox" name="interests" value="书法" checked="checked"/>书法 <input type="checkbox" name="interests" value="看电影" checked="checked"/>看电影 </div> <div class="clear"></div> <div><input type="submit" value="提交表单"/></div> </form> 

 

 

2.简单对象类型

与基本类型相拟,只不过绑定到对象上更加简洁.(类拟struts的ActionForm)

controller代码

[java]
  1. @RequestMapping  
  2.     public void test2(User user)  
  3.     {  
  4.         System.out.println("简单对象类型绑定=========");  
  5.         System.out.println("名字:" + user.getName());  
  6.         System.out.println("年龄:" + user.getAge());  
  7.         System.out.println("收入:" + user.getIncome());  
  8.         System.out.println("已结婚:" + user.getIsMarried());  
  9.         System.out.println("========================");  
  10.     }  

@RequestMapping public void test2(User user) { System.out.println("简单对象类型绑定========="); System.out.println("名字:" + user.getName()); System.out.println("年龄:" + user.getAge()); System.out.println("收入:" + user.getIncome()); System.out.println("已结婚:" + user.getIsMarried()); System.out.println("========================"); } 

 

user代码

[java]
  1. public class User {  
  2.     private String name;  
  3.     private Integer age;  
  4.     private Boolean isMarried;  
  5.     private Double income;  
  6.     private String[] interests;  
  7.       
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public Integer getAge() {  
  15.         return age;  
  16.     }  
  17.     public void setAge(Integer age) {  
  18.         this.age = age;  
  19.     }  
  20.     public Boolean getIsMarried() {  
  21.         return isMarried;  
  22.     }  
  23.     public void setIsMarried(Boolean isMarried) {  
  24.         this.isMarried = isMarried;  
  25.     }  
  26.     public Double getIncome() {  
  27.         return income;  
  28.     }  
  29.     public void setIncome(Double income) {  
  30.         this.income = income;  
  31.     }  
  32.     public String[] getInterests() {  
  33.         return interests;  
  34.     }  
  35.     public void setInterests(String[] interests) {  
  36.         this.interests = interests;  
  37.     }  
  38. }  

public class User { private String name; private Integer age; private Boolean isMarried; private Double income; private String[] interests; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getIsMarried() { return isMarried; } public void setIsMarried(Boolean isMarried) { this.isMarried = isMarried; } public Double getIncome() { return income; } public void setIncome(Double income) { this.income = income; } public String[] getInterests() { return interests; } public void setInterests(String[] interests) { this.interests = interests; } }  

 

jsp代码

[xhtml]
  1. <form action="test2.do" method="post">  
  2.         <div class="f_left">姓名:</div>  
  3.         <div class="f_left"><input name="name" value="lxf"/></div>  
  4.         <div class="clear"></div>  
  5.         <div class="f_left">年龄:</div>  
  6.         <div class="f_left"><input name="age" value="20"/></div>  
  7.         <div class="clear"></div>  
  8.         <div class="f_left">收入:</div>  
  9.         <div class="f_left"><input name="income" value="100000"/></div>  
  10.         <div class="clear"></div>  
  11.         <div class="f_left">结婚:</div>  
  12.         <div class="f_left">  
  13.         <input type="radio" name="isMarried" value="true" checked="checked"/>是  
  14.         <input type="radio" name="isMarried" value="false"/></div>  
  15.         <div class="clear"></div>  
  16.         <div class="f_left">兴趣:</div>  
  17.         <div class="f_left">  
  18.         <input type="checkbox" name="interests" value="听歌" checked="checked"/>听歌  
  19.         <input type="checkbox" name="interests" value="书法" checked="checked"/>书法  
  20.         <input type="checkbox" name="interests" value="看电影" checked="checked"/>看电影  
  21.         </div>  
  22.         <div class="clear"></div>  
  23.         <div><input type="submit" value="提交表单"/></div>  
  24.     </form>  

<form action="test2.do" method="post"> <div class="f_left">姓名:</div> <div class="f_left"><input name="name" value="lxf"/></div> <div class="clear"></div> <div class="f_left">年龄:</div> <div class="f_left"><input name="age" value="20"/></div> <div class="clear"></div> <div class="f_left">收入:</div> <div class="f_left"><input name="income" value="100000"/></div> <div class="clear"></div> <div class="f_left">结婚:</div> <div class="f_left"> <input type="radio" name="isMarried" value="true" checked="checked"/>是 <input type="radio" name="isMarried" value="false"/>否</div> <div class="clear"></div> <div class="f_left">兴趣:</div> <div class="f_left"> <input type="checkbox" name="interests" value="听歌" checked="checked"/>听歌 <input type="checkbox" name="interests" value="书法" checked="checked"/>书法 <input type="checkbox" name="interests" value="看电影" checked="checked"/>看电影 </div> <div class="clear"></div> <div><input type="submit" value="提交表单"/></div> </form> 

 

 

3.List类型绑定

网上很多人都给不出答案,关键在于,List需要绑定在对象(ActionForm),直接写在request-mapping函数的参数是不行的,更重要的一点是要创建对象(ArrayList).

controller代码

[java]
  1. @RequestMapping  
  2.     public void test3(Club club)  
  3.     {  
  4.         System.out.println("list类型绑定=========");  
  5.         System.out.println("会员:");  
  6.         for (User user : club.getUsers())  
  7.         {  
  8.             System.out.println(user.getName());  
  9.         }  
  10.         System.out.println("========================");  
  11.     }  

@RequestMapping public void test3(Club club) { System.out.println("list类型绑定========="); System.out.println("会员:"); for (User user : club.getUsers()) { System.out.println(user.getName()); } System.out.println("========================"); } 

 

Club代码

[java]
  1. public class Club {  
  2.     private List<User> users = new ArrayList<User>();  
  3.       
  4.     public List<User> getUsers() {  
  5.         return users;  
  6.     }  
  7.       
  8.     public void setUsers(List<User> users) {  
  9.         this.users = users;  
  10.     }  
  11. }  

public class Club { private List<User> users = new ArrayList<User>(); public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } } 

 

jsp代码

[xhtml]
  1. <form action="test3.do" method="post">  
  2.         <div class="f_left">会员1号:</div>  
  3.         <div class="f_left">  
  4.         <input name="users[0].name" value="张三"/>  
  5.         </div>  
  6.         <div class="clear"></div>  
  7.         <div class="f_left">会员2号:</div>  
  8.         <div class="f_left">  
  9.         <input name="users[1].name" value="李四"/>  
  10.         </div>  
  11.         <div class="clear"></div>  
  12.         <div class="f_left">会员3号:</div>  
  13.         <div class="f_left">  
  14.         <input name="users[2].name" value="王五"/>  
  15.         </div>  
  16.         <div class="clear"></div>  
  17.         <div><input type="submit" value="提交表单"/></div>  
  18.     </form>  

<form action="test3.do" method="post"> <div class="f_left">会员1号:</div> <div class="f_left"> <input name="users[0].name" value="张三"/> </div> <div class="clear"></div> <div class="f_left">会员2号:</div> <div class="f_left"> <input name="users[1].name" value="李四"/> </div> <div class="clear"></div> <div class="f_left">会员3号:</div> <div class="f_left"> <input name="users[2].name" value="王五"/> </div> <div class="clear"></div> <div><input type="submit" value="提交表单"/></div> </form> 

 

 

4.Set类型绑定

与List类型相似,经测试表明,绑定Set数据时,必须先在Set对象中Add相应的数量的模型对象.

controller代码

[java]
  1. @RequestMapping  
  2.     public void test4(Club2 club)  
  3.     {  
  4.         System.out.println("set类型绑定=========");  
  5.         System.out.println("会员:");  
  6.         for (User user : club.getUsers())  
  7.         {  
  8.             System.out.println(user.getName());  
  9.         }  
  10.         System.out.println("========================");  
  11.     }  

@RequestMapping public void test4(Club2 club) { System.out.println("set类型绑定========="); System.out.println("会员:"); for (User user : club.getUsers()) { System.out.println(user.getName()); } System.out.println("========================"); }

Club2代码

 

 

[java]
  1. public class Club2 {  
  2.     private Set<User> users = new LinkedHashSet<User>();  
  3.       
  4.     public Club2() {  
  5.         users.add(new User());  
  6.         users.add(new User());  
  7.         users.add(new User());  
  8.     }  
  9.       
  10.     public Set<User> getUsers() {  
  11.         return users;  
  12.     }  
  13.     public void setUsers(Set<User> users) {  
  14.         this.users = users;  
  15.     }  
  16. }  

public class Club2 { private Set<User> users = new LinkedHashSet<User>(); public Club2() { 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; } } 

 

 

jsp代码

[xhtml]
  1. <form action="test4.do" method="post">  
  2.         <div class="f_left">会员1号:</div>  
  3.         <div class="f_left">  
  4.         <input name="users[0].name" value="张三"/>  
  5.         </div>  
  6.         <div class="clear"></div>  
  7.         <div class="f_left">会员2号:</div>  
  8.         <div class="f_left">  
  9.         <input name="users[1].name" value="李四"/>  
  10.         </div>  
  11.         <div class="clear"></div>  
  12.         <div class="f_left">会员3号:</div>  
  13.         <div class="f_left">  
  14.         <input name="users[2].name" value="王五"/>  
  15.         </div>  
  16.         <div class="clear"></div>  
  17.         <div><input type="submit" value="提交表单"/></div>  
  18.     </form>  

<form action="test4.do" method="post"> <div class="f_left">会员1号:</div> <div class="f_left"> <input name="users[0].name" value="张三"/> </div> <div class="clear"></div> <div class="f_left">会员2号:</div> <div class="f_left"> <input name="users[1].name" value="李四"/> </div> <div class="clear"></div> <div class="f_left">会员3号:</div> <div class="f_left"> <input name="users[2].name" value="王五"/> </div> <div class="clear"></div> <div><input type="submit" value="提交表单"/></div> </form> 

 

 

5.Map类型绑定

最灵活的一种方式,可无限绑定未定义的参数,注意必须绑定在对象下(ActionForm).

controller代码

[java]
  1. @RequestMapping  
  2.     public void test5(UserForm userForm)  
  3.     {  
  4.         System.out.println("Map类型绑定=========");  
  5.         System.out.println("名字:" + userForm.getUser().get("name"));  
  6.         System.out.println("年龄:" + userForm.getUser().get("age"));  
  7.         System.out.println("收入:" + userForm.getUser().get("income"));  
  8.         System.out.println("已结婚:" + userForm.getUser().get("isMarried"));  
  9.         System.out.println("========================");  
  10.     }  

@RequestMapping public void test5(UserForm userForm) { System.out.println("Map类型绑定========="); System.out.println("名字:" + userForm.getUser().get("name")); System.out.println("年龄:" + userForm.getUser().get("age")); System.out.println("收入:" + userForm.getUser().get("income")); System.out.println("已结婚:" + userForm.getUser().get("isMarried")); System.out.println("========================"); } 

 

UserForm.java代码

[java]
  1. public class UserForm {  
  2.     private Map<String, String> userMap = new LinkedHashMap<String, String>();  
  3.     private User user = new User();  
  4.       
  5.     public Map<String, String> getUserMap() {  
  6.         return userMap;  
  7.     }  
  8.     public void setUserMap(Map<String, String> userMap) {  
  9.         this.userMap = userMap;  
  10.     }  
  11.     public User getUser() {  
  12.         return user;  
  13.     }  
  14.     public void setUser(User user) {  
  15.         this.user = user;  
  16.     }  
  17.       
  18. }  

public class UserForm { private Map<String, String> userMap = new LinkedHashMap<String, String>(); private User user = new User(); public Map<String, String> getUserMap() { return userMap; } public void setUserMap(Map<String, String> userMap) { this.userMap = userMap; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } } 

 

jsp代码

[xhtml]
  1. <form action="test5.do" method="post">  
  2.         <div class="f_left">姓名:</div>  
  3.         <div class="f_left"><input name="user['name']" value="张三"/></div>  
  4.         <div class="clear"></div>  
  5.         <div class="f_left">年龄:</div>  
  6.         <div class="f_left"><input name="user['age']" value="20"/></div>  
  7.         <div class="clear"></div>  
  8.         <div class="f_left">收入:</div>  
  9.         <div class="f_left"><input name="user['income']" value="100000"/></div>  
  10.         <div class="clear"></div>  
  11.         <div class="f_left">结婚:</div>  
  12.         <div class="f_left">  
  13.         <input type="radio" name="user['isMarried']" value="true" checked="checked"/>是  
  14.         <input type="radio" name="user['isMarried']" value="false"/></div>  
  15.         <div class="clear"></div>  
  16.         <div><input type="submit" value="提交表单"/></div>  
  17.     </form>  

<form action="test5.do" method="post"> <div class="f_left">姓名:</div> <div class="f_left"><input name="user['name']" value="张三"/></div> <div class="clear"></div> <div class="f_left">年龄:</div> <div class="f_left"><input name="user['age']" value="20"/></div> <div class="clear"></div> <div class="f_left">收入:</div> <div class="f_left"><input name="user['income']" value="100000"/></div> <div class="clear"></div> <div class="f_left">结婚:</div> <div class="f_left"> <input type="radio" name="user['isMarried']" value="true" checked="checked"/>是 <input type="radio" name="user['isMarried']" value="false"/>否</div> <div class="clear"></div> <div><input type="submit" value="提交表单"/></div> </form> 

 

 

6.复合类型绑定

controller代码

[java]
  1. @RequestMapping  
  2.     public void test6(UserForm userForm)  
  3.     {  
  4.         System.out.println("复合类型绑定=========");  
  5.         System.out.println("名字:" + userForm.getUser().getName());  
  6.         System.out.println("年龄:" + userForm.getUser().getAge());  
  7.         System.out.println("收入:" + userForm.getUser().getIncome());  
  8.         System.out.println("已结婚:" + userForm.getUser().getIsMarried());  
  9.         System.out.println("========================");  
  10.     }  

@RequestMapping public void test6(UserForm userForm) { System.out.println("复合类型绑定========="); System.out.println("名字:" + userForm.getUser().getName()); System.out.println("年龄:" + userForm.getUser().getAge()); System.out.println("收入:" + userForm.getUser().getIncome()); System.out.println("已结婚:" + userForm.getUser().getIsMarried()); System.out.println("========================"); } 

 

UserForm代码

[java]
  1. public class UserForm {  
  2.     private Map<String, String> userMap = new LinkedHashMap<String, String>();  
  3.     private User user = new User();  
  4.       
  5.     public Map<String, String> getUserMap() {  
  6.         return userMap;  
  7.     }  
  8.     public void setUserMap(Map<String, String> userMap) {  
  9.         this.userMap = userMap;  
  10.     }  
  11.     public User getUser() {  
  12.         return user;  
  13.     }  
  14.     public void setUser(User user) {  
  15.         this.user = user;  
  16.     }  
  17.       
  18. }  

public class UserForm { private Map<String, String> userMap = new LinkedHashMap<String, String>(); private User user = new User(); public Map<String, String> getUserMap() { return userMap; } public void setUserMap(Map<String, String> userMap) { this.userMap = userMap; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } } 

 

jsp代码

[xhtml]
  1. <form action="test6.do" method="post">  
  2.     <div class="f_left">姓名:</div>  
  3.     <div class="f_left"><input name="user.name" value="张三"/></div>  
  4.     <div class="clear"></div>  
  5.     <div class="f_left">年龄:</div>  
  6.     <div class="f_left"><input name="user.age" value="20"/></div>  
  7.     <div class="clear"></div>  
  8.     <div class="f_left">收入:</div>  
  9.     <div class="f_left"><input name="user.income" value="100000"/></div>  
  10.     <div class="clear"></div>  
  11.     <div class="f_left">结婚:</div>  
  12.     <div class="f_left">  
  13.     <input type="radio" name="user.isMarried" value="true" checked="checked"/>是  
  14.     <input type="radio" name="user.isMarried" value="false"/></div>  
  15.     <div class="clear"></div>  
  16.     <div><input type="submit" value="提交表单"/></div>  
  17. </form>  

<form action="test6.do" method="post"> <div class="f_left">姓名:</div> <div class="f_left"><input name="user.name" value="张三"/></div> <div class="clear"></div> <div class="f_left">年龄:</div> <div class="f_left"><input name="user.age" value="20"/></div> <div class="clear"></div> <div class="f_left">收入:</div> <div class="f_left"><input name="user.income" value="100000"/></div> <div class="clear"></div> <div class="f_left">结婚:</div> <div class="f_left"> <input type="radio" name="user.isMarried" value="true" checked="checked"/>是 <input type="radio" name="user.isMarried" value="false"/>否</div> <div class="clear"></div> <div><input type="submit" value="提交表单"/></div> </form> 

 转自

转载于:https://www.cnblogs.com/xd502djj/p/3245485.html

你可能感兴趣的文章
2013-07-04学习笔记二
查看>>
CP15 协处理器寄存器解读
查看>>
【codeforces 787B】Not Afraid
查看>>
【9111】高精度除法(高精度除高精度)
查看>>
【hihocoder 1312】搜索三·启发式搜索(普通广搜做法)
查看>>
JavaFX中ObservableValue类型
查看>>
杭电 1097 A hard puzzle
查看>>
[转载]INFORMIX锁机制及如何剖析其锁申辩(第二部门)
查看>>
Andriod-项目stymqjlb-学习笔记2-原型
查看>>
Web AppDomain
查看>>
JQuery创建规范插件
查看>>
AD 域服务简介(三)- Java 对 AD 域用户的增删改查操作
查看>>
Unity中Text渐变色,和Text间距
查看>>
P4932 浏览器
查看>>
Concurrency Kit 0.2.13 发布,并发工具包
查看>>
SQL Relay 0.50 发布,数据库负载均衡器
查看>>
Infinispan 5.3.0.Alpha1 发布
查看>>
设计模式学习笔记——原型模式(Prototype)
查看>>
算法普林斯顿
查看>>
Struts2之类范围拦截器和方法拦截器
查看>>