Use of th:with to define global variable

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Use of th:with to define global variable

pavan
Hi,

I have to set a global variable on the page, which should be accessible through out the different parts of the page. I tried using the following
<div th:with="test= ${#httpServletRequest.getAttribute('test')}"></div>

and Using test variable in other div's. However, I realized the scope of th:with id limited to the dom element it is defined with.

Is there any way to define test globally on the page? so that i can do #httpServletRequest.getAttribute('test') only once and use it through out the page?.
Help on this is appreciated .


Thanks
Pavan Kumar
Reply | Threaded
Open this post in threaded view
|

Re: Use of th:with to define global variable

jjbenson
Hi Pavan,

  Have you tried attaching the th:with to the body element?

        <body th:with="test=${#httpServletRequest.getAttribute('test')}">

  Cheers,

   Jim
Reply | Threaded
Open this post in threaded view
|

Re: Use of th:with to define global variable

pavan
It Worked !!!

Thank you Jim.
Reply | Threaded
Open this post in threaded view
|

Re: Use of th:with to define global variable

Manoj
But how to access it in the template page
Reply | Threaded
Open this post in threaded view
|

Re: Use of th:with to define global variable

ravi.g.nihalani@gmail.com
I had the same issue, and the only quick way I could think of was to just pass the variables as a parameter to the called fragments.

<div th:replace="fragments :: my_fragment(content='a', defaultParameter='b')"></div>