SpringMVC笔记02 请求参数的绑定
coconutnut

https://www.bilibili.com/video/BV1Sb411s7qa

P11~16

简单参数

1
<a href="param/testParam?username=hehe&password=123">
1
2
3
4
@RequestMapping("/testParam")
public String testParam(String username,String password){
// ...
}

绑定实体类型

实体类

1
2
3
4
5
6
7
8
9
public class Account implements Serializable{

private String username;
private String password;
private Double money;

// get set ...

}

表单

1
2
3
4
5
6
<form action="param/saveAccount" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="text" name="password" /><br/>
金额:<input type="text" name="money" /><br/>
<input type="submit" value="提交" />
</form>

绑定

1
2
3
4
5
6
7
8
9
10
/**
* 请求参数绑定把数据封装到JavaBean的类中
* @return
*/
@RequestMapping("/saveAccount")
public String saveAccount(Account account){
System.out.println("执行了...");
System.out.println(account);
return "success";
}

引用类型

实体类

1
2
3
4
5
6
7
8
public class User implements Serializable{

private String uname;
private Integer age;

// get set ...

}
1
2
3
4
5
6
7
8
9
public class Account implements Serializable{

private String username;
private String password;
private Double money;

private User user;

}

表单

1
2
3
4
5
6
7
8
<form action="param/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="user.uname" /><br/>
用户年龄:<input type="text" name="user.age" /><br/>
<input type="submit" value="提交" />
</form>

集合类型

实体类

1
2
3
4
5
6
7
8
9
10
public class Account implements Serializable{

private String username;
private String password;
private Double money;

private List<User> list;
private Map<String,User> map;

}

表单

1
2
3
4
5
6
7
8
9
10
11
12
<form action="param/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].uname" /><br/>
用户年龄:<input type="text" name="list[0].age" /><br/>

用户姓名:<input type="text" name="map['one'].uname" /><br/>
用户年龄:<input type="text" name="map['one'].age" /><br/>
<input type="submit" value="提交" />
</form>

map中放key值,这里是’one’

自定义类型转换器

页面提交的数据都是字符串类型

基本类型可以自动转,如Integer

但日期类型可能不支持某些格式,需要自定义类型转换器

例如,给User加一个Date属性

1
2
3
4
5
6
7
public class User implements Serializable{

private String uname;
private Integer age;
private Date date;

}
1
2
3
4
5
6
<form action="param/saveUser" method="post">
用户姓名:<input type="text" name="uname" /><br/>
用户年龄:<input type="text" name="age" /><br/>
用户生日:<input type="text" name="date" /><br/>
<input type="submit" value="提交" />
</form>
1
2
3
4
5
6
@RequestMapping("/saveUser")
public String saveUser(User user){
System.out.println("执行了...");
System.out.println(user);
return "success";
}

在生日框中输入”2000/11/11”发现可以正常封装

但是”2000-11-11”出现Bad Request

需要写一个类型转换类并注册

需要实现Converter接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class StringToDateConverter implements Converter<String,Date>{

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("数据类型转换出现错误");
}
}

}

然后在springmvc.xml中配置

1
2
3
4
5
6
7
8
9
10
11
<!--配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.itcast.utils.StringToDateConverter"/>
</set>
</property>
</bean>

<!-- 开启SpringMVC框架注解的支持 -->
<mvc:annotation-driven conversion-service="conversionService"/>

解决中文乱码

在web.xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--配置解决中文乱码的过滤器-->
<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>

获取Servlet原生的API

直接在方法上加参数即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RequestMapping("/testServlet")
public String testServlet(HttpServletRequest request, HttpServletResponse response){
System.out.println("执行了...");
System.out.println(request);

HttpSession session = request.getSession();
System.out.println(session);

ServletContext servletContext = session.getServletContext();
System.out.println(servletContext);

System.out.println(response);
return "success";
}