如下全部源碼只截取了部分代碼,標題即爲類名app
<filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class>
if (jfinal.init(jfinalConfig, filterConfig.getServletContext()) == false)
boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) { Config.configJFinal(jfinalConfig); // start plugin and init log factory in this method }
static void configJFinal(JFinalConfig jfinalConfig) { jfinalConfig.configConstant(constants); initLogFactory(); jfinalConfig.configRoute(routes); jfinalConfig.configPlugin(plugins); startPlugins(); // very important!!! jfinalConfig.configInterceptor(interceptors); jfinalConfig.configHandler(handlers); }
加載jfinalConfig配置文件this
public void configRoute(Routes me) { //增長自定義route me.add(new ApiRoute());
public Routes add(Routes routes) { if (routes != null) { routes.config(); // very important!!! for (Entry<String, Class<? extends Controller>> e : routes.map.entrySet()) { String controllerKey = e.getKey(); if (this.map.containsKey(controllerKey)) { throw new IllegalArgumentException("The controllerKey already exists: " + controllerKey); } this.map.put(controllerKey, e.getValue()); this.viewPathMap.put(controllerKey, routes.getViewPath(controllerKey)); } } return this; }
加載完jfinalConfig回到JFinalFilterurl
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try { handler.handle(target, request, response, isHandled); }
// handler.handle(target, request, response, isHandled);是整個Filter最核心的方法xml
這裏的handlre來自JFinalFilter.init方法中52行handler=jfinal.getHandler();blog
private void initHandler() { Handler actionHandler = new ActionHandler(actionMapping, constants); handler = HandlerFactory.getHandler(Config.getHandlers().getHandlerList(), actionHandler); }
handler是由HandlerFactory的getHandler方法得來的,此處使用handler子類ActionHandler,而且傳進去了有兩個參數,一個是ActionMapping類的變量,一個是constants。路由
ActionMapping在ActionMapping中定義了一個路由(routes)和一個Interceptors,這個routes類裏面主要的核心是兩個Map,主要處理處理一些關於ActionMapping中對應的ControllerKey與Controller.class的事情。看下ActionHandlerget
1)、根據ActionMapping得到相應的Action,源碼
Action action = actionMapping.getAction(target, urlPara);
2)、而後利用反射進行方法的調用,最後把結果映射到相應的頁面上去servlet
new Invocation(action, controller).invoke();