Exemple #1
0
  private String buildCacheKey(Invocation inv, Controller controller) {
    StringBuilder sb = new StringBuilder(inv.getActionKey());
    String urlPara = controller.getPara();
    if (urlPara != null) sb.append("/").append(urlPara);

    String queryString = controller.getRequest().getQueryString();
    if (queryString != null) sb.append("?").append(queryString);
    return sb.toString();
  }
 @Override
 protected void handleError(Controller c) {
   c.keepModel(User.class);
   c.keepPara();
   c.setAttr("state", "failure");
   if (ReturnKit.isJson(c.getRequest())) c.renderJson();
   else {
     if (getActionKey().equals("/member/control")) c.forwardAction("/member");
   }
 }
Exemple #3
0
  private 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));
    }

    cacheData.put(renderKey, new RenderInfo(controller.getRender())); // cache RenderInfo
    CacheKit.put(cacheName, cacheKey, cacheData);
  }
Exemple #4
0
  private void useCacheDataAndRender(Map<String, Object> cacheData, Controller controller) {
    HttpServletRequest request = controller.getRequest();
    Set<Entry<String, Object>> set = cacheData.entrySet();
    for (Iterator<Entry<String, Object>> it = set.iterator(); it.hasNext(); ) {
      Entry<String, Object> entry = it.next();
      request.setAttribute(entry.getKey(), entry.getValue());
    }
    request.removeAttribute(renderKey);

    controller.render(
        ((RenderInfo) cacheData.get(renderKey)).createRender()); // set render from cacheData
  }
  /**
   * 该拦截器取得当前ActionPath,从Cache中检查是否有传送给当前Action的Flash对象Map 若有,则遍历Map,并将所有key,value注入到当前的request请求中。
   */
  public void intercept(ActionInvocation ai) {
    Controller c = ai.getController();
    HttpSession session = c.getSession(false);
    if (null == session) {
      return;
    }

    // String curAction = ai.getViewPath()+ai.getMethodName();
    String curAction = c.getRequest().getServletPath();
    ConcurrentHashMap<String, Object> flashMap = c.getFlashManager().getFlash(session, curAction);
    if (flashMap != null) {
      for (Entry<String, Object> flashEntry : flashMap.entrySet()) {
        c.setAttr(flashEntry.getKey(), flashEntry.getValue());
      }
    }
    ai.invoke();
  }
Exemple #6
0
  /**
   * add edit 无需处理
   *
   * <p>GET /user ---> index GET /user/id ---> show POST /user ---> save PUT /user/id ---> update
   * DELECT /user/id ---> delete
   */
  public void intercept(ActionInvocation ai) {
    // 阻止 JFinal 原有规则 action 请求
    Controller controller = ai.getController();
    Boolean isRestfulForward = controller.getAttr(isRestfulForwardKey);
    String methodName = ai.getMethodName();
    if (set.contains(methodName) && isRestfulForward == null) {
      ai.getController().renderError(404);
      return;
    }

    if (isRestfulForward != null && isRestfulForward) {
      ai.invoke();
      return;
    }

    String controllerKey = ai.getControllerKey();
    String method = controller.getRequest().getMethod().toUpperCase();
    String urlPara = controller.getPara();
    if ("GET".equals(method)) {
      if (urlPara != null && !"edit".equals(methodName)) {
        controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
        controller.forwardAction(controllerKey + "/show/" + urlPara);
        return;
      }
    } else if ("POST".equals(method)) {
      controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
      controller.forwardAction(controllerKey + "/save");
      return;
    } else if ("PUT".equals(method)) {
      controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
      controller.forwardAction(controllerKey + "/update/" + urlPara);
      return;
    } else if ("DELETE".equals(method)) {
      controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
      controller.forwardAction(controllerKey + "/delete/" + urlPara);
      return;
    }

    ai.invoke();
  }