Getting request attributes in freemarker - freemarker

How do I check a value from the request attribute in freemarker?
I tried <#if *${RequestParameters['servicesettings']} ??> 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

Related

Oracle ORDS REST modules - optional parameters in handler

To create a handler, it seems that I would need to create a template with a URI template to create bindings.
However, the bindings seem to me only possible as a path structure (e.g. /:id/:records/:department) instead of being searchParams (?id=1&department=IT)
How could I allow optional parameters in the URL for handlers?
You can access search params variables just as you would access it in the URL template. There's no need to strictly define all possible input parameters in the URL template.
You can use binds or parameters, or even a mix.
I have full code examples here.
Run the sql's...
Confirm your APIs have been created, and call the API...
In this case a
GET http://localhost:8080/ords/hr/parameters/headers-classic?id=4

Access request parameters in Freemarker templates with JForum

I'm using JForum and I wanted to access request parameters in Freemarker template files.
I googled a bit and tried the followings without any luck.
${RequestParameters.paramName}
${RequestParameters['paramName']}
${args['paramName']}
${param.paramName}
They all return RequestParameters is undefined or args is undefined sort of errors.
I saw some questions like "How do you access request parameters in Freemarker templates using Spring MVC?" but I could not get help from those questions and answers.
Followup
So as far as I've found out, Freemarker does not reveal those information. You need to add those info by yourself.
I got some help from javaranch.com. Here is the link, if it would help anyone who's asking the same question: Access request parameters in Freemarker templates.
Maybe JForum doesn't expose the request parameters at all. FreeMarker is not like JSP, it's not bound to Servlet-s. The library/framework that embeds FreeMarker decides if it will expose the request (and the session, etc.), and how. Cleaner applications won't do that. It breaks the separation of concerns (and pure MVC).

Easy way for no-content without controller

I am using Spring Security with Spring Controllers. There are some weird requests caused by some third party browser extension ending such as undefined or weird hexadecimal numbers. I would like to configure my application to block these requests but I could not find an easy way.
I do not want to declare a empty controller for this purpose. What is the correct way to return no-content for these requests?
Edit:
Some sample requets:
/activity/favorites/undefined
/activity/favorites/my/undefined
/help/undefined
Create some servlet filter that is invoked for every request (*). That filter has to check the request URL, and if it is on of the "strangers" that return what you want, but prevent the request from being future processed.

Redirecting back to Portlet from ResourceMapping in Spring 3 portlets

I am trying to work out a way to provide a CSV download through a Spring 3 Portlet. I have a method that uses the #ResourceMapping annotation to define a handler that takes some report params in the form of a #ModelAttribute, builds the report, and returns it. The catch-22 I am running into is validating the parameters being send in from the client form.
If I make the handler a #ResourceMapping, I can set the headers and write out the report as using the ResourceResponse, but I can't seem to figure out how to redirect the user back to the Portlet view with errors when their input fails validation. However, if I make it an #ActionMapping, I can then check the BindingResults and forward them back to the form as needed, but the ActionResponse doesn't allow me to set the Content-Disposition header nor write out the CSV bytes, which is sort of critical for sending the report back.
I am at a total loss here, as I don't even know what my options are. Is it even possible to do what I am trying to do with a Portlet? Are there other examples I could look at for a possible work-around?
I suggest you to use both #ActionMapping and #ResourceMapping to fulfill your requirement.
As you said you were able to handle the validation errors using the #ActionResponse, I'll tell you how to handle the Resource Streaming.
As you know every #ActionResponse is followed by a #RenderResponse, just return the same view but, with a hidden iframe this time whose src points to the ResourceURL.
Now the Request you receive in #ResourceMapping is something which is already Validated. So, you can now serve your CSV.
I dont know how complex is your UI and if you are using jsp as views in your application. If nicely managed, Validation can be handled by #ResourceMapping.
Thank you

Access request parameters from a JSP View in Spring Web MVC without putting them in a model

I'd like to be able to access some HTTP GET parameters directly in a JSP, without having to pass them through a Controller+Model, but at the same time still use the dispatcher/controller/model/view mechanism for other parameters and logic.
This is because I have many HTTP GET parameters that are generated by Javascript and used also only in Javascript. My Controllers don't need them at all.
I tried ${arg}, ${request.arg}, ${requestScope.arg}, nothing seems to work.
If I bypass the dispatcher, ${requestScope.arg} works.
But is there a way to make it work with the dispatcher?
Thanks!
If that's request parameters that you want to access (and not request attributes like the title says), then the syntax is ${param.parameterName}.
If it's request attributes, then it's ${requestScope.attributeName}.
See http://java.sun.com/products/jsp/syntax/2.0/syntaxref207.html#1010522 for a quick reference.

Resources