[英]Suppress Freemarker template error
我正在使用struts-2.3.16,我必須在我們的應用程序中全局禁止Freemarker模板的異常。 這意味着,我必須轉至顯示通用消息的全局jsp,而不是從Freemarker看到帶有堆棧跟蹤的黃色屏幕,以防止向用戶顯示堆棧跟蹤。 對於struts中的通用異常,我們在struts.xml中映射了一個全局結果,但不適用於Freemarker異常。
到目前為止,我已經實現了FreeMarker模板中的錯誤處理方法有哪些? 。 因此,我創建了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中用通用消息替換堆棧跟蹤。
好的,所以我解決這個問題的方法是在我的CustomTemplateExceptionHandler中的PrintWriter上打印一個類似於Freemarker提供的標准HTML_DEBUG_HANDLER的響應。 看看這個鏈接:
在這里,您可以看到如何管理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,因為響應已經提交,所以它給了我一個例外。
最終代碼如下所示:
Class 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;
}
如果有人對此有更好的回應,請發布您的答案!
它應該更容易。 如果您不想要黃色調試模板錯誤,則必須將TemplateExceptionHandler
從HTML_DEBUG_HANDLER
切換到RETHROW_HANDLER
(因為頂部的freemarker消息建議: FreeMarker模板錯誤DEBUG模式;在生產中使用RETHROW! )
現在,我更喜歡以編程方式(就像你做的那樣),因為我想根據環境( PRO
, TEST
, DEVEL
)選擇TemplateExceptionHandler
,我的解決方案是將環境信息放在上下文中。
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
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.