• <legend id='GFzZZ'><style id='GFzZZ'><dir id='GFzZZ'><q id='GFzZZ'></q></dir></style></legend>

      <i id='GFzZZ'><tr id='GFzZZ'><dt id='GFzZZ'><q id='GFzZZ'><span id='GFzZZ'><b id='GFzZZ'><form id='GFzZZ'><ins id='GFzZZ'></ins><ul id='GFzZZ'></ul><sub id='GFzZZ'></sub></form><legend id='GFzZZ'></legend><bdo id='GFzZZ'><pre id='GFzZZ'><center id='GFzZZ'></center></pre></bdo></b><th id='GFzZZ'></th></span></q></dt></tr></i><div id='GFzZZ'><tfoot id='GFzZZ'></tfoot><dl id='GFzZZ'><fieldset id='GFzZZ'></fieldset></dl></div>
        <bdo id='GFzZZ'></bdo><ul id='GFzZZ'></ul>

        <tfoot id='GFzZZ'></tfoot>
      1. <small id='GFzZZ'></small><noframes id='GFzZZ'>

      2. 处理 FreeMarker 模板中的错误有哪些不同的方法?

        时间:2023-09-24

            <tbody id='9AVdY'></tbody>
          • <small id='9AVdY'></small><noframes id='9AVdY'>

            1. <tfoot id='9AVdY'></tfoot>
                <bdo id='9AVdY'></bdo><ul id='9AVdY'></ul>

                <legend id='9AVdY'><style id='9AVdY'><dir id='9AVdY'><q id='9AVdY'></q></dir></style></legend>
                  <i id='9AVdY'><tr id='9AVdY'><dt id='9AVdY'><q id='9AVdY'><span id='9AVdY'><b id='9AVdY'><form id='9AVdY'><ins id='9AVdY'></ins><ul id='9AVdY'></ul><sub id='9AVdY'></sub></form><legend id='9AVdY'></legend><bdo id='9AVdY'><pre id='9AVdY'><center id='9AVdY'></center></pre></bdo></b><th id='9AVdY'></th></span></q></dt></tr></i><div id='9AVdY'><tfoot id='9AVdY'></tfoot><dl id='9AVdY'><fieldset id='9AVdY'></fieldset></dl></div>

                1. 本文介绍了处理 FreeMarker 模板中的错误有哪些不同的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何抑制 FreeMarker 模板错误?我在看这里:http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html但我不明白如何TemplateExceptionHandler.IGNORE_HANDLER".我正在使用 Struts2 以及如何显示另一个 ftl 页面而不是显示堆栈跟踪?

                  How to suppress FreeMarker template error? I am looking here: http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html But 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());
                  

                  在 http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html我该如何使用它?最后一行,cfg 是从哪里来的?

                  Found the above piece at http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html How do I use this? That last line, where does cfg come from?

                  FreeMarker API 的主要入口点"... http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html

                  "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.

                  这个博客也指出了同样的事情:http://blog.cherouvim.com/freemarker-exception-handling/ 我应该在哪里配置我的 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?

                  推荐答案

                  如果想在Struts2中设置TemplateExceptionHandlerTemplateExceptionHandler.IGNORE_HANDLER,需要扩展org.apache.struts2.views.freemarker.FreemarkerManager 类,覆盖 initcreateConfiguration 方法并在 struts.properties文件.

                  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 模板中的错误有哪些不同的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                2. <small id='ppg0Q'></small><noframes id='ppg0Q'>

                3. <i id='ppg0Q'><tr id='ppg0Q'><dt id='ppg0Q'><q id='ppg0Q'><span id='ppg0Q'><b id='ppg0Q'><form id='ppg0Q'><ins id='ppg0Q'></ins><ul id='ppg0Q'></ul><sub id='ppg0Q'></sub></form><legend id='ppg0Q'></legend><bdo id='ppg0Q'><pre id='ppg0Q'><center id='ppg0Q'></center></pre></bdo></b><th id='ppg0Q'></th></span></q></dt></tr></i><div id='ppg0Q'><tfoot id='ppg0Q'></tfoot><dl id='ppg0Q'><fieldset id='ppg0Q'></fieldset></dl></div>
                      <tbody id='ppg0Q'></tbody>

                        <bdo id='ppg0Q'></bdo><ul id='ppg0Q'></ul>
                        <tfoot id='ppg0Q'></tfoot>

                        <legend id='ppg0Q'><style id='ppg0Q'><dir id='ppg0Q'><q id='ppg0Q'></q></dir></style></legend>