只需要在方法中的参数写上变量名与参数名相同的形参,即可自动获取。
底层实现:通过反射获取形参的名字,再如servlet那样通过request获取参数。
基本数据类型和字符串类型
实体类型(JavaBean)
集合数据类型(List、map集合等)
Account类(其中包含User类)
package com.czy.domain; import java.io.Serializable; public class Account implements Serializable { private String username; private String password; private Double money; private User user; public User getUser() { return user; } @Override public String toString() { return "Account{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", money=" + money + ", user=" + user + '}'; } public void setUser(User user) { this.user = user; } 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; } }
User类
package com.czy.domain; import java.io.Serializable; public class User implements Serializable { private String uname; private Integer age; @Override public String toString() { return "User{" + "uname='" + uname + '\'' + ", age=" + age + '}'; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
表单(注意user的项目)
<form method="post" action="param/saveAccount"> <label for="username">姓名:</label> <input id="username" name="username"><br> <label for="password">密码:</label> <input type="password" name="password" id="password"><br> <label for="money">金额:</label> <input type="text" id="money" name="money"><br> <label for="uname">用户姓名:</label> <input id="uname" name="user.uname"><br> <!--user.uname封装进user中--> <label for="age">用户年龄:</label> <!--同上--> <input id="age" name="user.age"><br> <input type="submit" value="提交"> </form>
Controller接收自动封装
@RequestMapping("/saveAccount") public String saveAccount(Account account){ System.out.println(account); return "success"; }
Account类
package com.czy.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> users; private Map<String,User> map; public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } @Override public String toString() { return "Account{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", money=" + money + ", users=" + users + ", map=" + 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; } }
前台表单
<form method="post" action="param/saveAccount"> <label for="username">姓名:</label> <input id="username" name="username"><br> <label for="password">密码:</label> <input type="password" name="password" id="password"><br> <label for="money">金额:</label> <input type="text" id="money" name="money"><br> <label for="uname1">用户姓名:</label> <input id="uname1" name="users[0].uname"><br> <label for="age1">用户年龄:</label> <input id="age1" name="users[0].age"><br> <label for="uname2">用户姓名:</label> <input id="uname2" name="map['one'].uname"><br> <label for="age2">用户年龄:</label> <input id="age2" name="map['one'].age"><br> <input type="submit" value="提交"> </form>
web.xml中配置过滤器
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
我们从前台的数据提交上来的都是String,MVC通过内置的类型转换器帮我们转换成对应的类型,存入JavaBean/参数中。但是有时某些形式的数据,会导致存储失败,比如说:日期形式2020/2/2可以存储,而2020-2-2无法存储或前台表单type=date也无法存储。
故我们需要自定义类型转换器解决该问题
package com.czy.utils; import org.springframework.core.convert.converter.Converter; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 把字符串转换为日期 */ public class StringToDateConverter implements Converter<String, Date> { @Override public Date convert(String s) { if(s == null){ throw new RuntimeException("传参为空"); } DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { return format.parse(s); } catch (ParseException exception) { throw new RuntimeException("数据类型转换错误"); } } }
在springmvc.xml配置文件中配置
<!--开启SpringMVC框架注解的支持--> <mvc:annotation-driven conversion-service="factoryBean"></mvc:annotation-driven> <!--配置自定义类型转换器--> <bean id="factoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.czy.utils.StringToDateConverter"></bean> </set> </property> </bean>