开发者

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, and Application 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 write attrName, and if no such variable was created in the template, it will search the variable in Request, and then in Session, and finally in Application.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜