Hi Harsha,
On Mon, Feb 25, 2019 at 6:17 PM Harsha Thota <[email protected]> wrote:
>
> Hi,
>
> We have a jersey based web application. We use Apache Freemarker for the
> front end web pages. We integrate the Freemarker in our application using
> the jersey-mvc-freemarker. In our web app we register the
> FreeMarkerMvcFeature.class in the ResouceConfig on Startup. In our java
> code, we return a Viewable which gets rendered in the FTL.
>
> I have created a servlet Filter and added it in out web.xml. This filter
> intercepts all the http requests. In the doFilter method of that Filter, I
> am getting the HTTPSession from the HTTP request and setting an attribute
> value. Say "myVal".
>
> myVal should now be set on the HTTPSession in the HTTPServletRequest. I am
> not able to access this myVal in the FTL page. I tried doing a lot of
> research and also posted the question in stack overflow[
> https://stackoverflow.com/questions/54835683/freemarker-httpsession-attribute-returning-null]
> but I did not get any proper response.
How do you execute a Template? Do you invoke somewhere either
freemarker.template.Template.process(Object model, Writer), or
template.createProcessingEnvironment(Object model, Writer).process()?
If so, you need to pass all the model objects you want to use through
the model argument. Otherwise, the template cannot know what kind of
variables are available. You can use a map for the model argument:
Map<String, Object> context = new HashMap<String, Object>();
context.put("foo", new Foo());
Then you will be able to use ${foo.something}.
>
> I have tried to do the following:
>
> I have updated Freemarker to the latest version: 2.3.28
>
> I have also tried adding the servlet[
> https://freemarker.apache.org/docs/pgui_misc_servlet.html] mentioned in the
> Freemarker documentation to my web.xml and tried the following ways to
> access the myVal:
>
> ${Request.myVal}
>
> ${myVal}
Are you sure if your filter is invoked before the FreemarkerServlet
and if your filter sets "myVal" as a request attribute?
If so, ${myVal} should work fine as FreemarkerServlet will resolve it
from HttpServletRequest.getAttribute("myVal").
Regards,
Woonsan
>
> ${Request.Session.myVal}
>
> ${Session.myVal}
>
> All the above values are "null" and the below exception is thrown
>
> FreeMarker template error (DEBUG mode; use RETHROW in production!): The
> following has evaluated to null or missing: ==> Request [in template
> "home.ftl" at line 18, column 3]
>
> *---- Tip: If the failing expression is known to legally refer to something
> that's sometimes null or missing, either specify a default value like
> myOptionalVar!myDefault, or use <#if
> myOptionalVar??>when-present<#else>when-missing. (These only cover the last
> step of the expression; to cover the whole expression, use parenthesis:
> (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack
> trace ("~" means nesting-related): - Failed at:
> ${Request.Session.user_privilages} [in template "home.ftl" at line 18,
> column 1]*
>
> Please suggest on how I can access the http request in the FTL page. It
> would be a great help for my project.
>
> Please help. Thank you.
> Regards,
> Harsha Thota
> 1 (470)-535-2763 || [email protected]
> https://www.linkedin.com/in/thotaharsha