对前端传来的参数,绑定至已有对象中
通过表单post数据,首先先在web.xml中加入filter,防止中文乱码
<filter>
<filter-name>encoding</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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1. 传入基本数据类型
Integer、Boolean、String 等简单数据类型
先创建hello.jsp,传入基本数据类型
<body>
<form action="/commit" method="post">
<table>
<tr>
<td>名字</td>
<td><input type="text" name="name" value="" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="" /></td>
</tr>
<tr>
<td>性别
<input type="radio" name="sex" value="true">男</input>
<input type="radio" name="sex" value="false">女</input>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
Controller中接收
@Controller
public class MyController {
@GetMapping("/user")
public Object sayHello(){
return "hello";
}
@PostMapping("/commit")
@ResponseBody
public Object format(String name,Integer age,Boolean sex){
JSONObject jsonObject=new JSONObject();
jsonObject.put("姓名",name);
jsonObject.put("年龄",age);
jsonObject.put("性别",sex?"男":"女");
return jsonObject;
}
}
测试结果
2. 传入实体类
创建Student
类,还是用hello.jsp传入,只要名字对应相等,就可以传入
@Data
public class Person {
String name;
Integer age;
Boolean sex;
}
Controller类
@Controller
public class MyController {
@GetMapping("/user")
public Object sayHello(){
return "hello";
}
@PostMapping("/commit")
@ResponseBody
public void format(Person person){
System.out.println(person);
}
}
输出:Person(name=admin, age=23, sex=false)
如果实体类中还有其他类,如:
@Data
public class Person {
String name;
Integer age;
Boolean sex;
Money money;
}
//其中Money类:
@Data
public class Money {
String name;
Integer num;
}
只要在jsp中添加为money.xx
即可
<body>
<form action="/commit" method="post">
<table>
<tr>
<td>名字</td>
<td><input type="text" name="name" value="" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="" /></td>
</tr>
<tr>
<td>性别
<input type="radio" name="sex" value="true">男</input>
<input type="radio" name="sex" value="false">女</input>
</td>
</tr>
<%--Money类--%>
<tr>
<td>货币类型</td>
<td><input type="text" name="money.name" value="" /></td>
</tr>
<tr>
<td>余额</td>
<td><input type="text" name="money.num" value="" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
3. 自定义参数类型转换
对Integer、String、Double等类,Spring MVC自身的HttpMessageConverter
就能进行转换,但如果想把String类转换成Person类,如传入字符串“Lihua-22-false”,转换成Person类型,则要定制自己的转换器
定制转换器,首先要实现Converter<S,T>
接口,S表示源类型,T为目标类型
@Component
public class StringToPerson implements Converter<String,Person> {
@Override
public Person convert(String source) {
String[] str=source.split("-");
String name=str[0];
Integer age=Integer.parseInt(str[1]);
Boolean sex=Boolean.parseBoolean(str[2]);
return new Person(name,age,sex);
}
}
在mvc配置文件springmvc-servlet.xml
中注册转换器
<mvc:annotation-driven conversion-service="conversionService"/>
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
<property name="converters">
<set>
<ref bean="stringToPerson"/>
</set>
</property>
</bean>
Controller如下:
@Controller
public class MyController {
@GetMapping("/person")
@ResponseBody
public void getPerson(Person person){
System.out.println(person);
}
}
浏览器请求http://localhost:8080/person?person=Lihua-22-false
,输出:
Person(name=Lihua, age=22, sex=false)
,转换成功
**注意:**如果用SpringBoot,可以省去注册步骤,会自动把实现了Converter
接口的Bean注册到ConverterService
对象中
4. 传入数组
如果前端传入多选数据:
<form action="/commit" method="post">
<table>
<tr>
<td>名字</td>
<td><input type="text" name="name" value="" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="" /></td>
</tr>
<tr>
<td>性别
<input type="radio" name="sex" value="true">男</input>
<input type="radio" name="sex" value="false">女</input>
</td>
</tr>
<tr>
<td>兴趣爱好:</td>
<td>
<input type="checkbox" name="favorites" value="足球">足球
<input type="checkbox" name="favorites" value="篮球">篮球
<input type="checkbox" name="favorites" value="乒乓球">乒乓球
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
可以用String数组接收,注意不能使用List接收
Controller类:
@Controller
public class MyController {
@GetMapping("/user")
public Object sayHello(){
return "hello";
}
@PostMapping("/commit")
@ResponseBody
public void format(Person person,String[] favorites){
System.out.println(person);
System.out.println("兴趣爱好:");
for (String favorite : favorites) {
System.out.println(favorite);
}
}
}
输出:
Person(name=admin, age=23, sex=true)
兴趣爱好:
足球
篮球