所有的开发之中拦截器一定是一个必须要使用的功能,利用拦截器可以更加有效的实现数据的验证处理,而且最为幸运的是在SpringBoot之中所使用的拦截器与Spring中的拦截器完全一样。
一、基础拦截器操作
package com.gwolf.controller;
import com.gwolf.util.controller.AbstractBaseController;
import com.gwolf.vo.Member;
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.bind.annotation.ResponseBody;
@Controller
public class MemberController extends AbstractBaseController{
@RequestMapping(value="/member_add_pre", method = RequestMethod.GET)
public String memberAddPre() {
return "member_add";
}
@RequestMapping(value="/member_add", method = RequestMethod.POST)
@ResponseBody
public Object memberAdd(Member member) {
return member;
}
}
二:定义一个member_add.html的页面实现表单定义:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模板渲染</title>
<link rel="icon" type="image/x-icon"
href="/images/baidu.ico" />
<meta http-equiv="content-type"
content="text/html;charset=UTF-8" />
</head>
<body>
<form th:action="@{/member_add}" method="post">
用户编号:<input type="text" name="mid" value="101">
用户姓名:<input type="text" name="name" value="SMITH">
<input type="submit" value="表单提交">
</form>
</body>
</html>
三、此时一个正常的MVC的代码就实现完成了,随后需要编写一个拦截器对其进行控制。为了更好的说明问题,现在将拦截器定义在外包中:package com.gwolf.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor{
private Logger log = LoggerFactory.getLogger(MyInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)handler;
this.log.info("[****MyInterceptor.preHandle****" + handlerMethod.getBean().getClass().getSimpleName());
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)handler;
this.log.info("[****MyInterceptor.postHandle****" + handlerMethod.getBean().getClass().getSimpleName());
this.log.info("[****MyInterceptor.postHandle****" + modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)handler;
this.log.info("[****MyInterceptor.afterCompletion****拦截结束" );
}
}
四、那么此时如果要想使用拦截器则必须有一个拦截器的配置类。现在不再编写配置文件了,所有的配置直接利用一个类完成。package com.gwolf.config;
import com.gwolf.util.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//定义mvc配置
@Configuration
public class MyWebApplicationConfig extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}