如何抑制FreeMarker模板错误?
我在寻找: http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html
但我不明白如何“TemplateExceptionHandler.IGnorE_HANDLER”.我正在使用Struts2以及如何显示另一个ftl页面,而不是显示堆栈跟踪?
我在寻找: http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html
但我不明白如何“TemplateExceptionHandler.IGnorE_HANDLER”.我正在使用Struts2以及如何显示另一个ftl页面,而不是显示堆栈跟踪?
class MyTemplateExceptionHandler implements TemplateExceptionHandler { public void handleTemplateException(TemplateException te,Environment env,java.io.Writer out) throws TemplateException { try { out.write("[ERROR: " + te.getMessage() + "]"); } catch (IOException e) { throw new TemplateException("Failed to print error message. Cause: " + e,env); } } } ... cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());
发现上面的零件在http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html
我该如何使用?最后一行,cfg来自哪里?
“进入FreeMarker API的主要入口点”… http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html
所以,那是主要的入门点,我猜这个cfg来自这个类.我仍然看不到控制器如何进入我的类MyTemplateExceptionHandler.
以下行需要去哪里?
cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());
这只是把这条线放在正确的地方吗?
这是我现在的班级的样子:
import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Writer; import java.util.Properties; import freemarker.cache.FileTemplateLoader; import freemarker.cache.MultiTemplateLoader; import freemarker.cache.TemplateLoader; import freemarker.cache.WebappTemplateLoader; import freemarker.core.Environment; import freemarker.ext.beans.BeansWrapper; import freemarker.ext.jsp.TaglibFactory; import freemarker.ext.servlet.HttpRequestHashModel; import freemarker.ext.servlet.HttpRequestParametersHashModel; import freemarker.ext.servlet.HttpSessionHashModel; import freemarker.ext.servlet.ServletContextHashModel; import freemarker.template.ObjectWrapper; import freemarker.template.TemplateException; import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateModel; import javax.servlet.GenericServlet; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.views.JspSupportServlet; import org.apache.struts2.views.freemarker.FreemarkerManager; import org.apache.struts2.views.freemarker.ScopesHashModel; import org.apache.struts2.views.freemarker.StrutsBeanWrapper; import org.apache.struts2.views.freemarker.StrutsClasstemplateLoader; import org.omg.CORBA.PUBLIC_MEMBER; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.util.FileManager; import com.opensymphony.xwork2.util.ValueStack; public class MyTemplateExceptionHandler extends org.apache.struts2.views.freemarker.FreemarkerManager { freemarker.template.Configuration configuration = new freemarker.template.Configuration(); public MyTemplateExceptionHandler() { System.out.println("MyTemplateExceptionHandler constructor()"); configuration.setTemplateExceptionHandler(new Test1()); } class Test1 implements TemplateExceptionHandler { @Override public void handleTemplateException(TemplateException te,java.io.Writer out) throws TemplateException { System.out.println("MyTemplateExceptionHandler1 handleTemplateException()"); try { out.write("[ERROR TEST TEST: " + te.getMessage() + "]"); } catch (IOException e) { throw new TemplateException("Failed to print error message. Cause: " + e,env); } } } }
我的代码进入MyTemplateExceptionHandler构造函数().但不进入MyTemplateExceptionHandler1 handleTemplateException().我需要做什么?
我仍然看到黄色的FTL堆栈跟踪.
在这个博客上指出了同样的事情:http://blog.cherouvim.com/freemarker-exception-handling/哪里可以配置我的freemarker和如何?我仍然坚持要去哪里.
解决方法
如果要在Struts2中将TemplateExceptionHandler.IGnorE_HANDLER设置为TemplateExceptionHandler,则需要扩展org.apache.struts2.views.freemarker.FreemarkerManager类,覆盖init和createConfiguration方法,并在struts.properties文件中配置自定义管理器.
struts.freemarker.manager.classname = your.package.YourFreeMarkerManager
UPDATE
您的自定义FreemarkerManager应如下所示:
public class MyFreemarkerManager extends org.apache.struts2.views.freemarker.FreemarkerManager { private static final Logger LOG = LoggerFactory .getLogger(MyFreemarkerManager.class); @Override public void init(ServletContext servletContext) throws TemplateException { config = createConfiguration(servletContext); // Set defaults: config.setTemplateExceptionHandler(TemplateExceptionHandler.IGnorE_HANDLER); contentType = DEFAULT_CONTENT_TYPE; // Process object_wrapper init-param out of order: wrapper = createObjectWrapper(servletContext); if(LOG.isDebugEnabled()) { LOG.debug("Using object wrapper of class " + wrapper.getClass().getName()); } config.setobjectWrapper(wrapper); // Process TemplatePath init-param out of order: templatePath = servletContext.getinitParameter(INITParaM_TEMPLATE_PATH); if(templatePath == null) { templatePath = servletContext.getinitParameter("templatePath"); } config .setTemplateLoader(createTemplateLoader(servletContext,templatePath)); loadSettings(servletContext); } @Override protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException { Configuration configuration = new Configuration(); configuration .setTemplateExceptionHandler(TemplateExceptionHandler.IGnorE_HANDLER); if(mruMaxStrongSize > 0) { configuration.setSetting(Configuration.CACHE_STORAGE_KEY,"strong:" + mruMaxStrongSize); } if(templateUpdateDelay != null) { configuration.setSetting(Configuration.TEMPLATE_UPDATE_DELAY_KEY,templateUpdateDelay); } if(encoding != null) { configuration.setdefaultencoding(encoding); } configuration.setWhitespaceStripping(true); return configuration; } }
把这个常量放在你的struts.xml文件中:
<constant name="struts.freemarker.manager.classname" value="your_package.MyFreemarkerManager" />