WW-3329 ${Parameters.xxx} in freemarker view is broken

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@899763 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Musachy Barroso
2010-01-15 19:16:09 +00:00
parent 430b7aa22a
commit 053dfd2b05
2 changed files with 32 additions and 93 deletions
@@ -99,11 +99,11 @@ import freemarker.template.utility.StringUtil;
public class FreemarkerManager {
// coppied from freemarker servlet - so that there is no dependency on it
private static final String INITPARAM_TEMPLATE_PATH = "TemplatePath";
private static final String INITPARAM_NOCACHE = "NoCache";
private static final String INITPARAM_CONTENT_TYPE = "ContentType";
private static final String DEFAULT_CONTENT_TYPE = "text/html";
private static final String INITPARAM_DEBUG = "Debug";
public static final String INITPARAM_TEMPLATE_PATH = "TemplatePath";
public static final String INITPARAM_NOCACHE = "NoCache";
public static final String INITPARAM_CONTENT_TYPE = "ContentType";
public static final String DEFAULT_CONTENT_TYPE = "text/html";
public static final String INITPARAM_DEBUG = "Debug";
public static final String KEY_REQUEST = "Request";
public static final String KEY_INCLUDE = "include_page";
@@ -115,16 +115,21 @@ public class FreemarkerManager {
public static final String KEY_JSP_TAGLIBS = "JspTaglibs";
// Note these names start with dot, so they're essentially invisible from a freemarker script.
public static final String ATTR_TEMPLATE_MODEL = ".freemarker.TemplateModel"; // template model stored in request for siteMesh
private static final String ATTR_REQUEST_MODEL = ".freemarker.Request";
private static final String ATTR_REQUEST_PARAMETERS_MODEL = ".freemarker.RequestParameters";
private static final String ATTR_SESSION_MODEL = ".freemarker.Session";
private static final String ATTR_APPLICATION_MODEL = ".freemarker.Application";
private static final String ATTR_JSP_TAGLIBS_MODEL = ".freemarker.JspTaglibs";
// for sitemesh
public static final String ATTR_TEMPLATE_MODEL = ".freemarker.TemplateModel"; // template model stored in request for siteMesh
// for Struts
public static final String KEY_REQUEST_PARAMETERS_STRUTS = "Parameters";
public static final String KEY_HASHMODEL_PRIVATE = "__FreeMarkerManager.Request__";
private static final String EXPIRATION_DATE;
public static final String EXPIRATION_DATE;
/**
* Adds individual settings.
@@ -305,32 +310,36 @@ public class FreemarkerManager {
protected ScopesHashModel buildScopesHashModel(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, ObjectWrapper wrapper, ValueStack stack) {
ScopesHashModel model = new ScopesHashModel(wrapper, servletContext, request, stack);
// Create hash model wrapper for servlet context (the application)
// only need one thread to do this once, per servlet context
// Create hash model wrapper for servlet context (the application). We need one thread, once per servlet context
synchronized (servletContext) {
ServletContextHashModel servletContextModel = (ServletContextHashModel) servletContext.getAttribute(ATTR_APPLICATION_MODEL);
if (servletContextModel == null) {
// first try a JSP support servlet. If it fails, default to the servlet.
GenericServlet servlet = JspSupportServlet.jspSupportServlet;
// TODO if the jsp support servlet isn't load-on-startup then it won't exist
// if it hasn't been accessed, and a JSP page is accessed
if (servlet != null) {
servletContextModel = new ServletContextHashModel(servlet, wrapper);
servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
TaglibFactory taglibs = new TaglibFactory(servletContext);
servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
} else {
servletContextModel = new ServletContextHashModel(servletContext, wrapper);
servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
}
TaglibFactory taglibs = new TaglibFactory(servletContext);
servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
}
model.put(KEY_APPLICATION, servletContextModel);
model.put(KEY_JSP_TAGLIBS, (TemplateModel) servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL));
model.putUnlistedModel(KEY_APPLICATION_PRIVATE, servletContextModel);
}
model.put(KEY_JSP_TAGLIBS, (TemplateModel) servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL));
// Create hash model wrapper for session
HttpSession session = request.getSession(false);
HttpSessionHashModel sessionModel;
if (session != null) {
sessionModel = (HttpSessionHashModel) session.getAttribute(ATTR_SESSION_MODEL);
if (sessionModel == null) {
sessionModel = new HttpSessionHashModel(session, wrapper);
session.setAttribute(ATTR_SESSION_MODEL, sessionModel);
}
model.put(KEY_SESSION, new HttpSessionHashModel(session, wrapper));
} else {
// no session means no attributes ???
@@ -355,6 +364,7 @@ public class FreemarkerManager {
request.setAttribute(ATTR_REQUEST_PARAMETERS_MODEL, reqParametersModel);
}
model.put(ATTR_REQUEST_PARAMETERS_MODEL, reqParametersModel);
model.put(KEY_REQUEST_PARAMETERS_STRUTS,reqParametersModel);
return model;
}
@@ -66,26 +66,8 @@ public class FreemarkerDecoratorServlet extends freemarker.ext.servlet.Freemarke
public static final long serialVersionUID = -2440216393145762479L;
private static final String INITPARAM_TEMPLATE_PATH = "TemplatePath";
public static final String KEY_REQUEST = "Request";
public static final String KEY_INCLUDE = "include_page";
public static final String KEY_REQUEST_PRIVATE = "__FreeMarkerServlet.Request__";
public static final String KEY_REQUEST_PARAMETERS = "RequestParameters";
public static final String KEY_SESSION = "Session";
public static final String KEY_APPLICATION = "Application";
public static final String KEY_APPLICATION_PRIVATE = "__FreeMarkerServlet.Application__";
public static final String KEY_JSP_TAGLIBS = "JspTaglibs";
private static final String ATTR_REQUEST_PARAMETERS_MODEL = "Parameters";
// Note these names start with dot, so they're essentially invisible from
// a freemarker script.
private static final String ATTR_REQUEST_MODEL = ".freemarker.Request";
private static final String ATTR_SESSION_MODEL = ".freemarker.Session";
private static final String ATTR_APPLICATION_MODEL = ".freemarker.Application";
private static final String ATTR_JSP_TAGLIBS_MODEL = ".freemarker.JspTaglibs";
/*
private static final String EXPIRATION_DATE;
static {
@@ -95,7 +77,7 @@ public class FreemarkerDecoratorServlet extends freemarker.ext.servlet.Freemarke
SimpleDateFormat httpDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", java.util.Locale.US);
EXPIRATION_DATE = httpDate.format(expiration.getTime());
}
*/
protected String templatePath;
protected boolean nocache;
protected boolean debug;
@@ -132,7 +114,7 @@ public class FreemarkerDecoratorServlet extends freemarker.ext.servlet.Freemarke
}
// template path is already handled!
if (!INITPARAM_TEMPLATE_PATH.equals(name)) freemarkerManager.addSetting(name, value);
if (!FreemarkerManager.INITPARAM_TEMPLATE_PATH.equals(name)) freemarkerManager.addSetting(name, value);
}
nocache = freemarkerManager.getNocache();
debug = freemarkerManager.getDebug();
@@ -195,8 +177,6 @@ public class FreemarkerDecoratorServlet extends freemarker.ext.servlet.Freemarke
model = freemarkerManager.buildTemplateModel(ctx.getValueStack(), ctx.getActionInvocation().getAction(), servletContext, request, response, wrapper);
}
populateModel(model, wrapper, servletContext, request, response);
// Give subclasses a chance to hook into preprocessing
if (preTemplateProcess(request, response, template, model)) {
try {
@@ -352,58 +332,7 @@ public class FreemarkerDecoratorServlet extends freemarker.ext.servlet.Freemarke
// HTTP/1.0
res.setHeader("Pragma", "no-cache");
// Last resort for those that ignore all of the above
res.setHeader("Expires", EXPIRATION_DATE);
}
}
protected ScopesHashModel populateModel(ScopesHashModel params, ObjectWrapper wrapper, ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response) throws TemplateModelException {
try {
// Create hash model wrapper for servlet context (the application)
ServletContextHashModel servletContextModel = (ServletContextHashModel) servletContext.getAttribute(ATTR_APPLICATION_MODEL);
if (servletContextModel == null) {
servletContextModel = new ServletContextHashModel(this, wrapper);
servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
TaglibFactory taglibs = new TaglibFactory(servletContext);
servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
initializeServletContext(request, response);
}
params.putUnlistedModel(KEY_APPLICATION, servletContextModel);
params.putUnlistedModel(KEY_APPLICATION_PRIVATE, servletContextModel);
params.putUnlistedModel(KEY_JSP_TAGLIBS, (TemplateModel) servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL));
// Create hash model wrapper for session
HttpSessionHashModel sessionModel;
HttpSession session = request.getSession(false);
if (session != null) {
sessionModel = (HttpSessionHashModel) session.getAttribute(ATTR_SESSION_MODEL);
if (sessionModel == null) {
sessionModel = new HttpSessionHashModel(session, wrapper);
session.setAttribute(ATTR_SESSION_MODEL, sessionModel);
initializeSession(request, response);
}
} else {
sessionModel = new HttpSessionHashModel(this, request, response, wrapper);
}
params.putUnlistedModel(KEY_SESSION, sessionModel);
// Create hash model wrapper for request
HttpRequestHashModel requestModel = (HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL);
if (requestModel == null || requestModel.getRequest() != request) {
requestModel = new HttpRequestHashModel(request, response, wrapper);
request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
request.setAttribute(ATTR_REQUEST_PARAMETERS_MODEL, createRequestParametersHashModel(request));
}
params.putUnlistedModel(KEY_REQUEST, requestModel);
params.putUnlistedModel(KEY_INCLUDE, new IncludePage(request, response));
params.putUnlistedModel(KEY_REQUEST_PRIVATE, requestModel);
// Create hash model wrapper for request parameters
HttpRequestParametersHashModel requestParametersModel = (HttpRequestParametersHashModel) request.getAttribute(ATTR_REQUEST_PARAMETERS_MODEL);
params.putUnlistedModel(KEY_REQUEST_PARAMETERS, requestParametersModel);
return params;
} catch (ServletException e) {
throw new TemplateModelException(e);
} catch (IOException e) {
throw new TemplateModelException(e);
res.setHeader("Expires", freemarkerManager.EXPIRATION_DATE);
}
}
}