【问题标题】:Suppress Freemarker template error抑制 Freemarker 模板错误
【发布时间】:2016-07-07 20:47:50
【问题描述】:

我正在使用 struts-2.3.16,我必须在我们的应用程序中全局抑制来自 Freemarker 模板的异常。这意味着,我必须转发到显示通用消息的全局 jsp,而不是带有来自 Freemarker 的堆栈跟踪的黄色屏幕,从而阻止向用户显示堆栈跟踪。对于 struts 中的通用异常,我们在 struts.xml 中映射了一个全局结果,但它不适用于 Freemarker 异常。

到目前为止,我已经实现了What are different ways to handle error in FreeMarker template? 的解决方案。所以我创建了一个 CustomFreemarkerManager 和一个 CustomTemplateExceptionHandler。

我的 CustomFreemarkerManager 看起来像这样:

@Override
public void init(ServletContext servletContext) throws TemplateException {
    super.config = super.createConfiguration(servletContext);
    super.config.setTemplateExceptionHandler(new CustomTemplateExceptionHandler(servletContext));
    super.contentType = "text/html";
    super.wrapper = super.createObjectWrapper(servletContext);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Using object wrapper of class " + super.wrapper.getClass().getName(), new String[0]);
    }

    super.config.setObjectWrapper(super.wrapper);
    super.templatePath = servletContext.getInitParameter("TemplatePath");
    if (super.templatePath == null) {
        super.templatePath = servletContext.getInitParameter("templatePath");
    }

    super.configureTemplateLoader(super.createTemplateLoader(servletContext, super.templatePath));
    super.loadSettings(servletContext);
}

@Override
protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException {
    Configuration configuration = new Configuration();
    configuration.setTemplateExceptionHandler(new CustomTemplateExceptionHandler(servletContext));
    if (super.mruMaxStrongSize > 0) {
        configuration.setSetting("cache_storage", "strong:" + super.mruMaxStrongSize);
    }

    if (super.templateUpdateDelay != null) {
        configuration.setSetting("template_update_delay", super.templateUpdateDelay);
    }

    if (super.encoding != null) {
        configuration.setDefaultEncoding(super.encoding);
    }

    configuration.setLocalizedLookup(false);
    configuration.setWhitespaceStripping(true);
    return configuration;
}

从这里我将 ServletContext 发送到我的 CustomTemplateExceptionHandler,这样我就可以创建一个 RequestDispatcher 来转发到我的 exception.jsp。问题是在异常处理程序中我没有请求和响应,我无法转发到我的 jsp。

到目前为止,CustomTemplateExceptionHandler 类看起来像这样:

private ServletContext servletContext;

public CustomTemplateExceptionHandler(ServletContext servletContext) {
    this.servletContext = servletContext;
}

public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
    if (servletContext != null) {
        RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/resources/exception.jsp");

        //HERE I have to forward to my jsp
    }
}

有人知道我该怎么做吗?我希望堆栈跟踪仅记录在服务器上,并在 UI 中以通用消息替换堆栈跟踪。

【问题讨论】:

  • 我现在将代码修改为重新抛出异常。它是一个扩展 java.lang.Exception 的 freemarker.core.InvalidReferenceException,应该被 Struts2 捕获。对于 Struts 中抛出的所有其他异常,将捕获异常并显示全局异常消息。似乎在这种情况下,首先执行操作,然后抛出 Freemarker 异常,所以也许这就是它没有被 Struts 捕获的原因。
  • 是的,你是对的。您可以尝试使用异常处理程序打印重定向。见freemarker.624813.n4.nabble.com/…
  • 查看HTML_DEBUG_HANDLER 的打印效果 - github.com/apache/incubator-freemarker/blob/2.3-gae/src/main/…。顺便说一句,好问题。 :)
  • 谢谢!问题是异常处理程序中的 PrintWriter 不是空的,那里加载了操作的结果页面,然后您编写的所有内容都附加到之前在 Writer 中编写的内容。我不敢相信这个问题没有简单的解决方案!
  • 那又怎样?重定向不起作用吗?不要忘记这是模板,您通常不会重定向离开模板。在生产中也不会收到错误。

标签: java struts2 freemarker


【解决方案1】:

好的,所以我对这个问题的解决方案是在我的 CustomTemplateExceptionHandler 中的 PrintWriter 上打印一个类似于 Freemarker 提供的标准 HTML_DEBUG_HANDLER 的响应。看看这个链接:

https://github.com/apache/incubator-freemarker/blob/2.3-gae/src/main/java/freemarker/template/TemplateExceptionHandler.java#L98

在这里您可以看到 HTML_DEBUG_HANDLER 是如何管理的。我用一般消息替换了打印堆栈跟踪。 Freemarker 文档建议您使用 RETHROW_HANDLER 并稍后在调用 Template.process() 后在应用程序中捕获异常。见这里:

http://freemarker.org/docs/app_faq.html#misc.faq.niceErrorPage

但是由于 Struts2 在后台使用 Freemarker,并且 Freemarker 方法是在执行操作后执行的,所以我无法弄清楚如何以及在何处捕获异常。我已经设法在方法 handleTemplateException() 中获得了 HttpServlet 响应和请求(请参阅问题),但我无法转发到我的 exception.jsp,因为响应已经提交,所以它给了我一个异常。

最终代码如下所示:

类 CustomFreemarkerManager:

@Override
public void init(ServletContext servletContext) throws TemplateException {
    super.config = super.createConfiguration(servletContext);
    super.config.setTemplateExceptionHandler(new CustomTemplateExceptionHandler());
    super.contentType = "text/html";
    super.wrapper = super.createObjectWrapper(servletContext);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Using object wrapper of class " + super.wrapper.getClass().getName(), new String[0]);
    }

    super.config.setObjectWrapper(super.wrapper);
    super.templatePath = servletContext.getInitParameter("TemplatePath");
    if (super.templatePath == null) {
        super.templatePath = servletContext.getInitParameter("templatePath");
    }

    super.configureTemplateLoader(super.createTemplateLoader(servletContext, super.templatePath));
    super.loadSettings(servletContext);
}

@Override
protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException {
    Configuration configuration = new Configuration();
    configuration.setTemplateExceptionHandler(new CustomTemplateExceptionHandler());
    if (super.mruMaxStrongSize > 0) {
        configuration.setSetting("cache_storage", "strong:" + super.mruMaxStrongSize);
    }

    if (super.templateUpdateDelay != null) {
        configuration.setSetting("template_update_delay", super.templateUpdateDelay);
    }

    if (super.encoding != null) {
        configuration.setDefaultEncoding(super.encoding);
    }

    configuration.setLocalizedLookup(false);
    configuration.setWhitespaceStripping(true);
    return configuration;
}

类 CustomTemplateExceptionHandler:

public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {

    boolean externalPw = out instanceof PrintWriter;
    PrintWriter pw = externalPw ? (PrintWriter) out : new PrintWriter(out);
    try {
        pw.print("<!-- ERROR MESSAGE STARTS HERE -->"
                + "<!-- ]]> -->"
                + "</table></table></table>"
                + "<div align='left' style='"
                + "background-color:#FFFF7C; "
                + "display:block; "
                + "border-top:double; "
                + "padding:10px; "
                + "'>");
        pw.print("<b style='"
                + "color: red; "
                + "font-size:14px; "
                + "font-style:normal; "
                + "font-weight:bold; "
                + "'>"
                + "Oops! We have encountered a problem. Please try again!"
                + "</b>");
        pw.println("</div></html>");
        pw.flush();  // To commit the HTTP response
    } finally {
        if (!externalPw) pw.close();
    }

    throw te;
}

如果有人对此有更好的回应,请发布您的答案!

【讨论】:

    【解决方案2】:

    应该更容易。如果您不希望出现黄色调试模板错误,则必须将 TemplateExceptionHandlerHTML_DEBUG_HANDLER 切换到 RETHROW_HANDLER(如顶部的 freemarker 消息建议:FreeMarker 模板错误调试模式;在生产中使用 RETHROW !)

    现在,我更喜欢以编程方式(就像你一样),因为我想根据环境选择TemplateExceptionHandlerPROTESTDEVEL),我的解决方案是放置环境信息在上下文中。

    public class CustomFreemarkerManager extends FreemarkerManager {    
    
    
        public CustomFreemarkerManager(){
            super();
        }
    
        @Override
        public void init(ServletContext servletContext) throws TemplateException {
    
            //important!
            super.init(servletContext);
    
            //other stuff maybe you want to tune...
    
            //Getting environmentInfo object from the context, it's a personal solution 
            EnvironmentInfo environmentInfo = (EnvironmentInfo)servletContext.getAttribute(EnvironmentInfo.CONTEXT_NAME);
    
            if (environment.isPro()) {
                config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            }else{
                config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);    
            }
    
        }
    }
    

    并告诉 struts 在struts.properties 中使用你的经理

    struts.freemarker.manager.classname=com.jobisjob.northpole.web.core.CustomFreemarkerManager
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 2015-02-27
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2011-11-25
      • 2011-04-21
      相关资源
      最近更新 更多