前文已经对SpringMVC中DispatcherServlet数据初始化过程有了一定的认识,下面开始编码:
2.设计自定义注解@MyInterceptor:
3.设计自定义注解@MyRequestMapping:
4.设计拦截器接口BaseInterceptor:
5.设计拦截器适配器 :
1.设计自定义注解@MyController:
- package com.wbh.mymvc.annotation;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- public @interface MyController {
- }
- package com.wbh.mymvc.annotation;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- public @interface MyInterceptor {
- /**
- * 默认全匹配,匹配规则如下
- * "/*"项目根目录下匹配,不包含子目录
- * "/**"项目下全匹配,包括所有子目录下的请求
- * "/Example/*"即Example下的请求匹配,不包含子目录,以此类推
- * "!:"后跟的目录为要排除的目录
- * @return
- */
- String[] mappingPath() default {"/**"};
- String interceptionMethod() default "both";//默认拦截GET,POST方法
- int index() ;
- }
- package com.wbh.mymvc.annotation;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- public @interface MyRequestMapping {
- String value() default "/";
- String method() default "GET"; //默认请求方式为GET
- }
- package com.wbh.mymvc.interceptor;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.wbh.mymvc.ui.MyModelAndView;
- /**
- * 基本的拦截器接口
- * @author wbh
- *
- */
- public interface BaseInterceptor {
- /**
- * 先于获得handler
- * @param request
- * @param response
- * @param handler
- * @return
- * @throws Exception
- */
- boolean beforeHandler(HttpServletRequest request, HttpServletResponse response)
- throws Exception;
- /**
- * handler执行后执行,先于view加载
- * @param request
- * @param response
- * @param handler
- * @throws Exception
- */
- void afterHandler(HttpServletRequest request, HttpServletResponse response, MyModelAndView modelAndeView)
- throws Exception;
- /**
- * view加载后执行
- * @param request
- * @param response
- * @param handler
- * @throws Exception
- */
- void afterViewLoad(HttpServletRequest request, HttpServletResponse response)
- throws Exception;
- }
- package com.wbh.mymvc.interceptor;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.wbh.mymvc.ui.MyModelAndView;
- /**
- * 拦截器适配器
- * @author wbh
- *
- */
- public abstract class InterceptorAdapt implements BaseInterceptor {
- @Override
- public boolean beforeHandler(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- return true; //默认允许
- }
- @Override
- public void afterHandler(HttpServletRequest request,
- HttpServletResponse response, MyModelAndView modelAndView) throws Exception {
- }
- @Override
- public void afterViewLoad(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- }
- }