【发布时间】: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