package com.itcast.domain; import java.io.Serializable; import java.util.List; import java.util.Map; public class Account implements Serializable{ private String username; private String password; private Double money; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", money=" + money + '}'; } }
import java.io.Serializable; public class User implements Serializable{ private String username; private String password; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } }
Account类
package com.itcast.domain; import java.io.Serializable; import java.util.List; import java.util.Map; public class Account implements Serializable{ private String username; private String password; private Double money; private List<User> list; private Map<String,User> map; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } @Override public String toString() { return "Account{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", money=" + money + ", list=" + list + ", map=" + map + '}'; } }
JSP代码
<!--把数据封装Account类中,类中存在list和map的集合--> <form action="<%=path%>/user/saveAccount" method="post"> 姓名:<input type="text" name="username" /><br/> 密码:<input type="text" name="password" /><br/> 金额:<input type="text" name="money" /><br/> 用户姓名:<input type="text" name="list[0].username" /><br/> 用户年龄:<input type="text" name="list[0].age" /><br/> 用户姓名:<input type="text" name="map['one'].username" /><br/> 用户年龄:<input type="text" name="map['one'].age" /><br/> <input type="submit" value="提交" />
控制类
@RequestMapping(path = "/saveAccount") public String saveAccount(Account account){ System.out.println("保存了账户......"+account); return "success"; }
import org.springframework.core.convert.converter.Converter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * 把字符串转换日期 */ public class StringToDateConverter implements Converter<String,Date>{ /** * String source 传入进来字符串 * @param source * @return */ public Date convert(String source) { // 判断 if(source == null){ throw new RuntimeException("请您传入数据"); } DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { // 把字符串转换日期 return df.parse(source); } catch (Exception e) { throw new RuntimeException("数据类型转换出现错误"); } } }
<!--配置类型转化器工厂--> <bean id="converterService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <!--给工厂注入一个新的类型转化器--> <property name="converters"> <array> <!--配置自定义类型转化器--> <bean class="com.itcast.utils.StringToDateConverter"></bean> </array> </property> </bean>
<!--引用自定义类型转换器--> <mvc:annotation-driven conversion-service="converterService"> </mvc:annotation-driven>
最后结果图: