高级参数绑定
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Arrays; @Controller public class TestController { /** * 数组参数绑定 */ @RequestMapping("test") public void test(Integer[] ids) { System.out.println(Arrays.toString(ids)); /* [1, 2] */ } }
例:数组参数绑定
package com.zze.springmvc.pojo; import java.util.List; public class QueryVo { private List<User> users; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } } package com.zze.springmvc.web.controller; import com.zze.springmvc.pojo.QueryVo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Arrays; @Controller public class TestController { /** * 集合参数绑定 */ @RequestMapping("test") public void test(QueryVo queryVo) { System.out.println(Arrays.toString(queryVo.getUsers().toArray())); /* [User{id=1, name='zhangsan'}, User{id=2, name='lisi'}] */ } }
例:集合参数绑定
参数转换器
如果要进行日期类型参数绑定,SpringMVC 默认是不支持的,需要我们手动定义一个转换器来告知 SpringMVC 如何转换,如下:
package com.zze.springmvc.web.converter; import org.springframework.core.convert.converter.Converter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * String : 源类型 * Date : 目标类型 */ public class DateConverter implements Converter<String,Date> { public Date convert(String source) { Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(source); } catch (ParseException e) { e.printStackTrace(); } return date; } }
com.zze.springmvc.web.converter.DateConverter : 自定义的日期转换器
<!--自定义转换器工厂--> <bean id="myConverters" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <!--注册自己定义的日期转换器--> <bean class="com.zze.springmvc.web.converter.DateConverter"/> </property> </bean> <!--使用上自定义的转换器工厂--> <mvc:annotation-driven conversion-service="myConverters"/>
config/spring/springmvc.xml : 注册自定义转换器
相关注解
@RequestMapping
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { /** * 请求 /test1 和 /test2 都会指向 test 方法 */ @RequestMapping(value = {"test1", "test2"}) public void test() { } }
例:多路由
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("test") public class TestController { /** * 因为类上使用了 RequestMapping 注解,此时 test 方法的访问路径就为: * /test/test1 和 /test/test2 */ @RequestMapping(value = {"test1", "test2"}) public void test() { } }
例:配置在类上
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class TestController { /** * 限定请求方法只能为 GET 和 POST */ @RequestMapping(value = "test",method = {RequestMethod.POST,RequestMethod.GET}) public void test() { } }
例:限定请求方式
@RequestParam
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class TestController { /** * @RequestParam * name 指定所修饰参数对应请求参数名 * required 指定所修饰参数是否必须 * defaultValue 指定所修饰参数的默认值 */ @RequestMapping(value = "test") public void test(@RequestParam(name = "pid",required = false,defaultValue = "1") Integer id) { } }
例:自定义请求参数映射
请求方法的返回值
在 SpringMVC 中,请求方法的返回值除了 ModelAndView,还可以有如下几种方式:
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { /** * 转发到 jsp */ @RequestMapping(value = "test") public String test() { // 因为配置了视图解析器,这里的 userlist 实际指向 /WEB-INF/jsp/userlist.jsp return "userlist"; } }
返回 String,转发到 JSP
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { /** * 转发到 Action */ @RequestMapping(value = "test") public String test() { return "forward:/user/list"; } }
返回 String,转发到 Action
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { /** * 重定向到 Action */ @RequestMapping(value = "test") public String test() { return "redirect:/user/list"; } }
返回 String,重定向到 Action
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class TestController { /** * 访问原生 request 和 response API */ @RequestMapping(value = "test") public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain;charset=utf-8"); // request.getRequestDispatcher("/user/list").forward(request,response); 转发 // response.sendRedirect("/user/list"); // 重定向 response.getWriter().write("你好世界"); } }
无返回值,访问原生 Servlet API
请求乱码问题解决
<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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
WEB-INF/web.xml : 解决 POST 请求乱码
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
tomcat - server.xml : 解决 GET 请求乱码
全局异常处理
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>消息页</title> </head> <body> ${msg} </body> </html>
WEB-INF/jsp/msg.jsp : 1、创建一个消息通知页
package com.zze.springmvc.web.util; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("msg"); modelAndView.addObject("msg", "服务器异常!"); return modelAndView; } }
com.zze.springmvc.web.util.MyExceptionResolver : 2、编写自定义的异常处理器
<!--注册全局异常处理器--> <bean class="com.zze.springmvc.web.util.MyExceptionResolver"/>
config/spring/springmvc.xml : 3、直接注册到 Spring 容器中就可以生效
文件上传
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
pom.xml : 1、添加文件上传依赖
<!-- 配置多媒体处理器 注意:这里的 id 必须使用 multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--限制单次文件上传大小不超过 8m--> <property name="maxUploadSize" value="8388608"/> </bean>
config/spring/springmvc.xml : 2、配置多媒体处理器
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>图片上传</title> </head> <body> <form action="/fileupload" method="post" enctype="multipart/form-data"> <input type="file" name="picFile"> <input type="submit" value="上传"> </form> </body> </html>
WEB-INF/jsp/fileupload.jsp : 3、编写文件上传页面
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @Controller public class TestController { @RequestMapping(value = "fileupload", method = RequestMethod.GET) public String upload() { return "fileupload"; } @RequestMapping(value = "fileupload", method = RequestMethod.POST) public String upload(Model model, MultipartFile picFile) throws IOException { File file = new File("D:/upload/" + picFile.getOriginalFilename()); picFile.transferTo(file); model.addAttribute("msg", "上传成功"); return "fileupload"; } }
com.zze.springmvc.web.controller.TestController : 4、编写文件上传服务端代码
// 新建目录 D:/upload // 浏览器访问 localhost:8080/fileupload
5、测试:
JSON交互
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.2</version>
pom.xml : 加入 json 支持依赖
package com.zze.springmvc.web.controller; import com.zze.springmvc.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.text.ParseException; import java.text.SimpleDateFormat; @Controller public class TestController { /** * 返回 JSON 格式数据 * 返回值设为要序列化的类型,并在方法上添加上 @ResponseBody 注解 */ @RequestMapping("getUser") @ResponseBody public User getUser() throws ParseException { User user = new User(); user.setId(1); user.setName("张三"); user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1998-1-1")); user.setGender("男"); user.setAddress("河北"); return user; } }
返回 JSON 格式数据
package com.zze.springmvc.web.controller; import com.zze.springmvc.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { /** * 1、使用 @RequestBody 修饰入参 * 2、请求方式为 POST,content-type 为 application/json * 3、json 中字段名与 pojo 属性一致 */ @RequestMapping("addUser") @ResponseBody public User addUser(@RequestBody User user){ System.out.println(user); user.setGender("女"); return user; } }
接收 JSON 格式数据
要使用该方式进行 JSON 交互,必须配置注解驱动。
<!--配置注解驱动,相当于同时使用最新处理器映射器和处理器适配器,同时对 JSON 数据响应提供支持--> <mvc:annotation-driven/>
Restful风格
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @RequestMapping("user/{id}") @ResponseBody public void getUser(@PathVariable Integer id){ System.out.println(id); /* 请求 localhost:8080/user/1 控制台输出: 1 */ } }
com.zze.springmvc.web.controller.TestController
拦截器
SpringMVC 的处理器拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理。
package com.zze.springmvc.web.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor1 implements HandlerInterceptor { /** * 处理器执行前执行 */ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor.preHandle"); return true; // true:放行 false:拦截 } /** * 处理器执行后, modelAndView 返回前执行 */ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor.postHandle"); } /** * 处理器执行后执行 */ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor.afterCompletion"); } }
com.zze.springmvc.web.interceptor.MyInterceptor1 : 1、定义拦截器 1
package com.zze.springmvc.web.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor2 implements HandlerInterceptor { /** * 处理器执行前执行 */ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor.preHandle"); return true; // true:放行 false:拦截 } /** * 处理器执行后, modelAndView 返回前执行 */ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor.postHandle"); } /** * 处理器执行后执行 */ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor.afterCompletion"); } }
com.zze.springmvc.web.interceptor.MyInterceptor2 : 2、定义拦截器 2
<!--注册拦截器--> <mvc:interceptors> <mvc:interceptor> <!--拦截所有请求,包括二级以上目录--> <mvc:mapping path="/**"/> <!--指定路径不拦截--> <mvc:exclude-mapping path="/login"/> <bean class="com.zze.springmvc.web.interceptor.MyInterceptor1"/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.zze.springmvc.web.interceptor.MyInterceptor2"/> </mvc:interceptor> </mvc:interceptors>
config/spring/springmvc.xml : 3、注册拦截器
package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping("test") public void test() { System.out.println("from TestController.test"); } }
com.zze.springmvc.web.controller.TestController : 4、编写测试 Controller
5、测试,请求 localhost:8080/test ,控制台输出如下: