ServletContext servletContext = (ServletContext) context.get(ServletActionContext.SERVLET_CONTEXT);
HttpServletRequest req = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse res = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
// prepare freemarker
Configuration config = freemarkerManager.getConfiguration(servletContext);
// get the list of templates we can use
List<Template> templates = templateContext.getTemplate().getPossibleTemplates(this);
// find the right template
freemarker.template.Template template = null;
String templateName = null;
Exception exception = null;
for (Template t : templates) {
templateName = getFinalTemplateName(t);
try {
// try to load, and if it works, stop at the first one
template = config.getTemplate(templateName);
break;
} catch (ParseException e) {
// template was found but was invalid - always report this.
exception = e;
break;
} catch (IOException e) {
// FileNotFoundException is anticipated - report the first IOException if no template found
if (exception == null) {
exception = e;
}
}
}
if (template == null) {
if (LOG.isErrorEnabled()) {
LOG.error("Could not load the FreeMarker template named '" + templateContext.getTemplate().getName() +"':");
for (Template t : templates) {
LOG.error("Attempted: " + getFinalTemplateName(t));
}
LOG.error("The TemplateLoader provided by the FreeMarker Configuration was a: "+config.getTemplateLoader().getClass().getName());
}
if (exception != null) {
throw exception;
} else {
return;
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Rendering template " + templateName);
}
ActionInvocation ai = ActionContext.getContext().getActionInvocation();
Object action = (ai == null) ? null : ai.getAction();
SimpleHash model = freemarkerManager.buildTemplateModel(stack, action, servletContext, req, res, config.getObjectWrapper());
model.put("tag", templateContext.getTag());
model.put("themeProperties", getThemeProps(templateContext.getTemplate()));
// the BodyContent JSP writer doesn't like it when FM flushes automatically --