接下来编写封装类:
2.对requestMapping 方法的封装:
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 .......
- 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......
- }