| Method from org.apache.struts2.views.freemarker.FreemarkerResult Detail: |
protected TemplateModel createModel() throws TemplateModelException {
ServletContext servletContext = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ValueStack stack = ServletActionContext.getContext().getValueStack();
Object action = null;
if(invocation!= null ) action = invocation.getAction(); //Added for NullPointException
return freemarkerManager.buildTemplateModel(stack, action, servletContext, request, response, wrapper);
}
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
|
protected Locale deduceLocale() {
if (invocation.getAction() instanceof LocaleProvider) {
return ((LocaleProvider) invocation.getAction()).getLocale();
} else {
return configuration.getLocale();
}
}
Returns the locale used for the Configuration#getTemplate(String, Locale) call. The base implementation
simply returns the locale setting of the action (assuming the action implements LocaleProvider ) or, if
the action does not the configuration's locale is returned. Override this method to provide different behaviour, |
public void doExecute(String locationArg,
ActionInvocation invocation) throws IOException, TemplateException {
this.location = locationArg;
this.invocation = invocation;
this.configuration = getConfiguration();
this.wrapper = getObjectWrapper();
if (!locationArg.startsWith("/")) {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest req = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
String base = ResourceUtil.getResourceBase(req);
locationArg = base + "/" + locationArg;
}
Template template = configuration.getTemplate(locationArg, deduceLocale());
TemplateModel model = createModel();
// Give subclasses a chance to hook into preprocessing
if (preTemplateProcess(template, model)) {
try {
// Process the template
Writer writer = getWriter();
if (isWriteIfCompleted() || configuration.getTemplateExceptionHandler() == TemplateExceptionHandler.RETHROW_HANDLER) {
CharArrayWriter charArrayWriter = new CharArrayWriter();
try {
template.process(model, charArrayWriter);
charArrayWriter.flush();
charArrayWriter.writeTo(writer);
} finally {
if (charArrayWriter != null)
charArrayWriter.close();
}
} else {
template.process(model, writer);
}
} finally {
// Give subclasses a chance to hook into postprocessing
postTemplateProcess(template, model);
}
}
}
Execute this result, using the specified template locationArg.
The template locationArg 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 |
protected Configuration getConfiguration() throws TemplateException {
return freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
}
This method is called from #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.
|
public String getContentType() {
return pContentType;
}
allow parameterization of the contentType
the default being text/html |
protected ObjectWrapper getObjectWrapper() {
return configuration.getObjectWrapper();
}
|
protected Writer getWriter() throws IOException {
if(writer != null) {
return writer;
}
return ServletActionContext.getResponse().getWriter();
}
The default writer writes directly to the response writer. |
public boolean isWriteIfCompleted() {
return writeIfCompleted;
}
|
protected void postTemplateProcess(Template template,
TemplateModel data) throws IOException {
}
the default implementation of postTemplateProcess applies the contentType parameter |
protected boolean preTemplateProcess(Template template,
TemplateModel model) throws IOException {
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;
}
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 |
public void setContentType(String aContentType) {
pContentType = aContentType;
}
|
public void setFreemarkerManager(FreemarkerManager mgr) {
this.freemarkerManager = mgr;
}
|
public void setWriteIfCompleted(boolean writeIfCompleted) {
this.writeIfCompleted = writeIfCompleted;
}
Writes to the stream only when template processing completed successfully |
public void setWriter(Writer writer) {
this.writer = writer;
}
|