Accessing request.setAttribute(0

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

Accessing request.setAttribute(0

Steven
Hi,

In a Java controller I have: request.setAttribute("userId", user.getId());

I'm now trying to access from Thymeleaf (as one would expect to do in JSP).

I've tried the following, as well as a few other examples, any ideas welcome.

id: ...  

Please note, I wish to put this value into a hidden, but as using a span just so I can 'eye-ball' it in the UI first.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing request.setAttribute(0

Steven
Seemed to loose the code, so: span th:text="${param.userId[0]}"  ... /span
Reply | Threaded
Open this post in threaded view
|

Re: Accessing request.setAttribute(0

Zemi
Administrator
Hello,

you can access the full HttpServletRequest object via the #httpServletRequest expression.

Anyway, modifying the request from your view is usually not considered a good practice.

Regards,
   Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Accessing request.setAttribute(0

Zemi
Administrator
Hello,

I think I was mislead by the title, if you just want to display the value use

   <span th:text="${userId}">...</span>

Regards,
   Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Accessing request.setAttribute(0

Steven
Thanks folks, to confirm..:

1) The value is added / written in the Java controller, ie: request.setAttribute("userId", authUser.getId());

2) and then read by the view / Thymeleaf: userId: span th:text="${param.userId[0]}" > ... /span

I'll give your suggestions a try.

Thanks !!!
Reply | Threaded
Open this post in threaded view
|

Re: Accessing request.setAttribute(0

Steven
Yea, tried these, not working, just cant seem to access the added attribute fro the view..:

            1__userId: span th:text="${param.userId[0]}" > ... /span
            2__userId: span th:text="${param.userId}" > ... /span
            3__userId: span th:text="${userId}">... /span  

This is soooooo simple in JSP, but seems impossible with Thymeleaf !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Aghhhhh !!!!!!!!!....

I see the use of httpServletRequest mentioned, but am unable to find reliable guidance.

Starting to miss good old JSP :(

...

One last Aghhhhh !!!!!!!!!....
Reply | Threaded
Open this post in threaded view
|

Re: Accessing request.setAttribute(0

Zemi
Administrator
Hi again,

any technology requires some time to learn it, you can start by looking at the main tutorial at http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html

If you feel more comfortable with JSP is a completely respectable choice to continue with it.

You display model attributes, like userId, using the expression
   <span th:text="${userId}">...</span>

If it is not displayed properly, maybe it is not set in the controller. Please review your controller code. If you are not using Spring, this is the simplest example of a controller
  https://github.com/thymeleaf/thymeleafexamples-gtvg/blob/2.1-master/src/main/java/thymeleafexamples/gtvg/web/controller/HomeController.java

Regards,
  Zemi