1. JFinal类 初始化ActionMapping java
private void initActionMapping() { actionMapping = new ActionMapping(Config.getRoutes(), Config.getInterceptors());//将配置路径映射、拦截器,传给ActionMapping类 actionMapping.buildActionMapping();//创建ActionMapping映射 }
2. ActionMapping类 数组
目的:获得请求URL与请求执行者Action的映射关系,保存到map中app
Action中保存有拦截器,执行请求所须要的Controller类及方法,返回页面的目录等信息ui
Interceptor[]url
从配置里获得global级别拦截器spa
从controller的注释声明(@before)获得controller级别拦截器code
依靠反射的方法遍历controller的方法,从方法的注释声明(@before)获得method级别拦截器继承
URL:ActionKey+Methodkeyget
从配置里获得ActionKeyit
从method上的注释声明获得ActionKey,没有声明的默认为method名
new 一个Action保存interceptor[],controller,method等信息
将URL,Action添加到map里保存,获得映射关系 map.add(URL,Action)
void buildActionMapping() { mapping.clear(); Set<String> excludedMethodName = buildExcludedMethodName();//获得Controller类(父类)里的无参方法 InterceptorBuilder interceptorBuilder = new InterceptorBuilder();//拦截器建立类(保存拦截器) Interceptor[] defaultInters = interceptors.getInterceptorArray();//在config类里咱们配置的全局拦截器 interceptorBuilder.addToInterceptorsMap(defaultInters);//把全局拦截器添加到map里保存 for (Entry<String, Class<? extends Controller>> entry : routes.getEntrySet()) {//遍历config类里咱们配置的请求-Controller映射 add("/", ManagerController.class); Class<? extends Controller> controllerClass = entry.getValue();//获得控制器Controller类 Interceptor[] controllerInters = interceptorBuilder.buildControllerInterceptors(controllerClass);//获得控制器上的拦截器 Method[] methods = controllerClass.getMethods();//获得控制器的全部方法 for (Method method : methods) { String methodName = method.getName(); if (!excludedMethodName.contains(methodName) && method.getParameterTypes().length == 0) {//咱们写的控制器都继承了Controller父类,同时添加了本身的无参方法,这里的目的就是获得咱们本身添加的方法(controller的全部无参方法除去父类里的无参方法,就是咱们本身添加的方法了)。 Interceptor[] methodInters = interceptorBuilder.buildMethodInterceptors(method);//获得method上的拦截器 Interceptor[] actionInters = interceptorBuilder.buildActionInterceptors(defaultInters, controllerInters, controllerClass, methodInters, method);//将拦截器按照优先级(global>controller>method)依次保存到actionInters String controllerKey = entry.getKey(); ActionKey ak = method.getAnnotation(ActionKey.class);//获得该方法上添加的ActionKey声明(即请求路径) if (ak != null) {//有声明则ActionKey为声明的值,若是没有声明则ActionKey默认为该方法名,默认index的ActionKey为“/”值,将请求URL(controllerKey+ActionKey)作为key,Action做为value保存到mapping中。Action中保存有拦截器,执行请求所须要的Controller类及方法,返回页面的目录等信息 String actionKey = ak.value().trim(); if ("".equals(actionKey)) throw new IllegalArgumentException(controllerClass.getName() + "." + methodName + "(): The argument of ActionKey can not be blank."); if (!actionKey.startsWith(SLASH)) actionKey = SLASH + actionKey; if (mapping.containsKey(actionKey)) { warnning(actionKey, controllerClass, method); continue; } Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey)); mapping.put(actionKey, action); } else if (methodName.equals("index")) { String actionKey = controllerKey; Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey)); action = mapping.put(actionKey, action); if (action != null) { warnning(action.getActionKey(), action.getControllerClass(), action.getMethod()); } } else { String actionKey = controllerKey.equals(SLASH) ? SLASH + methodName : controllerKey + SLASH + methodName; if (mapping.containsKey(actionKey)) { warnning(actionKey, controllerClass, method); continue; } Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey)); mapping.put(actionKey, action); } } } } // support url = controllerKey + urlParas with "/" of controllerKey Action actoin = mapping.get("/"); if (actoin != null) mapping.put("", actoin); }
private Set<String> buildExcludedMethodName() {//返回Controller类里的无参方法 Set<String> excludedMethodName = new HashSet<String>(); Method[] methods = Controller.class.getMethods(); for (Method m : methods) { if (m.getParameterTypes().length == 0)//getParameterTypes获取该方法的参数数组,这里是获得controller的无参方法 excludedMethodName.add(m.getName()); } return excludedMethodName; }
3. InterceptorBuilder 拦截器建立类
@SuppressWarnings("unchecked") void addToInterceptorsMap(Interceptor[] defaultInters) { for (Interceptor inter : defaultInters) intersMap.put((Class<Interceptor>)inter.getClass(), inter); }
/** * Build interceptors of Action * 将拦截器按照优先级(global>controller>method)依次保存到actionInters */ Interceptor[] buildActionInterceptors(Interceptor[] defaultInters, Interceptor[] controllerInters, Class<? extends Controller> controllerClass, Interceptor[] methodInters, Method method) { ClearLayer controllerClearType = getControllerClearType(controllerClass); if (controllerClearType != null) { defaultInters = NULL_INTERCEPTOR_ARRAY; } ClearLayer methodClearType = getMethodClearType(method); if (methodClearType != null) { controllerInters = NULL_INTERCEPTOR_ARRAY; if (methodClearType == ClearLayer.ALL) { defaultInters = NULL_INTERCEPTOR_ARRAY; } } int size = defaultInters.length + controllerInters.length + methodInters.length; Interceptor[] result = (size == 0 ? NULL_INTERCEPTOR_ARRAY : new Interceptor[size]); int index = 0; for (int i=0; i<defaultInters.length; i++) { result[index++] = defaultInters[i]; } for (int i=0; i<controllerInters.length; i++) { result[index++] = controllerInters[i]; } for (int i=0; i<methodInters.length; i++) { result[index++] = methodInters[i]; } return result; }