Getting request attributes in freemarker
How do I check a value from the request attribute in freemarker? I tried <#if *${RequestParameters['servicesettings']} ??开发者_如何学JAVA> but getting errors ->
Encountered "*" at line
Can anyone help?
It depends on the Web application framework, because FreeMarker itself doesn't expose the request parameters. (Well, except if the framework uses freemareker.ext.servlet.FreemarkerServlet
which is kind of an extension to FreeMarker.) Also, usually you shouldn't access request parameters directly from an MVC template, or anything that is HTTP/Servlet specific.
As of the error message, what you have written has a few syntax errors... probably you meant <#if RequestParameters.servicesettings??>
(it's not JSP - don't use ${...}
-s inside FreeMarker tags). This will require that you have RequestParameters
in the data-model, that I can't know for sure...
We should write like this:
${Request.requestattribute}
You can use
${requestParameters.servicesettings}.
According to the JavaDoc of the FreemarkerServlet
:
It makes all request, request parameters, session, and servlet context attributes available to templates through
Request
,RequestParameters
,Session
, andApplication
variables.The scope variables are also available via automatic scope discovery. That is, writing
Application.attrName
,Session.attrName
,Request.attrName
is not mandatory; it's enough to writeattrName
, and if no such variable was created in the template, it will search the variable inRequest
, and then inSession
, and finally inApplication
.
You can simply write:
${attrName}
to get the value of a request attribute (that you might have set in a servlet request filter using request.setAttribute('attrName', 'value')
Worked for me with Freemarker 2.3.27-incubating
精彩评论