PortletVelocityResultpublic 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.Context | createContext(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.
return velocityManager.createContext(stack, request, response);
| public void | doExecute(java.lang.String location, com.opensymphony.xwork2.ActionInvocation invocation)
if (PortletActionContext.isEvent()) {
executeActionResult(location, invocation);
} else if (PortletActionContext.isRender()) {
executeRenderResult(location, invocation);
}
| private void | executeActionResult(java.lang.String location, com.opensymphony.xwork2.ActionInvocation invocation)Executes the result
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 void | executeRenderResult(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.
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.String | getContentType(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 "text/html";
| protected java.lang.String | getEncoding(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.
String encoding = defaultEncoding;
if (encoding == null) {
encoding = System.getProperty("file.encoding");
}
if (encoding == null) {
encoding = "UTF-8";
}
return encoding;
| protected org.apache.velocity.Template | getTemplate(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.
if (!location.startsWith("/")) {
location = invocation.getProxy().getNamespace() + "/" + location;
}
Template template = velocity.getTemplate(location, encoding);
return template;
| private void | prepareServletActionContext()Prepares the servlet action context for this request
PortletRequestDispatcher disp = PortletActionContext.getPortletConfig()
.getPortletContext().getNamedDispatcher("preparator");
disp.include(PortletActionContext.getRenderRequest(),
PortletActionContext.getRenderResponse());
| public void | setDefaultEncoding(java.lang.String encoding)
this.defaultEncoding = encoding;
| public void | setVelocityManager(org.apache.struts2.views.velocity.VelocityManager mgr)
this.velocityManager = mgr;
|
|