FileDocCategorySizeDatePackage
PortletVelocityResult.javaAPI DocExample11650Mon Jul 23 13:26:40 BST 2007org.apache.struts2.portlet.result

PortletVelocityResult

public class PortletVelocityResult extends org.apache.struts2.dispatcher.StrutsResultSupport
Using the Servlet container's {@link JspFactory}, this result mocks a JSP execution environment and then displays a Velocity template that will be streamed directly to the servlet output.

This result type takes the following parameters:

  • location (default) - the location of the template to process.
  • parse - true by default. If set to false, the location param will not be parsed for Ognl expressions.

This result follows the same rules from {@link StrutsResultSupport}.

Example:
<!-- START SNIPPET: example -->
<result name="success" type="velocity">
<param name="location">foo.vm</param>
</result>
<!-- END SNIPPET: example -->

Fields Summary
private static final long
serialVersionUID
private static final Log
log
private String
defaultEncoding
private org.apache.struts2.views.velocity.VelocityManager
velocityManager
Constructors Summary
public PortletVelocityResult()

    
      
        super();
    
public PortletVelocityResult(String location)

        super(location);
    
Methods Summary
protected org.apache.velocity.context.ContextcreateContext(org.apache.struts2.views.velocity.VelocityManager velocityManager, com.opensymphony.xwork2.util.ValueStack stack, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String location)
Creates the VelocityContext that we'll use to render this page.

param
velocityManager a reference to the velocityManager to use
param
stack the value stack to resolve the location against (when parse equals true)
param
location the name of the template that is being used
return
the a minted Velocity context.

        return velocityManager.createContext(stack, request, response);
    
public voiddoExecute(java.lang.String location, com.opensymphony.xwork2.ActionInvocation invocation)

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

param
location The location string
param
invocation The action 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());

    
public voidexecuteRenderResult(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation invocation)
Creates a Velocity context from the action, loads a Velocity template and executes the template. Output is written to the servlet output stream.

param
finalLocation the location of the Velocity template
param
invocation an encapsulation of the action execution state.
throws
Exception if an error occurs when creating the Velocity context, loading or executing the template or writing output to the servlet response stream.

        prepareServletActionContext();
        ValueStack stack = ActionContext.getContext().getValueStack();

        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        JspFactory jspFactory = null;
        ServletContext servletContext = ServletActionContext
                .getServletContext();
        Servlet servlet = JspSupportServlet.jspSupportServlet;

        velocityManager.init(servletContext);

        boolean usedJspFactory = false;
        PageContext pageContext = (PageContext) ActionContext.getContext().get(
                ServletActionContext.PAGE_CONTEXT);

        if (pageContext == null && servlet != null) {
            jspFactory = JspFactory.getDefaultFactory();
            pageContext = jspFactory.getPageContext(servlet, request, response,
                    null, true, 8192, true);
            ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT,
                    pageContext);
            usedJspFactory = true;
        }

        try {
            String encoding = getEncoding(finalLocation);
            String contentType = getContentType(finalLocation);

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

            Template t = getTemplate(stack,
                    velocityManager.getVelocityEngine(), invocation,
                    finalLocation, encoding);

            Context context = createContext(velocityManager, stack, request,
                    response, finalLocation);
            Writer writer = new OutputStreamWriter(response.getOutputStream(),
                    encoding);

            response.setContentType(contentType);

            t.merge(context, writer);

            // always flush the writer (we used to only flush it if this was a
            // jspWriter, but someone asked
            // to do it all the time (WW-829). Since Velocity support is being
            // deprecated, we'll oblige :)
            writer.flush();
        } catch (Exception e) {
            log.error("Unable to render Velocity Template, '" + finalLocation
                    + "'", e);
            throw e;
        } finally {
            if (usedJspFactory) {
                jspFactory.releasePageContext(pageContext);
            }
        }

        return;
    
protected java.lang.StringgetContentType(java.lang.String templateLocation)
Retrieve the content type for this template.

People can override this method if they want to provide specific content types for specific templates (eg text/xml).

return
The content type associated with this template (default "text/html")

        return "text/html";
    
protected java.lang.StringgetEncoding(java.lang.String templateLocation)
Retrieve the encoding for this template.

People can override this method if they want to provide specific encodings for specific templates.

return
The encoding associated with this template (defaults to the value of 'struts.i18n.encoding' property)

        String encoding = defaultEncoding;
        if (encoding == null) {
            encoding = System.getProperty("file.encoding");
        }
        if (encoding == null) {
            encoding = "UTF-8";
        }
        return encoding;
    
protected org.apache.velocity.TemplategetTemplate(com.opensymphony.xwork2.util.ValueStack stack, org.apache.velocity.app.VelocityEngine velocity, com.opensymphony.xwork2.ActionInvocation invocation, java.lang.String location, java.lang.String encoding)
Given a value stack, a Velocity engine, and an action invocation, this method returns the appropriate Velocity template to render.

param
stack the value stack to resolve the location again (when parse equals true)
param
velocity the velocity engine to process the request against
param
invocation an encapsulation of the action execution state.
param
location the location of the template
param
encoding the charset encoding of the template
return
the template to render
throws
Exception when the requested template could not be found

        if (!location.startsWith("/")) {
            location = invocation.getProxy().getNamespace() + "/" + location;
        }

        Template template = velocity.getTemplate(location, encoding);

        return template;
    
private voidprepareServletActionContext()
Prepares the servlet action context for this request

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

        this.defaultEncoding = encoding;
    
public voidsetVelocityManager(org.apache.struts2.views.velocity.VelocityManager mgr)

        this.velocityManager = mgr;