有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java抑制Freemarker模板错误

我使用的是struts-2.3.16,我必须在应用程序中全局抑制Freemarker模板的异常。这意味着,我必须转发到一个显示通用消息的全局jsp,以防止向用户显示stacktrace,而不是带有Freemarker的stacktrace的黄色屏幕。对于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转发到我的异常。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
    }
}

有人知道我怎么做吗?我希望stacktrace只记录在服务器上,并在UI中用一般消息替换stacktrace


共 (2) 个答案

  1. # 1 楼答案

    好的,我解决这个问题的方法是在CustomTemplateExceptionHandler中的PrintWriter上打印一个响应,类似于Freemarker提供的标准HTML_调试_处理程序。查看此链接:

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

    在这里,您可以看到如何管理HTML_调试_处理程序。我将stacktrace打印替换为一般消息。Freemarker文档建议您使用RETHROW_处理程序,并在调用模板之后在应用程序中捕获异常。进程()。请看这里:

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

    但是由于Struts2在后台使用Freemarker,并且Freemarker方法是在操作执行后执行的,因此我无法确定如何以及在何处捕获异常。我已经设法在handleTemplateException()方法中获得了HttpServlet响应和请求(请参见问题),但我无法转发到我的异常。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. # 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
    

    希望这有帮助