Java – what are the different ways to handle errors in the FreeMarker template?

How to suppress FreeMarker template errors?

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());

Found the above parts in http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html How do I use it? In the last line, where does the CFG come from?

"Main entry point to FreeMarker API" http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html

So, that's the main entry point. I guess this CFG comes from this class I still can't see how the controller enters my class mytemplateexceptionhandler

Where do I need to go?

cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());

Is this just putting this line in the right place?

This is what 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,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);
            }
        }
    }
}

My code enters the mytemplateexceptionhandler constructor () But do not enter mytemplateexceptionhandler1 handletemplateexception() What do I need to do?

I still see the Yellow FTL stack trace

The same thing is pointed out on this blog: http://blog.cherouvim.com/freemarker-exception-handling/ Where can I configure my freemaker and how? I still insist on where to go

My other question is, what I post on my blog is similar to an internal class. Do I just put this internal class in any class or an external class?

Solution

If you want to use templateexceptionhandler in struts 2 IGNORE_ If handler is set to templateexceptionhandler, you need to extend org apache. struts2. views. freemarker. Freemarkermanager class, covering init and createconfiguration methods, and in struts Configure the custom manager in the properties file

struts.freemarker.manager.classname = your.package.YourFreeMarkerManager

UPDATE

Your custom freemarkermanager should look like this:

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;
}
}

Put this constant in your struts In the XML file:

<constant name="struts.freemarker.manager.classname" value="your_package.MyFreemarkerManager" />
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>