public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; request.setCharacterEncoding(encoding); String target = request.getRequestURI(); if (contextPathLength != 0) target = target.substring(contextPathLength); boolean[] isHandled = {false}; try { handler.handle(target, request, response, isHandled); } catch (Exception e) { if (log.isErrorEnabled()) { String qs = request.getQueryString(); log.error(qs == null ? target : target + "?" + qs, e); } } if (isHandled[0] == false) chain.doFilter(request, response); }2.String target = request.getRequestURI();
public final void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) { if (target.indexOf(".") != -1) { return ; } isHandled[0] = true; String[] urlPara = {null}; Action action = actionMapping.getAction(target, urlPara); if (action == null) { if (log.isWarnEnabled()) { String qs = request.getQueryString(); log.warn("Action not found: " + (qs == null ? target : target + "?" + qs)); } renderFactory.getErrorRender(404).setContext(request, response).render(); return ; } try { Controller controller = action.getControllerClass().newInstance(); controller.init(request, response, urlPara[0]); if (devMode) { boolean isMultipartRequest = ActionReporter.reportCommonRequest(controller, action); new ActionInvocation(action, controller).invoke(); if (isMultipartRequest) ActionReporter.reportMultipartRequest(controller, action); } else { new ActionInvocation(action, controller).invoke(); } Render render = controller.getRender(); if (render instanceof ActionRender) { String actionUrl = ((ActionRender)render).getActionUrl(); if (target.equals(actionUrl)) throw new RuntimeException("The forward action url is the same as before."); else handle(actionUrl, request, response, isHandled); return ; } if (render == null) render = renderFactory.getDefaultRender(action.getViewPath() + action.getMethodName()); render.setContext(request, response, action.getViewPath()).render(); } catch (RenderException e) { if (log.isErrorEnabled()) { String qs = request.getQueryString(); log.error(qs == null ? target : target + "?" + qs, e); } } catch (ActionException e) { int errorCode = e.getErrorCode(); if (errorCode == 404) { if (log.isWarnEnabled()) { String qs = request.getQueryString(); log.warn("404 Not Found: " + (qs == null ? target : target + "?" + qs)); } e.getErrorRender().setContext(request, response).render(); } else if (errorCode == 401) { if (log.isWarnEnabled()) { String qs = request.getQueryString(); log.warn("401 Unauthorized: " + (qs == null ? target : target + "?" + qs)); } e.getErrorRender().setContext(request, response).render(); } else if (errorCode == 403) { if (log.isWarnEnabled()) { String qs = request.getQueryString(); log.warn("403 Forbidden: " + (qs == null ? target : target + "?" + qs)); } e.getErrorRender().setContext(request, response).render(); } else { if (log.isErrorEnabled()) { String qs = request.getQueryString(); log.error(qs == null ? target : target + "?" + qs, e); } e.getErrorRender().setContext(request, response).render(); } } catch (Exception e) { if (log.isErrorEnabled()) { String qs = request.getQueryString(); log.error(qs == null ? target : target + "?" + qs, e); } renderFactory.getErrorRender(500).setContext(request, response).render(); } }5.if (target.indexOf(".") != -1) {
}
这几句代码的作用主要就是过滤css,img之类的,让filter不要对它起作用
6.Action action = actionMapping.getAction(target, urlPara);
/** * Support four types of url * 1: http://abc.com/controllerKey ---> 00 * 2: http://abc.com/controllerKey/para ---> 01 * 3: http://abc.com/controllerKey/method ---> 10 * 4: http://abc.com/controllerKey/method/para ---> 11 */ Action getAction(String url, String[] urlPara) { Action action = mapping.get(url); if (action != null) { return action; } // -------- int i = url.lastIndexOf(SLASH); if (i != -1) { action = mapping.get(url.substring(0, i)); urlPara[0] = url.substring(i + 1); } return action; }7. actionMapping我前面已经介绍过啦,在我们的例子里面的值如下:
如果是save方法: controllerKey = /blog ,actionKey = /blog/save,controllerClass = BlogController.class,methodName = save, 同理如果是index()方法: controllerKey=/blog,actionKey=/blog,controllerClass=BlogController.class,methodName =index 同理如果是add()方法: controllerKey=/blog,actionKey=/blog/add,controllerClass=BlogController.class,methodName=add 同理如果是edit()方法: controllerKey=/blog,actionKey=/blog/edit,controllerClass= BlogController.class,methodName=edit 同理如果是update()方法: controllerKey=/blog,actionKey=/blog/update,controllerClass= BlogController.class,methodName=update 同理如果是delete()方法: controllerKey=/blog,actionKey=/blog/delete,controllerClass= BlogController.class,methodName=delete 6. String controllerKey =entry.getKey();还有在我们中的例子为CommonController中的/ 只有1个index方法 controllcontrollerKey=/,actionKey=/,controllerClass=CommonController.class,methodName =index7.我们的入口是actionKey='/'
void init(HttpServletRequest request, HttpServletResponse response, String urlPara) { this.request = request; this.response = response; this.urlPara = urlPara; }8. ActionReporter.reportCommonRequest(controller, action) 这个就是打印控制台信息的
JFinal action report -------- 2013-06-21 15:13:48 ------------------------------ Controller : com.demo.common.CommonController.(CommonController.java:1) Method : index Interceptor : com.demo.blog.BlogInterceptor1.(BlogInterceptor1.java:1) com.demo.blog.BlogInterceptor2.(BlogInterceptor2.java:1) --------------------------------------------------------------------------------9. 这句话是 new ActionInvocation(action, controller).invoke();
ActionInvocation(Action action, Controller controller) { this.controller = controller; this.inters = action.getInterceptors(); this.action = action; }10.下面的代码就是上面代码的 核心
/** * Invoke the action. */ public void invoke() { if (index < inters.length) inters[index++].intercept(this); else if (index++ == inters.length) // index++ ensure invoke action only one time // try {action.getMethod().invoke(controller, NULL_ARGS);} catch (Exception e) {throw new RuntimeException(e);} try { action.getMethod().invoke(controller, NULL_ARGS); } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); if (cause instanceof RuntimeException) throw (RuntimeException)cause; throw new RuntimeException(e); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
Before invoking BlogInterceptor1/ Before invoking BlogInterceptor2/ 执行CommonController里面index方法 After invoking BlogInterceptor2/ After invoking BlogInterceptor1/11.Render render = controller.getRender();
public void render() { response.setContentType(contentType); Enumeration<String> attrs = request.getAttributeNames(); Map root = new HashMap(); while (attrs.hasMoreElements()) { String attrName = attrs.nextElement(); root.put(attrName, request.getAttribute(attrName)); } PrintWriter writer = null; try { Template template = config.getTemplate(view); writer = response.getWriter(); template.process(root, writer); // Merge the data-model and the template } catch (Exception e) { throw new RenderException(e); } finally { if (writer != null) writer.close(); } }一个流程完全结束下面,下一个阶段我会介绍数据库方面的啦,最后我会扩展权限功能.....