Search in sources :

Example 1 with Render

use of com.jfinal.render.Render in project jfinal by jfinal.

the class CacheInterceptor method cacheAction.

protected void cacheAction(String cacheName, String cacheKey, Controller controller) {
    HttpServletRequest request = controller.getRequest();
    Map<String, Object> cacheData = new HashMap<String, Object>();
    for (Enumeration<String> names = request.getAttributeNames(); names.hasMoreElements(); ) {
        String name = names.nextElement();
        cacheData.put(name, request.getAttribute(name));
    }
    Render render = controller.getRender();
    if (render != null) {
        // cache RenderInfo
        cacheData.put(renderKey, createRenderInfo(render));
    }
    CacheKit.put(cacheName, cacheKey, cacheData);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Render(com.jfinal.render.Render)

Example 2 with Render

use of com.jfinal.render.Render in project jfinal by jfinal.

the class ActionHandler method handle.

/**
	 * handle
	 * 1: Action action = actionMapping.getAction(target)
	 * 2: new Invocation(...).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("404 Action Not Found: " + (qs == null ? target : target + "?" + qs));
        }
        renderManager.getRenderFactory().getErrorRender(404).setContext(request, response).render();
        return;
    }
    try {
        Controller controller = action.getControllerClass().newInstance();
        controller.init(request, response, urlPara[0]);
        if (devMode) {
            if (ActionReporter.isReportAfterInvocation(request)) {
                new Invocation(action, controller).invoke();
                ActionReporter.report(target, controller, action);
            } else {
                ActionReporter.report(target, controller, action);
                new Invocation(action, controller).invoke();
            }
        } else {
            new Invocation(action, controller).invoke();
        }
        Render render = controller.getRender();
        if (render instanceof ForwardActionRender) {
            String actionUrl = ((ForwardActionRender) 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 = renderManager.getRenderFactory().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();
        String msg = null;
        if (errorCode == 404) {
            msg = "404 Not Found: ";
        } else if (errorCode == 401) {
            msg = "401 Unauthorized: ";
        } else if (errorCode == 403) {
            msg = "403 Forbidden: ";
        }
        if (msg != null) {
            if (log.isWarnEnabled()) {
                String qs = request.getQueryString();
                log.warn(msg + (qs == null ? target : target + "?" + qs));
            }
        } else {
            if (log.isErrorEnabled()) {
                String qs = request.getQueryString();
                log.error(qs == null ? target : target + "?" + qs, e);
            }
        }
        e.getErrorRender().setContext(request, response, action.getViewPath()).render();
    } catch (Exception e) {
        if (log.isErrorEnabled()) {
            String qs = request.getQueryString();
            log.error(qs == null ? target : target + "?" + qs, e);
        }
        renderManager.getRenderFactory().getErrorRender(500).setContext(request, response, action.getViewPath()).render();
    }
}
Also used : RenderException(com.jfinal.render.RenderException) Invocation(com.jfinal.aop.Invocation) Render(com.jfinal.render.Render) RenderException(com.jfinal.render.RenderException)

Aggregations

Render (com.jfinal.render.Render)2 Invocation (com.jfinal.aop.Invocation)1 RenderException (com.jfinal.render.RenderException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1