写出一个你自己的MVC框架-基于对springMVC源码实现和理解(3):数据初始化(二)

前文已经对SpringMVC中DispatcherServlet数据初始化过程有了一定的认识,下面开始编码:

1.设计自定义注解@MyController:

  1. package com.wbh.mymvc.annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. @Target(ElementType.TYPE)  
  11. @Retention(RetentionPolicy.RUNTIME)  
  12. @Documented  
  13. @Inherited  
  14. public @interface MyController {  
  15. }  
2.设计自定义注解@MyInterceptor:
  1. package com.wbh.mymvc.annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. @Target(ElementType.TYPE)  
  11. @Retention(RetentionPolicy.RUNTIME)  
  12. @Documented  
  13. @Inherited  
  14. public @interface MyInterceptor {  
  15.       
  16.       
  17.     /** 
  18.      * 默认全匹配,匹配规则如下 
  19.      * "/*"项目根目录下匹配,不包含子目录 
  20.      * "/**"项目下全匹配,包括所有子目录下的请求 
  21.      * "/Example/*"即Example下的请求匹配,不包含子目录,以此类推 
  22.      * "!:"后跟的目录为要排除的目录 
  23.      * @return 
  24.      */  
  25.     String[] mappingPath() default {"/**"};  
  26.       
  27.     String interceptionMethod() default "both";//默认拦截GET,POST方法  
  28.       
  29.     int index() ;  
  30.       
  31. }  
3.设计自定义注解@MyRequestMapping:
  1. package com.wbh.mymvc.annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10.   
  11. @Target(ElementType.METHOD)  
  12. @Retention(RetentionPolicy.RUNTIME)  
  13. @Documented  
  14. @Inherited  
  15. public @interface MyRequestMapping {  
  16.       
  17.     String value() default "/";  
  18.     String method() default "GET"//默认请求方式为GET  
  19.       
  20. }  
4.设计拦截器接口BaseInterceptor:
  1. package com.wbh.mymvc.interceptor;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import com.wbh.mymvc.ui.MyModelAndView;  
  7.   
  8. /** 
  9.  * 基本的拦截器接口 
  10.  * @author wbh 
  11.  *  
  12.  */  
  13. public interface BaseInterceptor {  
  14.       
  15.     /** 
  16.      * 先于获得handler 
  17.      * @param request 
  18.      * @param response 
  19.      * @param handler 
  20.      * @return 
  21.      * @throws Exception 
  22.      */  
  23.     boolean beforeHandler(HttpServletRequest request, HttpServletResponse response)  
  24.             throws Exception;  
  25.       
  26.     /** 
  27.      * handler执行后执行,先于view加载 
  28.      * @param request 
  29.      * @param response 
  30.      * @param handler 
  31.      * @throws Exception 
  32.      */  
  33.     void afterHandler(HttpServletRequest request, HttpServletResponse response, MyModelAndView modelAndeView)  
  34.             throws Exception;  
  35.       
  36.     /** 
  37.      * view加载后执行 
  38.      * @param request 
  39.      * @param response 
  40.      * @param handler 
  41.      * @throws Exception 
  42.      */  
  43.     void afterViewLoad(HttpServletRequest request, HttpServletResponse response)  
  44.             throws Exception;  
  45.       
  46. }  
5.设计拦截器适配器 :
  1. package com.wbh.mymvc.interceptor;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import com.wbh.mymvc.ui.MyModelAndView;  
  7.   
  8. /** 
  9.  * 拦截器适配器 
  10.  * @author wbh 
  11.  * 
  12.  */  
  13. public abstract class InterceptorAdapt implements BaseInterceptor {  
  14.       
  15.     @Override  
  16.     public boolean beforeHandler(HttpServletRequest request,  
  17.             HttpServletResponse response) throws Exception {  
  18.         return true;  //默认允许  
  19.     }  
  20.       
  21.     @Override  
  22.     public void afterHandler(HttpServletRequest request,  
  23.             HttpServletResponse response, MyModelAndView modelAndView) throws Exception {  
  24.     }  
  25.       
  26.     @Override  
  27.     public void afterViewLoad(HttpServletRequest request,  
  28.             HttpServletResponse response) throws Exception {  
  29.     }  
  30.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值