Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enum as a key in a Map in JSF  RSS feed

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Enum as a key in a map:



I want to refer to JSF:

nothing works:



#{myBean.myMap} shows me the whole map with the key correctly.

Enum as a key in JSF is not possible?
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't generally recommend getting creative with EL - it's more trouble than it's worth, and it effectively exposes internals of the backend which are generally better left abstract. So I usually make expressions like this be resolved by simple getproperty calls in the backing bean.

However, try this and see if it helps:

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, this does not work:

#{myBean.myMap[Me.one]} .

I have to change the type of the key in my map to a string and do something like one.name() to get the string value from a enum. However, it disturbs my type-safety. It s ugly that I cannot use enum keys in JSF for references..
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use enums. I have a project that uses them in droplists and it works just fine. However, I don't know if the EL parser you're using knows enough about enums to use them as constant key values in a map. I'm not even sure how it would be able to detect the difference between an enum's symbolic name and the name of a non-enum variable - which is why I suggested trying the Enum typename as a prefix.

You may just be ahead of your time.
 
Damian Harvey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've had the same problem. Looks like [] property accessor can't handle Enums.

I ended up putting a get() method on my Bean like so:

And as I'm using Jboss EL (which allows method parameters) I can call it from EL like so:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!