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
# 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:
类CustomTemplateExceptionHandler:
如果有人对此有更好的回应,请发布您的答案
# 2 楼答案
应该更容易些。如果不希望出现黄色的调试模板错误,则必须将
TemplateExceptionHandler
从HTML_DEBUG_HANDLER
切换到RETHROW_HANDLER
(如顶部的freemarker消息所示:freemarker模板错误调试模式;在生产中使用RETHROW!)现在,我更喜欢以编程的方式(正如您所做的那样)进行操作,因为我希望根据环境选择
TemplateExceptionHandler
(PRO
,TEST
,DEVEL
),我的解决方案是将环境信息放在上下文中并告诉struts在
struts.properties
中使用您的管理器希望这有帮助