1、什么是拦截器
Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。在SpringMVC中通过实现HandlerInterceptor接口实现自定义拦截器类。
2、 拦截器案例
(1)MyIntercept
package com.springmvc.intercept; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyIntercept implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("=========拦截器当中的handle方法========="); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("=======在HandlerAdapter之后执行,在DispatcherServlet解析视图之前执行====="); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("=========呈现之后调用========="); } }
(2)拦截器配置spring-mvc.xml
<!--利用default-servlet放行资源--> <mvc:default-servlet-handler/> <!--配置拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/my/**"/> <bean class="com.springmvc.intercept.MyIntercept"></bean> </mvc:interceptor> </mvc:interceptors>
(3)MyController
@Controller @RequestMapping("/my") public class MyController { @RequestMapping("/doFirst") public String doFirst(){ System.out.println("=========控制器中的方法========="); return "index"; }
(4)控制台效果
3、Spring MVC的数据校验
3.1 数据校验案例
(1)导入依赖
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency>
(2)validate.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>测试案例</title> </head> <body> <form action="/my/validate" method="post"> 编号:<input type="text" name="uid"/><br/> 姓名:<input type="text" name="uName"/><br/> 邮箱:<input type="text" name="email"/><br/> 电话:<input type="text" name="phone"/><br/> 身份证号:<input type="text" name="userCode"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
(3)实体类UserInfo
package com.springmvc.entity; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; public class UserInfo { @NotNull(message="编号不能为空") private Integer uid; @NotNull @Length(min = 2,max = 8,message = "用户名不能少于2位或大于8位") private String uName; @Email(message = "邮箱格式不正确") private String email; @Pattern(regexp = "/^1[3456789]\\d{9}$/",message = "手机号式不正确") private String phone; @Pattern(regexp = "/(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)/",message = "身份证号格式不正确") private String userCode; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getuName() { return uName; } public void setuName(String uName) { this.uName = uName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } }
(4)MyController
package com.springmvc.controller; import com.springmvc.entity.UserInfo; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.RequestMapping; import javax.validation.Valid; import java.util.List; @Controller @RequestMapping("/my") public class MyController { @RequestMapping("/doFirst") public String doFirst(){ System.out.println("=========控制器中的方法========="); return "index"; } @RequestMapping("/validate") public String Validate(@Valid UserInfo info, BindingResult bindingResult){ //如果有异常信息 if (bindingResult.hasErrors()){ //获取异常信息对象 List<ObjectError> allErrors = bindingResult.getAllErrors(); //将异常信息输出 for (ObjectError errors:allErrors) { System.out.println(errors.getDefaultMessage()); } } return "index"; } }
(5)效果展示