English 中文(简体)
How to get both label and value from f:selectItems
原标题:

I am working on a JSF page which has a dropdown based on List<SelectItem>:

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

I need to get both the value and label of the currently selected item. Right now I only get the value. How can I get the label, too?

最佳回答

You can t. That s just how HTML works. You know, JSF is a HTML code generator. The JSF <h:selectOneMenu> generates a HTML <select><option> . The HTML <select> element will only send the value attribute of the selected <option> element. It will not send its label.

But that shouldn t be a big issue. You namely already know both the value and label in the server side, inside the #{bean.availableItems}. All you need to do to get the associated label is to get it by the value as key. I suggest to make it a Map which in turn can also be used in f:selectItems.

Basic kickoff example:

public class Bean {
    private String selectedItem; // +getter +setter
    private Map<String, String> availableItems; // +getter

    public Bean() {
        availableItems = new LinkedHashMap<String, String>();
        availableItems.put("value1", "label1");
        availableItems.put("value2", "label2");
        availableItems.put("value3", "label3");
    }

    public void submit() {
        String selectedLabel = availableItems.get(selectedItem);
        // ...
    }
}

with

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

and in result

<p>Selected label is #{bean.availableItems[bean.selectedItem]}</p>

An alternative is to wrap both name and value in a javabean object representing an entity and set the whole object as value, via a converter.

See also:

问题回答

Instead of Using Map I tried like this and it s perfectly working for me to get both ItemValue and ItemLabel in the same property by using "ItemValue" attribute in selectItems tag.How ever provided no extra commas in the ItemLabel(@asfas....i had the same problem u mentioned so i selected this approach).

<h:selectOneMenu  value="#{company.issueDesc}"   required="true" onchange="submit()">
     <f:selectItem itemLabel="-- Select a Issue -- " itemValue="0"/>  
     <f:selectItems value="#{company.issueList}" var="model" itemLabel="#{model.IssueDesc}" itemValue="#{model.Code},#{model.IssueDesc}" >
      </f:selectItems>
</h:selectOneMenu>

Basically IssueDesc is String type in Bean Company

    public Class Company{

      private String issueDesc;    // getters and setters
      private int code;  // getters and setters
      private List<T>issueList; // getters and setters.


         public void getLblandVal(){

           String desc=getIssueDesc();
           String[] str_ary=desc.split(",");
           String s1=str_ary[0];
           String s2=str_ary[1];
          // parse **s1** to int;
    }


}

What if the the value should be Integer and label String and both are needed in backing bean. Using Map in bean doesn t work because JSF interprets the map key as label. Ideally it would be a LinkedHashMap and search the text by a number.

Seems upside down to search number (value) by a text (key). What if some implementation of JSF adds extra space to test or letter case changes for some reason. Then the value is not found from map.

This will do the trick.

private String getLabel(List<SelectItem> list, String selection) {
    String label = "";
    for (int i = 0; i < list.size(); i++) {
        if(selection.equals((String)list.get(i).getValue())) {
            label = (String)list.get(i).getLabel();
            break;
        }
    }

    return label;
}

In your example, you would pass in the availableItems as list and selectedItem as selection . This method will return the label value corresponding to the selectedItem.

The following approach may also be useful in getting value and label using List <SelectItem>:
Here, facade, statesFacade fetches list of states from database/enterprise bean.

In view (xhtml page):

    <h:selectOneMenu id="statesSelectUi" value="#{applicationBean1.state}">
        <f:selectItems value="#{applicationBean1.stateSelectItems}"/>
        <f:ajax render="selectedItem" event="change" execute="statesSelectUi"/>
    </h:selectOneMenu>
    <br/>
    <br/>
    <h:outputText id="selectedItem" value="#{applicationBean1.selectedState}"/>

In the Managed Bean(applicationBean1.java):

private String state;

/**
     * @return the stateSelectItems
     */
    public List<SelectItem> getStateSelectItemsItems() {
        stateSelectItems.add(new SelectItem("-1","--- Select state ---"));
        int statesCount = statesFacade.count();
        List<StateSelectItems> states;
        states = statesFacade.findAll();
        for (int i = 0; i < statesCount; i++) {
            stateSelectItems.add(new SelectItem(states.get(i).getStateSlNo(), states.get(i).getStateName()));
        }
        return stateSelectItems;
    }


       public String getSelectedState(){
       if("".equals(getState()) || getState() ==null){
        return "";
       }else{
        return "Selected State : " + getStateSelectItems.get(Integer.parseInt(getState())).getValue()+", "++ getStateSelectItems.get(Integer.parseInt(getState())).getLabel();
       }
    }




相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签