2、为了让SpringMVC中控制器的功能更加的专一,让控制器只专注自己的业务处理部分,而不用考虑用户的权限等一系列的因素,
使用在控制器中的方法上添加注解的方式来对添加注解的方法执行前做一些预处理操作或者后处理操作(比如,对用户验证,
以及验证成功后向response中写入cookie)。
1)自定义注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodFilter {
String operatorType() default "";
String paramNum() default "0";
}
2)将注解加入到要进行预处理或者后处理的方法上
@Controller
@RequestMapping("method")
public class MethodController {
@RequestMapping(value = "filter")
@MethodFilter
@JsonBody
public String testMethodFilter(){
System.out.println("testMethodFilter run");
return "success";
}
@RequestMapping(value = "nofilter")
@JsonBody
public String testMethodFilter1(){
System.out.println("testMethodFilter1 run");
return "success";
}
}
3)自定义拦截方法,并能过滤注解的拦截器类,该类继承 HandlerInterceptorAdapter,也可以实现HandlerInterceptor接口
public class MethodFilterHandlerInterceptorAdapter extends HandlerInterceptorAdapter{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
MethodFilter methodFilter = ((HandlerMethod)handler).getMethodAnnotation(MethodFilter.class);
if(methodFilter == null){
return true;
}else {
System.out.println("before MethodFilter1, and the operatorType="+methodFilter.operatorType()+
", the paramNum = "+methodFilter.paramNum()+" in the MethodFilter annotation");
return true;
}
} else{
System.out.println("before no MethodFilter");
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
}
4)在配置文件(×××-servlet.xml)中指定自定义的拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/method/*"/>
<bean class="qunar.MethodFilterHandlerInterceptorAdapter"/>
</mvc:interceptor>
</mvc:interceptors>
使用在控制器中的方法上添加注解的方式来对添加注解的方法执行前做一些预处理操作或者后处理操作(比如,对用户验证,
以及验证成功后向response中写入cookie)。
1)自定义注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodFilter {
String operatorType() default "";
String paramNum() default "0";
}
2)将注解加入到要进行预处理或者后处理的方法上
@Controller
@RequestMapping("method")
public class MethodController {
@RequestMapping(value = "filter")
@MethodFilter
@JsonBody
public String testMethodFilter(){
System.out.println("testMethodFilter run");
return "success";
}
@RequestMapping(value = "nofilter")
@JsonBody
public String testMethodFilter1(){
System.out.println("testMethodFilter1 run");
return "success";
}
}
3)自定义拦截方法,并能过滤注解的拦截器类,该类继承 HandlerInterceptorAdapter,也可以实现HandlerInterceptor接口
public class MethodFilterHandlerInterceptorAdapter extends HandlerInterceptorAdapter{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
MethodFilter methodFilter = ((HandlerMethod)handler).getMethodAnnotation(MethodFilter.class);
if(methodFilter == null){
return true;
}else {
System.out.println("before MethodFilter1, and the operatorType="+methodFilter.operatorType()+
", the paramNum = "+methodFilter.paramNum()+" in the MethodFilter annotation");
return true;
}
} else{
System.out.println("before no MethodFilter");
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
}
4)在配置文件(×××-servlet.xml)中指定自定义的拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/method/*"/>
<bean class="qunar.MethodFilterHandlerInterceptorAdapter"/>
</mvc:interceptor>
</mvc:interceptors>