开发者

Getting request attributes in freemarker

开发者 https://www.devze.com 2023-04-06 04:44 出处:网络
How do I check a value from the request attribute in freemarker? I tried <#if *${RequestParameters[\'servicesettings\']} ??开发者_如何学JAVA> but getting errors ->

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

精彩评论

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

关注公众号