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

接下来编写封装类:

1.对拦截器的封装:

  1. package com.wbh.mymvc.servlet;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import com.wbh.mymvc.interceptor.BaseInterceptor;  
  6.   
  7. /** 
  8.  * 对拦截器进行封装 
  9.  * @author wbh 
  10.  */  
  11. public class Obstruct {  
  12.   
  13.     private BaseInterceptor interceptor;  
  14.     private String[] mappingPath;  
  15.     private String interceptorMethod;  
  16.     private Integer index;  
  17.       
  18.   
  19.     public Obstruct(BaseInterceptor interceptor, String[] mappingPath,  
  20.             String interceptorMethod, Integer index) {  
  21.         super();  
  22.         this.interceptor = interceptor;  
  23.         this.mappingPath = mappingPath;  
  24.         this.interceptorMethod = interceptorMethod;  
  25.         this.index = index;  
  26.     }  
  27.   
  28.     //自己写的路径匹配没有进行细致测试  
  29.     public boolean pathMatch(HttpServletRequest req){  
  30.           
  31.         String appPath = req.getContextPath();  
  32.         String url = req.getRequestURI().replaceFirst(appPath+"/""").trim();  
  33.         String[] urlPiece = url.split("/");  
  34.         boolean flag = false;  
  35.         for(String s:mappingPath){  
  36.             if(s.contains("!:")&&(s.replace("!:""").trim().equals(url))){  
  37.             }else{  
  38.                 String[] strPiece = (s.replaceFirst("/""")).split("/");  
  39.                 if(strPiece[0].equals("**")){  
  40.                     flag = true;  
  41.                 }else if(urlPiece.length == strPiece.length){  
  42.                     boolean f = true;  
  43.                     for(int i=0;i<urlPiece.length;i++){  
  44.                         if(strPiece[i].equals("*")||strPiece[i].equals(urlPiece[i])){  
  45.                         }else{  
  46.                             f=false;  
  47.                         }  
  48.                     }  
  49.                     flag = f;  
  50.                 }  
  51.             }  
  52.         }  
  53.         return flag;  
  54.     }   //get/set .......  
2.对requestMapping 方法的封装:
  1. package com.wbh.mymvc.servlet;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5.   
  6. public class Handler {  
  7.       
  8.     private Object controller;  
  9.     private Method mappingMethod;  
  10.     private String requestMethod;  
  11.       
  12.     public Handler(Object controller,Method mappingMethod,String requestMethod) {  
  13.         this.controller = controller;  
  14.         this.mappingMethod = mappingMethod;  
  15.         this.requestMethod = requestMethod;  
  16.     }  
  17.       //get/set......  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值