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

Accessing Attribute Values

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a <c:forEach var="i" items="a map" ....> loop. The i variable is assigned a Map.Key type and I need to use ${i.value} to access its value. ${i.value} actually represents an object with .getAttribute("...") method - not a simple getter.

My question is what would be the best strategy to access this
.getAttribute("myattribute")? I cannot use ${i.value.myattribute} because getAttribute isn't a getter? Even if I use scriptlet, I still need a way to create a scriptlet variable representing $i.value}. Appreciate any suggestion.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the objects contained within your map, have proper getter methods for their properties, you can access them with the '.' syntax in EL.

In this case, my User objects have getters for firstName, lastName, etc.

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they don't have proper getter method. The method signature is getAttribute("abc") - actually the Map is holding a map of all sessions created by HttpSessionListener stored in application store. The proper getter recognised by EL . or [] operator must be getSomething() - without taking any parameter.

My problem is how to access some get methods that need to take parameters - like getAttribute("ss"). EL probably not suitable and perhaps scriptlet must be used. But my problem how do we create the scriptlet variable if we do need to use scriptlet? I can think of <c:set var="i"../> followed by <jsp:useBean id="i"...> but is there more elegant way to do this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than contorting your pages to try to conform to data structures that aren't EL-friendly, I strongly suggest that you modify the structures that you are passing to the pages to conform to EL-addressable patterns.

If the map values that you are referring to are classes that you are defining, then modify those classes. If they are 3rd-party or standard classes over which you have no control, then either copy the relevant values to conforming value objects, or use a conforming wrapper class to wrap the non-conforming class.

If that doesn't appeal to you for whatever reason, the next thing I'd recommend would be to write a custom tag that handles the interaction for you.

Last on the list would be resorting to scriptlets. But if you are going to go the scriplet route, then you might as well do the whole thing in scriptlets. Mixing and matching incompatible technologies on the page will just be a bit of a mess.

[ October 30, 2005: Message edited by: Bear Bibeault ]
[ October 30, 2005: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That all said, if you insist upon accessing scoped variables under scriptlet control, remember that the scoped variables are nothing more than what was previously thought of as "attributes" assigned to the various scoped contexts using setAttribute() and retrieved using getAttribute().
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alec Lee:
...actually the Map is holding a map of all sessions created by HttpSessionListener stored in application store....


This sounds very similar to the SessionMonitor project on http://simple.souther.us/not-so-simple.html.
Have a look and see if any of that code does what you're trying to do.
[ October 30, 2005: Message edited by: Ben Souther ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic