title: Suppress Freemarker template error
body: <p>I am using struts-2.3.16 and I have to suppress exceptions from Freemarker template globally in our application. This means that, instead of the yellow screen with the stacktrace from Freemarker, I have to forward to a global jsp that displays a generic message, so preventing the display of stacktraces to the user. For generic exceptions in struts we mapped a global-results in struts.xml, but it's not working for Freemarker exceptions.</p>
  
  <p>So far I have implemented the solution from <a href="https://stackoverflow.com/questions/15123743/what-are-different-ways-to-handle-error-in-freemarker-template">What are different ways to handle error in FreeMarker template?</a>. So I created a CustomFreemarkerManager and a CustomTemplateExceptionHandler. </p>
  
  <p>My CustomFreemarkerManager looks like this:</p>
  
  <pre><code>@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 &gt; 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;
  }
  </code></pre>
  
  <p>From here I send the ServletContext to my CustomTemplateExceptionHandler so I can create a RequestDispatcher to forward to my exception.jsp. The problem is that in the exception handler I don't have the request and the response and I can't forward to my jsp.</p>
  
  <p>The class CustomTemplateExceptionHandler looks like this so far:</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>Anybody knows how can I do that? I want the stacktrace to be logged only on the server, and in the UI to replace the stacktrace with a generic message.</p>