SpringMVC:请求参数绑定
1.请求参数的绑定说明
1. 绑定机制
1. 表单提交的数据都是k=v格式的 username=haha&password=123
2. SpringMVC的参数绑定过程是把表单提交的请求参数,作为控制器中方法的参数进行绑定的
3. 要求:提交表单的name和参数的名称是相同的
2. 支持的数据类型
1. 基本数据类型和字符串类型
2. 实体类型(JavaBean)
3. 集合数据类型(List、map集合等)
2. 基本数据类型和字符串类型
1. 提交表单的name和参数的名称是相同的
2. 区分大小写
3. 实体类型(JavaBean)
1. 提交表单的name和JavaBean中的属性名称需要一致
2. 如果一个JavaBean类中包含其他的引用类型,那么表单的name属性需要编写成:对象.属性 例如:
address.name
4. 给集合属性数据封装1. JSP页面编写方式:list[0].属性
1. JSP页面编写方式:list[0].属性
使用示例:
一、基本类型和String类型作为参数
jsp文件
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304204702274-858351916.png)
controller代码
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304204720301-275269213.png)
结果展示:
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304204908779-1985857694.png)
二、POJO类型作为参数
设置一个Account的类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 + '}'; } }
JSP代码
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304205144839-1862983050.png)
Controller代码:
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304205323391-1451782727.png)
结果显示
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304205221614-319326601.png)
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304205254746-313335700.png)
三、POJO类中包含集合类型参数
User类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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"; }
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304214957290-2036471871.png)
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304215007818-1425345242.png)
特殊情况:自定义类型转换器
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304224331106-785757666.png)
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304224358124-1943529180.png)
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304224340522-797474983.png)
如上三个图所示,如果使用字符串的形式是没有错误的
如果改成如下的形式如出现这样的情况。
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304230814282-1226545732.png)
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304231044140-1850627052.png)
然后就要完成类型转化:
转化代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
最后结果图:
![](https://img2020.cnblogs.com/blog/1661171/202103/1661171-20210304234557015-1839530381.png)
一纸高中万里风,寒窗读破华堂空。
莫道长安花看尽,由来枝叶几相同?