FileDocCategorySizeDatePackage
VelocityTemplateEngine.javaAPI DocExample4265Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components.template

VelocityTemplateEngine

public class VelocityTemplateEngine extends BaseTemplateEngine
Velocity based template engine.

Fields Summary
private static final Log
LOG
private org.apache.struts2.views.velocity.VelocityManager
velocityManager
Constructors Summary
Methods Summary
protected java.lang.StringgetSuffix()

        return "vm";
    
public voidrenderTemplate(TemplateRenderingContext templateContext)

        // get the various items required from the stack
        Map actionContext = templateContext.getStack().getContext();
        ServletContext servletContext = (ServletContext) actionContext.get(ServletActionContext.SERVLET_CONTEXT);
        HttpServletRequest req = (HttpServletRequest) actionContext.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse res = (HttpServletResponse) actionContext.get(ServletActionContext.HTTP_RESPONSE);

        // prepare velocity
        velocityManager.init(servletContext);
        VelocityEngine velocityEngine = velocityManager.getVelocityEngine();

        // get the list of templates we can use
        List templates = templateContext.getTemplate().getPossibleTemplates(this);

        // find the right template
        org.apache.velocity.Template template = null;
        String templateName = null;
        Exception exception = null;
        for (Iterator iterator = templates.iterator(); iterator.hasNext();) {
            Template t = (Template) iterator.next();
            templateName = getFinalTemplateName(t);
            try {
                // try to load, and if it works, stop at the first one
                template = velocityEngine.getTemplate(templateName);
                break;
            } catch (IOException e) {
                if (exception == null) {
                    exception = e;
                }
            }
        }

        if (template == null) {
            LOG.error("Could not load template " + templateContext.getTemplate());
            if (exception != null) {
                throw exception;
            } else {
                return;
            }
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("Rendering template " + templateName);
        }

        Context context = velocityManager.createContext(templateContext.getStack(), req, res);

        Writer outputWriter = templateContext.getWriter();
        context.put("tag", templateContext.getTag());
        context.put("parameters", templateContext.getParameters());

        template.merge(context, outputWriter);
    
public voidsetVelocityManager(org.apache.struts2.views.velocity.VelocityManager mgr)

    
    
        
        this.velocityManager = mgr;