0

My bean has a map of Integers to Strings, I am trying to have a menu to select a value to be referred to by a certain key in the map.

My bean looks something like this:

public class Bean {
    Map<Integer, String> map;
    //...
}

and the relevant portion of my xhtml file looks something like this:

<h:outputLabel value="Selection for code 5" for="code5menu" />
<h:selectOneMenu id="code5menu" value="#{bean.map.get(5)}">
    <f:selectItem itemValue="good" itemLabel="good" />
    <f:selectItem itemValue="bad" itemLabel="bad" />
</h:selectOneMenu>

Is there a way to do this or something similar?

I can change the bean around some if need be. One thing to note is that the Integers aren't sequential so making a List would have lots of empty space, but if that is the only way I could still try to use a List (the Integer Key wouldn't be the index though). Maybe there's a way to do this with ajax? I looked into EL Functions but that didn't seem like what I needed.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Captain Man
  • 6,997
  • 6
  • 48
  • 74

1 Answers1

2

You should use Map<Long, String> instead of Map<Integer, String> as EL interprets literal number 0 as long type and it cannot be accessible. All extra details you can find here

To access Map element by key you using EL use:

#{bean.map[your-key]}
Community
  • 1
  • 1
njjnex
  • 1,490
  • 13
  • 23
  • Thanks! There will be no 0 in my particular case, is it okay to use Integer instead of Long then? – Captain Man Oct 24 '14 at 21:10
  • 1
    @CaptainMan any *integer* you write in Facelets will be considered as `Long`, so redeclare your map as `Map`. That's what this answer basically tells you. – Luiggi Mendoza Oct 24 '14 at 21:12
  • 1
    @CaptainMan If it's no way to change your Map you can use 'longToInteger()' inside your EL – njjnex Oct 24 '14 at 21:30