Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

c:out doesn't print values as expected

 
L Duperval
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm having trouble getting attributes to be printed properly using using JSTL tags. I did a small test, which looks like this:

The resulting HTML is:

What is wrong with my setup (or my understanding)?
L
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"session" is not one of the JSTL implicit objects. Is it something that you have set up prior to the code snippet shown?
 
L Duperval
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Actually, I wanted the names of all the attributes defined in the session. Evidently not the way to do it.
L
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. session is an implicit scripting variable, and the JSTL has no access to scripting variables. Remember, the whole purpose of JSTL is to minimize/eliminate on-page scripting.
JSTL's variable focus is on "scoped variables" -- attributes placed on the page, request, session and application contexts. As such, JSTL is most useful in an environment where the JSP pages have controllers that set things up -- including scoped variables -- on behalf of the page. It takes a little bit of turning your thinking around from the days when scriptlets were the norm, but once you've adjusted your thinking, it's amazing how much simpler doing things the JSTL way makes your life and the JSP pages.
For your example, the controller could set up a collection/array of the names it read from the session to use with the forEach action.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a slightly more interesting example: let's say that your page wanted to show both the attribute names, as well as their values. The controller would set up a Map of the name value pairs -- let's say as request attribute "sessionAttributes":

then on-page:
 
L Duperval
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! That clears up a few things. Let me update my code and try again.
Thanks!
L
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic