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

接下来编写封装类:

1.对拦截器的封装:

package com.wbh.mymvc.servlet;

import javax.servlet.http.HttpServletRequest;

import com.wbh.mymvc.interceptor.BaseInterceptor;

/**
 * 对拦截器进行封装
 * @author wbh
 */
public class Obstruct {

	private BaseInterceptor interceptor;
	private String[] mappingPath;
	private String interceptorMethod;
	private Integer index;
	

	public Obstruct(BaseInterceptor interceptor, String[] mappingPath,
			String interceptorMethod, Integer index) {
		super();
		this.interceptor = interceptor;
		this.mappingPath = mappingPath;
		this.interceptorMethod = interceptorMethod;
		this.index = index;
	}

	//自己写的路径匹配没有进行细致测试
	public boolean pathMatch(HttpServletRequest req){
		
		String appPath = req.getContextPath();
		String url = req.getRequestURI().replaceFirst(appPath+"/", "").trim();
		String[] urlPiece = url.split("/");
		boolean flag = false;
		for(String s:mappingPath){
			if(s.contains("!:")&&(s.replace("!:", "").trim().equals(url))){
			}else{
				String[] strPiece = (s.replaceFirst("/", "")).split("/");
				if(strPiece[0].equals("**")){
					flag = true;
				}else if(urlPiece.length == strPiece.length){
					boolean f = true;
					for(int i=0;i<urlPiece.length;i++){
						if(strPiece[i].equals("*")||strPiece[i].equals(urlPiece[i])){
						}else{
							f=false;
						}
					}
					flag = f;
				}
			}
		}
		return flag;
	}   //get/set .......
2.对requestMapping 方法的封装:
package com.wbh.mymvc.servlet;

import java.lang.reflect.Method;


public class Handler {
	
	private Object controller;
	private Method mappingMethod;
	private String requestMethod;
	
	public Handler(Object controller,Method mappingMethod,String requestMethod) {
		this.controller = controller;
		this.mappingMethod = mappingMethod;
		this.requestMethod = requestMethod;
	}
      //get/set......
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值