This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Accessing Attribute Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Accessing Attribute Values" Watch "Accessing Attribute Values" New topic
Author

Accessing Attribute Values

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Dec 11, 2004
Posts: 13410

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.



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Jan 10, 2002
Posts: 61310
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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

Joined: Dec 11, 2004
Posts: 13410

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 ]
 
GeeCON Prague 2014
 
subject: Accessing Attribute Values