This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

map reference syntax ?  RSS feed

 
jack catler
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to display a simple name from an objects located in a map.
The map is in a session based object.

Neither of the statements below work.

1. <h utputText value="#{sessionObj.mymap[thekey][name]}" />

2. <h utputText value="#{sessionObj.mymap[thekey].name}" />

Am I even close?
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first off, since you're using JSF (this is the JSF forum, so I have to assume that!), you don't need to explicitly reference the session, just define a Managed Bean with session scope.

Secondly, the data structure reference you've put together seems more comples than the question seems to call for. Let's try it this way:

1. In faces-config.xml, define your collection with session scope. Let's call it "myMap". Let's say you've defined it as a java.util.HashMap. JSF will construct an empty map when you create the session. You can load key/value pairs into the map by declarations in faces-config, in bean backing logic, or in non-JSF code (or a combination of the above).

2. Now you want to display the value for a given key. Let's say the key is the constant value "Larry".



Or the variable named "Moe"



If this DOESN'T work, then it's because I'm occasionally confused myself, but I think I got that correct.

Here's a little secret: Both Struts and many JSF implementations handle such expressions using the jakarta beanutils library. So one of the best places to get detailed info is to look at the beanutils docs at http://jakarta.apache.org/commons/beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#package_description
 
jack catler
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Yes I am using JSF. I should clarify 'sessionObj' in the example is exactly that, a session bean, the data structure (rougly) is:

public class sessionObj {
private HashMap<String, myBean> mymap;
public HashMap<String, myBean> getMymap() {return mymap;}
....
}
public class myBean {
private String name;
public String getName() {return name;}
....
}

So now in the jsp file, I have the key I want for the hashmap entry, how do I go straight to it?

The example you give (the firt one that is) would return the object, but I want to go directly to the field within the object. I had a look at the link you gave, not really there.

Thanks!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#{sessionObj.mymap[thekey].name}

I think if "theKey" is a variable, this should work. If it *isn't*, but is the actual key, if I remember correctly you should use

#{sessionObj.mymap['thekey'].name}
 
jack catler
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it works. Actually the second example in my first post was working - same as your recommendation.

<h OutputText value="#{sessionObj.mymap[thekey].name}" />

..it was being obscured by the nature of some other output - and not enough coffee on my side....

Thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!