參考:https://www.jianshu.com/p/ca4c6ee1fc12web
流程:app
1.async
初始化的時候,把業務上的controller遍歷,利用反射把controller類的信息封裝爲Action類,ui
並構建了 k-targer,v-action的mappingxml
2.get
運行時,根據web.xml的配置,依次找處處理類信息處理鏈的鏈首節點 ActionHandler,servlet
3.it
在 ActionHandler 中io
3.1: Action action = actionMapping.getAction(target)class
3.2: new Invocation(...).invoke()
3.3: render(...)
TODO:尚未弄清http request 過來,第一個處理請求是哪一個類?
分析:
在web.xml中配置
<filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>configClass</param-name> <param-value>net.dreamlu.common.WebConfig</param-value> </init-param> </filter>
JFinalFilter中的init()方法中,
handler = jfinal.getHandler(); // 開始接受請求
Jfinal的init(JFinalConfig jfinalConfig, ServletContext servletContext)方法中,
調用了 initHandler();(實際裏這裏還有另外一個重要步驟 initActionMapping())
private void initHandler() {
ActionHandler actionHandler = Config.getHandlers().getActionHandler();
if (actionHandler == null) {
actionHandler = new ActionHandler();
}
actionHandler.init(actionMapping, constants);
handler = HandlerFactory.getHandler(Config.getHandlers().getHandlerList(), actionHandler);
}
最後一句調用 HandlerFactory.getHandler,
/**
* Build handler chain
*/
@SuppressWarnings("deprecation")
public static Handler getHandler(List<Handler> handlerList, Handler actionHandler) {
Handler result = actionHandler;
for (int i=handlerList.size()-1; i>=0; i--) {
Handler temp = handlerList.get(i);
temp.next = result;
temp.nextHandler = result;
result = temp;
}
return result;
}
在這個方法裏,返回值是Handler,並非List<Handler>。利用Handler的next屬性,實行了鏈表結構。返回值的Handler實際是一個HandlerChain的鏈首節點ActionHandler。
在action的handle方法中,處理最開始的httpServletRequest
/**
* handle
* 1: Action action = actionMapping.getAction(target)
* 2: new Invocation(...).invoke()
* 3: render(...)
*/
public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled)
在這個handle方法裏,註釋說明了流程
1: Action action = actionMapping.getAction(target)
2: new Invocation(...).invoke()
3: render(...)
回到前面,actionMapping 的產生。
( actionHandler.init(actionMapping, constants);的參數actionMapping)
private void initActionMapping() {
actionMapping = new ActionMapping(Config.getRoutes());
actionMapping.buildActionMapping();
Config.getRoutes().clear();
}
在buildActionMapping()裏作了一系列反射相關的而操做 ,構建主要是構建了Action這個類,而後把key-value 放到了mapping裏。
Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, route.getFinalViewPath(routes.getBaseViewPath()));
if (mapping.put(actionKey, action) != null) {
throw new RuntimeException(buildMsg(actionKey, controllerClass, method));
}