问题描述
如何抑制FreeMarker模板错误?
我在这里看:
但我不明白如何TemplateExceptionHandler.IGNORE_HANDLER。我正在使用Struts2以及如何显示另一个ftl页面而不是显示堆栈跟踪?
How to suppress FreeMarker template error?I am looking here: http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.htmlBut I do not understand how to "TemplateExceptionHandler.IGNORE_HANDLER." I am using Struts2 and also how to show another ftl page instead of showing the stack trace?
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());
在
我该如何使用它?最后一行,cfg来自哪里?
Found the above piece at http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.htmlHow do I use this? That last line, where does cfg come from?
进入FreeMarker API的主要入口点......
"Main entry point into the FreeMarker API"... http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html
所以,这是主要的切入点,我猜cfg来自这个类。我仍然没有看到控制器将如何进入我的类MyTemplateExceptionHandler。
So, that is the main entry point, I am guessing cfg comes from this class. I am still not seeing how the controller will come into my class MyTemplateExceptionHandler.
下一行需要去哪里?
cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());
这只是将这条线放在正确的位置吗?
And is it just a matter of placing this line in correct spot?
这是我当前班级的样子:
This is how my current class looks like:
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, Environment env, 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()。我需要做什么?
My code is going into MyTemplateExceptionHandler constructor(). But not into MyTemplateExceptionHandler1 handleTemplateException(). What do I need to do?
我仍然看到黄色的FTL堆栈跟踪。
I am still seeing the yellow FTL stack trace.
同样的事情是在这个博客上被指出:我在哪里兴奋地配置我的freemarker以及如何配置?我仍然不知道该行需要去哪里。
Same thing is being pointed out on this blog: http://blog.cherouvim.com/freemarker-exception-handling/ Where excatly do I configure my freemarker and how? I am still stuck as to where that line needs to go.
我的另一个问题是,博客上发布的类似乎是一个内部类,我只是把它放那个内部类进入任何类或者是一个外部类?
My other question is, the class posted on the blog seems to be an inner class, do I just put that inner class into any class or is that an outer class?
推荐答案
如果你想设置 TemplateExceptionHandler在Struts2中
到 TemplateExceptionHandler.IGNORE_HANDLER
你需要扩展 org.apache.struts2.views.freemarker.FreemarkerManager
class,覆盖 init
和 createConfiguration
方法并在 struts中配置自定义管理器.properties
file。
If you want to set TemplateExceptionHandler
to TemplateExceptionHandler.IGNORE_HANDLER
in Struts2 you need to extend org.apache.struts2.views.freemarker.FreemarkerManager
class, override init
and createConfiguration
methods and configure your custom manager in struts.properties
file.
struts.freemarker.manager.classname = your.package.YourFreeMarkerManager
更新
您的自定义FreemarkerManager应该如下所示:
Your custom FreemarkerManager should look like that:
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文件中:
Put that constant in your struts.xml file:
<constant name="struts.freemarker.manager.classname" value="your_package.MyFreemarkerManager" />
这篇关于在FreeMarker模板中处理错误的不同方法有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!