Beispiel #1
0
  /**
   * Send an HTTP error response code.
   *
   * @param request the HttpServletRequest object.
   * @param response the HttpServletResponse object.
   * @param code the HttpServletResponse error code (see {@link
   *     javax.servlet.http.HttpServletResponse} for possible error codes).
   * @param e the Exception that is reported.
   * @param ctx the ServletContext object.
   */
  public void sendError(
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext ctx,
      int code,
      Exception e) {
    if (devMode) {
      response.setContentType("text/html");

      try {
        FreemarkerManager mgr = getContainer().getInstance(FreemarkerManager.class);

        freemarker.template.Configuration config = mgr.getConfiguration(ctx);
        Template template = config.getTemplate("/org/apache/struts2/dispatcher/error.ftl");

        List<Throwable> chain = new ArrayList<Throwable>();
        Throwable cur = e;
        chain.add(cur);
        while ((cur = cur.getCause()) != null) {
          chain.add(cur);
        }

        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("exception", e);
        data.put("unknown", Location.UNKNOWN);
        data.put("chain", chain);
        data.put("locator", new Locator());
        template.process(data, response.getWriter());
        response.getWriter().close();
      } catch (Exception exp) {
        try {
          response.sendError(code, "Unable to show problem report: " + exp);
        } catch (IOException ex) {
          // we're already sending an error, not much else we can do if more stuff breaks
        }
      }
    } else {
      try {
        // WW-1977: Only put errors in the request when code is a 500 error
        if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
          // send a http error response to use the servlet defined error handler
          // make the exception availible to the web.xml defined error page
          request.setAttribute("javax.servlet.error.exception", e);

          // for compatibility
          request.setAttribute("javax.servlet.jsp.jspException", e);
        }

        // send the error response
        response.sendError(code, e.getMessage());
      } catch (IOException e1) {
        // we're already sending an error, not much else we can do if more stuff breaks
      }
    }
  }
 // 刷新所有缓存
 public String flush() {
   cacheManager.flushAll();
   flushCache();
   try {
     ServletContext servletContext = ServletActionContext.getServletContext();
     freemarkerManager.getConfiguration(servletContext).clearTemplateCache();
   } catch (TemplateException e) {
     e.printStackTrace();
   }
   return SUCCESS;
 }
 // 更新
 @Validations(
     requiredStrings = {
       @RequiredStringValidator(fieldName = "templateFileContent", message = "模板内容不允许为空!")
     })
 @InputConfig(resultName = "error")
 public String update() {
   printTemplateConfig = TemplateConfigUtil.getPrintTemplateConfig(printTemplateConfig.getName());
   TemplateConfigUtil.writeTemplateFileContent(printTemplateConfig, templateFileContent);
   try {
     freemarkerManager.getConfiguration(getServletContext()).clearTemplateCache();
   } catch (TemplateException e) {
     e.printStackTrace();
   }
   redirectUrl = "print_template!list.action";
   return SUCCESS;
 }