Spring MVC拦截器的使用和数据效验
什么是拦截器:
Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。在SpringMVC中通过实现HandlerInterceptor接口实现自定义拦截器类。
拦截器的执行流程
自定义拦截器类如下
public class MyIntercept implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("========拦截器当中的preHandle()方法========"); 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("=======呈现之后调用======="); } }
编写控制器
Controller @RequestMapping("/my") public class MyController { @RequestMapping("doFirst") public String doFirst(){ System.out.println("======控制器的执行方法======="); return "index"; }
拦截器的配置
输出
<!--配置拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/my/**"/> <bean class="com.springmvc.Interceptor.MyIntercept"/> </mvc:interceptor> </mvc:interceptors>
输出
Spring MVC的数据校验
架包
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency>
jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/my/validate" method="post"/> 编号:<input type="text" name="user_id"/> <br/> 姓名:<input type="text" name="user_name"/> <br/> 邮箱:<input type="text" name="user_email"/> <br/> 手机号:<input type="text" name="user_phone"/> <br/> 地址:<input type="text" name="user_address"/> <br/> 身份证号:<input type="text" name="user_cardCode"/> <br/> <input type="submit" value="提交"/> </form> </body>
实体类
public class UserInfo { @NotNull(message = "编号不能为空") private Integer user_id; @NotNull @Length(min = 2,max = 8,message = "用户名不能少于2位大于8位") private String user_name; @Email(message = "邮箱格式不正确") private String user_email; @Pattern(regexp = "",message = "手机号格式不正确") private String user_phone; private String user_address; @Pattern(regexp = "",message = "身份证号不正确") private String user_cardCode; public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_email() { return user_email; } public void setUser_email(String user_email) { this.user_email = user_email; } public String getUser_phone() { return user_phone; } public void setUser_phone(String user_phone) { this.user_phone = user_phone; } public String getUser_address() { return user_address; } public void setUser_address(String user_address) { this.user_address = user_address; } public String getUser_cardCode() { return user_cardCode; } public void setUser_cardCode(String user_cardCode) { this.user_cardCode = user_cardCode; } }
编写控制器
@RequestMapping("/validate") public String validate(@Valid UserInfo info, BindingResult bindingResult){ if (bindingResult.hasErrors()){ List<ObjectError> errors=bindingResult.getAllErrors(); for (ObjectError error:errors){ System.out.println(error.getDefaultMessage()); } } System.out.println(info.getUser_email()); return "index"; }