0

I'm using h:selectOneMenu and I want to get not the ID value, but the label. In the backing bean I create SelectItem objects which are taken for loading the h:selectOneMenu.

new SelectItem("id", "label");

My view code:

<h:selectOneMenu value="#{Metadata.thema}">
    <f:selectItems value="#{ThemaBean.themes}" /> 
    <f:valueChangeListener type="com.schober.events.SelectThemaEvent" />
</h:selectOneMenu>

The code here sets Metadata.thema with the "id", but I need to set the "label". I tried with label="#{Metadata.thema}" but it does not work for me.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
adgfs
  • 423
  • 6
  • 17

1 Answers1

2

Then just use the label as value. Use the SelectItem constructor taking a single argument instead:

new SelectItem("label");

This way the label will be used as both item value and item label.


Update you seem to have misphrased the question and actually want to get both. In that case, just hold a Map of ID-label value pairs yourself and get the label from the map by the selected ID.

private Map<Long, String> themaIdsAndLabels = new HashMap<Long, String>();

// ...

public void submit() {
    String themaLabel = themaIdsAndLabels.get(thema);
    // ...
}

You can reuse this Map to generate list of SelectItems or even more, if you're using JSF 2.0 and EL 2.2 you can also use that map straight inside <f:selectItems> without the need to copy it into List<SelectItem>.

<f:selectItems value="#{bean.themaIdsAndLabels.entrySet()}" var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />

Or if your sole intent is to redisplay the label in an output text, you can also just use

<h:outputText value="#{bean.themaIdsAndLabels[bean.thema]}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok, but is there any way to get and both, id and label? for example.. ? – adgfs Sep 01 '11 at 13:00
  • You told you didn't want to get the ID? Anyway, I updated the answer. – BalusC Sep 01 '11 at 13:00
  • Could you provide me some code example because it's not very clear to me. Thanks – adgfs Sep 01 '11 at 13:05
  • BalusC, i've tried your code but the selectOneMenu stay empty, any ideas ? – adgfs Sep 01 '11 at 14:15
  • Perhaps you need to fill the map first? – BalusC Sep 01 '11 at 14:21
  • What approach exactly are you using now? You are not entirely clear in your current problem. – BalusC Sep 01 '11 at 14:26
  • As you told me, in the backing bean i added private Map map = new HashMap(); and in the jsf i put , the map is filling up correctly. – adgfs Sep 01 '11 at 14:34
  • Didn't you see the parentheses in `.entrySet()` in my example? If you edited it away because you got an error, then it simply means that your container doesn't support EL 2.2 or that your web.xml is not declared conform Servlet 3.0. The `Map` interface has no `getEntrySet()` method, that's why you see nothing when using `.entrySet`. – BalusC Sep 01 '11 at 14:36
  • Yes, i got an error when i had and parentheses in .entrySet(). – adgfs Sep 01 '11 at 14:46
  • Then stick to copying the `Map` to `List` or retarget your project to a Servlet 3.0 compatible container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) and redeclare web.xml as such. – BalusC Sep 01 '11 at 14:48
  • I m with tomcat 6 but updated el-api-2.2 and el-impl-2.2 but still have the same error when i put entrySet(); -> The function entrySet must be used with a prefix when a default namespace is not specified – adgfs Sep 01 '11 at 15:22
  • That's indeed not going to work. You need a Servlet 3.0 compatible container in order to use EL 2.2. If you're using Tomcat 6 and Mojarra 2.x, then you could use JBoss EL. http://stackoverflow.com/questions/3284236/jsf-2-0-method-invocation Don't forget to cleanup your classpath! Those EL 2.2 libraries conflict with Tomcat 6's own EL 2.1 libraries. – BalusC Sep 01 '11 at 15:40