spring mvc 做web开发时,经常会不知道如何合适绑定页面数据.用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便(本人最开始也是这么认为),经过一段时间的应用和测试,总结出以下几种情况,希望对刚接触spring mvc 的朋友有所帮助.
查看spring源码可以看出spring支持转换的数据类型已经很多了.
org.springframework.beans.PropertyEditorRegistrySupport.java
- /**
- * 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代码
- @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代码
- <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代码
- @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代码
- 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代码
- <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代码
- @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代码
- 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代码
- <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代码
- @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代码
- 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代码
- <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代码
- @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代码
- 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代码
- <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代码
- @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代码
- 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代码
- <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>