用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

JFinal核心类之ActionHandler

2015-02-23 作者: java源代码大全举报

[java]代码库

/**
 * ActionHandler
 */
final class ActionHandler extends Handler {
  
  private final boolean devMode;
  private final ActionMapping actionMapping;
  private static final RenderFactory renderFactory = RenderFactory.me();
  private static final Logger log = Logger.getLogger(ActionHandler.class);
  
  public ActionHandler(ActionMapping actionMapping, Constants constants) {
    this.actionMapping = actionMapping;
    this.devMode = constants.getDevMode();
  }
  
  /**
   * handle
   * 1: Action action = actionMapping.getAction(target)
   * 2: new ActionInvocation(...).invoke()
   * 3: render(...)
   */
  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.getError404Render().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 (Error404Exception e) {
      if (log.isWarnEnabled()) {
        String qs = request.getQueryString();
        log.warn("Resource not found: " + (qs == null ? target : target + "?" + qs));
      }
      e.getError404Render().setContext(request, response).render();
    }
    catch (Error500Exception e) {
      if (log.isErrorEnabled()) {
        String qs = request.getQueryString();
        log.error(qs == null ? target : target + "?" + qs, e);
      }
      e.getError500Render().setContext(request, response).render();
    }
    catch (Exception e) {
      if (log.isErrorEnabled()) {
        String qs = request.getQueryString();
        log.error(qs == null ? target : target + "?" + qs, e);
      }
      renderFactory.getError500Render().setContext(request, response).render();
    }
  }
}//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...