#SpringMVC
SpringMVC请求
简单类型
简单类型包括:基本类型,基本类型的包装类型,字符串
编写Controller
@RequestMapping("/param")@Controllerpublic class SimpleParamController {/*简单类型*/@RequestMapping("/simpleParam")public String simplParam(String username,int age){System.out.println(username);System.out.println(age);return "success" ;}}
编写index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Title</title></head><body><a href="${pageContext.request.contextPath}/param/simpleParam?username=jack&age=15">简单类型</a></body></html>
编写success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Title</title></head><body>访问成功!</body></html>
对象类型
对象类型就是将提交的表单封装成一个对象
导入lombok依赖
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency>
编写表单
<html><head><title>Title</title></head><body><form action="${pageContext.request.contextPath}/param/objectParam">姓名:<input type="text" name="username" /> <br>年龄:<input type="text" name="age" ><input type="submit" value="提交"></form></body></html>
编写User类
@Data@NoArgsConstructor@AllArgsConstructorpublic class User {private String username;private Integer age;}
编写Controller
/*对象类型*/@RequestMapping("/objectParam")public String simplParam(User user){System.out.println(user.getUsername());System.out.println(user.getAge());return "success" ;}
表单中的name属性必须跟User类中的成员变量的名称一致,否则值将为null.
数组类型
当页面需要选择多个值的时候,我们需要用到数组来接收页面传递过来的参数。比如:批量删除,选择爱好等功能
编写表单
<form action="${pageContext.request.contextPath}/param/arrayParam"><input type="checkbox" name="ids" value="1"> 矢泽优步老湿 <br><input type="checkbox" name="ids" value="2"> 樱井莉亚老湿 <br><input type="checkbox" name="ids" value="3"> 吉泽明步老湿 <br><input type="checkbox" name="ids" value="4"> 泷泽萝拉老湿 <br><input type="checkbox" name="ids" value="5"> 波多野结衣老湿 <br><input type="submit" value="数组类型"></form>
编写Controller
@RequestMapping("/arrayParam")public String simplParam(Integer[] ids){System.out.println(Arrays.toString(ids));return "success" ;}
复选框的name属性名必须方法中参数的变量名一致,否则接收不成功!
集合类型
如果提交的是一个集合的数据,SpringMVC的方法形参是无法接收的,我们必须通过实体类进行包装才行
编写UserVo
@AllArgsConstructor@NoArgsConstructor@Datapublic class UserVO {private String username;private Integer age;private User user;private List<Object> list;private Map<String,Object> map;}
编写表单
<form action="${pageContext.request.contextPath}/param/listParam">姓名:<input type="text" name="username">年龄:<input type="text" name="age"> <br/>user属性中的姓名: <input type="text" name="user.username"> <br/>user属性中的年龄: <input type="text" name="user.age"> <br/>list中第一个元素:<input type="text" name="list[0]"> <br/>list中第二个元素: <input type="text" name="list[1]"> <br/>list中第三个元素: <input type="text" name="list[2]"> <br/>map中第一个键值对中的值:<input type="text" name="map['one']"><br/>map中第一个键值对中的值:<input type="text" name="map['two']"><br/><input type="submit" value="复杂类型"></form>
集合存储User对象
修改UserVo
@AllArgsConstructor@NoArgsConstructor@Datapublic class UserVO {private String username;private Integer age;private User user;private List<User> list;private Map<String,User> map;}
修改表单
<form action="${pageContext.request.contextPath}/param/listParam">姓名: <input type="text" name="username">年龄: <input type="text" name="age"> <br/>user属性中的姓名:<input type="text" name="user.username"><br/>user属性中的年龄:<input type="text" name="user.age"><br/>list中第一个user对象的年龄:<input type="text" name="list[0].age"> <br/>list中第一个user对象的姓名:<input type="text" name="list[0].username"> <br/>list中第二个user对象的年龄: <input type="text" name="list[1].age"> <br/>list中第二个user对象的姓名: <input type="text" name="list[1].username"> <br/>map中第一个键值对中值user对象的年龄: <input type="text" name="map['one'].age"> <br/>map中第一个键值对中值user对象的姓名: <input type="text" name="map['one'].username"> <br/>map中第二个键值对中值user对象的年龄: <input type="text" name="map['two'].age"> <br/>map中第二个键值对中值user对象的姓名: <input type="text" name="map['two'].username"><br/><input type="submit" value="复杂类型"></form>
对象中存储数组
修改UserVo
@AllArgsConstructor@NoArgsConstructor@Datapublic class UserVO {private String username;private Integer age;private User user;private List<User> list;private Map<String,User> map;private Integer[] ids;}
修改表单
<form action="${pageContext.request.contextPath}/param/listParam">姓名: <input type="text" name="username">年龄: <input type="text" name="age"> <br/>user属性中的姓名:<input type="text" name="user.username"><br/>user属性中的年龄:<input type="text" name="user.age"><br/>list中第一个user对象的年龄:<input type="text" name="list[0].age"> <br/>list中第一个user对象的姓名:<input type="text" name="list[0].username"> <br/>list中第二个user对象的年龄: <input type="text" name="list[1].age"> <br/>list中第二个user对象的姓名: <input type="text" name="list[1].username"> <br/>map中第一个键值对中值user对象的年龄: <input type="text" name="map['one'].age"> <br/>map中第一个键值对中值user对象的姓名: <input type="text" name="map['one'].username"> <br/>map中第二个键值对中值user对象的年龄: <input type="text" name="map['two'].age"> <br/>map中第二个键值对中值user对象的姓名: <input type="text" name="map['two'].username"><br/>请选择喜欢的老湿:<input type="checkbox" name="ids" value="1"> 矢泽优步老湿 <br><input type="checkbox" name="ids" value="2"> 樱井莉亚老湿 <br><input type="checkbox" name="ids" value="3"> 吉泽明步老湿 <br><input type="checkbox" name="ids" value="4"> 泷泽萝拉老湿 <br><input type="checkbox" name="ids" value="5"> 波多野结衣老湿 <br><input type="submit" value="复杂类型"></form>
解决中文乱码问题
如果是get请求,tomcat8以上版本的服务器统一了UTF-8编码,不会出现乱码的问题!
如果是post请求,由于servlet规范当中post默认编码是ISO-8859-1,就会出现中文乱码问题。
spring框架提供了post请求中文过滤器…
修改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>
自定义类型转换器
实际开发中,经常使用将日期转换成Date。我们就需要用到类型转换的问题。
在SpringMVC框架中默认将字符串转为日期对象了,格式为:yyyy/MM/dd.但是我们经常熟悉的格式是yyyy-MM-dd.
SpringMVC不支持yyyy-MM-dd的格式,要想使用这种格式,我们就需要自己定义一个类型转换器
演示错误案例
定义表单
<form action="${pageContext.request.contextPath}/param/dateParam">生日:<input type="text" name="birthday"><input type="submit" value="日期提交"></form>
编写Controller
@RequestMapping("/param")@Controllerpublic class SimpleParamController {@RequestMapping("/dateParam")public String listParam(Date birthday){SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");System.out.println(dateFormat.format(birthday));return "success" ;}}
测试
编写类型转换器
表单页面
<form action="${pageContext.request.contextPath}/param/dateParam">生日:<input type="text" name="birthday"><input type="submit" value="日期提交"></form>
编写Controller
@RequestMapping("/dateParam")public String dateParam( Date birthday){SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");System.out.println(dateFormat.format(birthday));return "success" ;}
定义类实现Converter接口
/*日期类型转换器*/public class DateConverter implements Converter<String, Date> {public Date convert(String dateStr) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");Date date = null;try {date = dateFormat.parse(dateStr);} catch (Exception e) {e.printStackTrace();}return date;}}
配置转换器
<!--开启mvc注解支持--><mvc:annotation-driven conversion-service="conversionService"/><!-- 扩展自定义的类型转换器 --><bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"><property name="converters"><set><bean class="cn.yunhe.convert.DateConverter"></bean></set></property></bean>
注解版
@DateTimeFormat() 注解来实现类型转换的功能,具体操作如下:
修改Controller
@RequestMapping("/dateParam")public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");System.out.println(dateFormat.format(birthday));return "success" ;}
修改spring-mvc配置文件
<!--开启扫描注解--><mvc:annotation-driven /><!--<mvc:annotation-driven conversion-service="conversionService" />--><context:component-scan base-package="com.lifly.controller"/>
SpringMVC相关注解
@RequestParam
@RequestParam:用于将指定的请求参数赋值给方法中的形参,可以接收普通参数,也可以接收对象!
经典使用场景:在数据查询时做分页
语法
@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)
@RequestParam属性
value
:请求参数名(必须配置)required
表示是否必须,默认为true
,请求该方法必须携带参数和值。defaultValue
可设置请求参数的默认值。
@RequestParam的使用
编写jsp
<a href="${pageContext.request.contextPath}/param/pageParam?pageNum=1&size=10">分页查询</a>
编写Controller
@RequestMapping("/pageParam")public String pageParam(@RequestParam(value = "pageNum",defaultValue = "1") Integer currentPage,@RequestParam(value = "size",defaultValue = "10")Integer pageSize){System.out.println(currentPage);System.out.println(pageSize);return "success" ;}
@RequestHeader
获取指定的请求头,相当于Servlet中的getHeader() 方法
使用步骤
编写jsp
<a href="${pageContext.request.contextPath}/param/requestHeader">请求头</a>
编写Controller
@RequestMapping("/requestHeader")public String requestHeader(@RequestHeader("User-Agent") String userAgent ,@RequestHeader("cookie") String cookie){System.out.println(userAgent);System.out.println(cookie);return "success" ;}
@CookieValue
获取指定的Cookie的值
使用步骤
编写jsp
<a href="${pageContext.request.contextPath}/param/cookieValue">获取指定的Cookie</a>
编写Controller
@RequestMapping("/cookieValue")public String cookieValue(@RequestHeader("User-Agent") String userAgent ,@RequestHeader("cookie") String cookie){System.out.println(userAgent);System.out.println(cookie);return "success" ;}