FileDocCategorySizeDatePackage
PortletFreemarkerResult.javaAPI DocExample11222Mon Jul 23 13:26:42 BST 2007org.apache.struts2.views.freemarker

PortletFreemarkerResult

public class PortletFreemarkerResult extends org.apache.struts2.dispatcher.StrutsResultSupport

Fields Summary
private static final long
serialVersionUID
protected com.opensymphony.xwork2.ActionInvocation
invocation
protected freemarker.template.Configuration
configuration
protected freemarker.template.ObjectWrapper
wrapper
protected FreemarkerManager
freemarkerManager
protected String
location
private String
pContentType
Constructors Summary
public PortletFreemarkerResult()


      
        super();
    
public PortletFreemarkerResult(String location)

        super(location);
    
Methods Summary
protected freemarker.template.TemplateModelcreateModel()
Build the instance of the ScopesHashModel, including JspTagLib support

Objects added to the model are

  • Application - servlet context attributes hash model
  • JspTaglibs - jsp tag lib factory model
  • Request - request attributes hash model
  • Session - session attributes hash model
  • request - the HttpServletRequst object for direct access
  • response - the HttpServletResponse object for direct access
  • stack - the OgnLValueStack instance for direct access
  • ognl - the instance of the OgnlTool
  • action - the action itself
  • exception - optional : the JSP or Servlet exception as per the servlet spec (for JSP Exception pages)
  • struts - instance of the StrutsUtil class

        ServletContext servletContext = ServletActionContext
                .getServletContext();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        ValueStack stack = ServletActionContext.getContext()
                .getValueStack();
        return freemarkerManager.buildTemplateModel(stack,
                invocation.getAction(), servletContext, request, response,
                wrapper);
    
protected java.util.LocalededuceLocale()
Returns the locale used for the {@link Configuration#getTemplate(String, Locale)}call. The base implementation simply returns the locale setting of the configuration. Override this method to provide different behaviour,

        return configuration.getLocale();
    
public voiddoExecute(java.lang.String location, com.opensymphony.xwork2.ActionInvocation invocation)
Execute this result, using the specified template location.

The template location has already been interoplated for any variable substitutions

this method obtains the freemarker configuration and the object wrapper from the provided hooks. It them implements the template processing workflow by calling the hooks for preTemplateProcess and postTemplateProcess

        if (PortletActionContext.isEvent()) {
            executeActionResult(location, invocation);
        } else if (PortletActionContext.isRender()) {
            executeRenderResult(location, invocation);
        }
    
private voidexecuteActionResult(java.lang.String location, com.opensymphony.xwork2.ActionInvocation invocation)

param
location
param
invocation

        ActionResponse res = PortletActionContext.getActionResponse();
        // View is rendered outside an action...uh oh...
        res.setRenderParameter(PortletActionConstants.ACTION_PARAM, "freemarkerDirect");
        res.setRenderParameter("location", location);
        res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext
                .getRequest().getPortletMode().toString());

    
private voidexecuteRenderResult(java.lang.String location, com.opensymphony.xwork2.ActionInvocation invocation)

param
location
param
invocation
throws
TemplateException
throws
IOException
throws
TemplateModelException

        prepareServletActionContext();
        this.location = location;
        this.invocation = invocation;
        this.configuration = getConfiguration();
        this.wrapper = getObjectWrapper();

        HttpServletRequest req = ServletActionContext.getRequest();

        if (!location.startsWith("/")) {
            String base = ResourceUtil.getResourceBase(req);
            location = base + "/" + location;
        }

        Template template = configuration.getTemplate(location, deduceLocale());
        TemplateModel model = createModel();
        // Give subclasses a chance to hook into preprocessing
        if (preTemplateProcess(template, model)) {
            try {
                // Process the template
                PortletActionContext.getRenderResponse().setContentType(pContentType);
                template.process(model, getWriter());
            } finally {
                // Give subclasses a chance to hook into postprocessing
                postTemplateProcess(template, model);
            }
        }
    
protected freemarker.template.ConfigurationgetConfiguration()
This method is called from {@link #doExecute(String, ActionInvocation)} to obtain the FreeMarker configuration object that this result will use for template loading. This is a hook that allows you to custom-configure the configuration object in a subclass, or to fetch it from an IoC container.

The default implementation obtains the configuration from the ConfigurationManager instance.

        return freemarkerManager.getConfiguration(
                ServletActionContext.getServletContext());
    
public java.lang.StringgetContentType()
allow parameterization of the contentType the default being text/html

        return pContentType;
    
protected freemarker.template.ObjectWrappergetObjectWrapper()
This method is called from {@link #doExecute(String, ActionInvocation)} to obtain the FreeMarker object wrapper object that this result will use for adapting objects into template models. This is a hook that allows you to custom-configure the wrapper object in a subclass.

The default implementation returns {@link Configuration#getObjectWrapper()}

        return configuration.getObjectWrapper();
    
protected java.io.WritergetWriter()
The default writer writes directly to the response writer.

        return PortletActionContext.getRenderResponse().getWriter();
    
protected voidpostTemplateProcess(freemarker.template.Template template, freemarker.template.TemplateModel data)
the default implementation of postTemplateProcess applies the contentType parameter

    
protected booleanpreTemplateProcess(freemarker.template.Template template, freemarker.template.TemplateModel model)
Called before the execution is passed to template.process(). This is a generic hook you might use in subclasses to perform a specific action before the template is processed. By default does nothing. A typical action to perform here is to inject application-specific objects into the model root

return
true to process the template, false to suppress template processing.

        Object attrContentType = template.getCustomAttribute("content_type");

        if (attrContentType != null) {
            ServletActionContext.getResponse().setContentType(
                    attrContentType.toString());
        } else {
            String contentType = getContentType();

            if (contentType == null) {
                contentType = "text/html";
            }

            String encoding = template.getEncoding();

            if (encoding != null) {
                contentType = contentType + "; charset=" + encoding;
            }

            ServletActionContext.getResponse().setContentType(contentType);
        }

        return true;
    
private voidprepareServletActionContext()

        PortletRequestDispatcher disp = PortletActionContext.getPortletConfig()
                .getPortletContext().getNamedDispatcher("preparator");
        disp.include(PortletActionContext.getRenderRequest(),
                PortletActionContext.getRenderResponse());
    
public voidsetContentType(java.lang.String aContentType)

        pContentType = aContentType;
    
public voidsetFreemarkerManager(FreemarkerManager mgr)

        this.freemarkerManager = mgr;