开发者

How to access the selected label and selected description in jsf selectOneMenu where selectOneMenu is bound with javax.faces.model.SelectItem objects?

开发者 https://www.devze.com 2023-04-11 16:07 出处:网络
This is for JSF 2.0 (Kindly note - this is mojerra implementation and I am not using Icefaces, myfaces etc.)

This is for JSF 2.0 (Kindly note - this is mojerra implementation and I am not using Icefaces, myfaces etc.)

Consider I just have a drop down in my my form and the dropdown is bound with a List of SelectItems objects which stores value,label and description.

In my Value change actionlistener event how can I access the selected value,label and description. I am able to access only the selected value?

Sample code-

In my xhtml - the dropdown is -

<h:selectOneMenu onchange="submit()" valueChangeListener="#{person.changeDD}" value="#{person.selectedValue}">
    <f:selectItems value="#{person.lists}"></f:selectItems>
</h:selectOneMenu>

where person is the name of the bean

ModelBean-

@ManagedBean(name="person")
@SessionScoped
public class PersonBean implements Serializable{

private String selectedValue;

private List<SelectItem> lists=new ArrayList<SelectItem>(); 
public PersonBean() {
        lists=new ArrayList<SelectItem>();
        lists.add(new SelectItem("1","India","desc1"));
        lists.add(new SelectItem("2","canada","desc2"));
        lists.add(new SelectItem("3","america","desc3"));
    }
//getters a开发者_JS百科nd setters


public void changeDD(ValueChangeEvent vce) throws IOException{
        System.out.println("in value change");
        System.out.println("New value-->"+vce.getNewValue().toString());
//I have access only to the selected value and not to the description and label

    }
}   

Kindly help


In my Value change actionlistener event how can I access the selected value,label and description. I am able to access only the selected value?

That's indeed also only what's been sent by a HTML <select> element. You need to maintain a mapping of the available values and SelectItems yourself.

@ManagedBean(name="person")
@SessionScoped
public class PersonBean implements Serializable {

    private String selectedValue;
    private Map<String, SelectItem> availableValues;
    private List<SelectItem> lists; 

    public PersonBean() {
        availableValues = new LinkedHashMap<String, SelectItem>();
        availableValues.put("1", new SelectItem("1", "India", "desc1"));
        availableValues.put("2", new SelectItem("2", "canada", "desc2"));
        availableValues.put("3", new SelectItem("3", "america", "desc3"));
        lists = new ArrayList<SelectItem>(availableValues.values());
    }

    public void changeDD(ValueChangeEvent event) {
        String selectedValue = (String) event.getNewValue();
        SelectItem selectedItem = availableItems.get(newValue);
        // ...
    }

}


you can do that by having variables for them and creating getters and setters for the variables

in your bean for eg. for the description create in your bean

  private String description;

  public String getDescription(){
       return description;
  }

  public void setDescription(String desc){
       description = desc;
  }

and similarly for any variable.

Sorry i misunderstood

you can do

//String selectedvalue
SelectItem selectedValue;//create getters setter
public void changeDD(ValueChangeEvent vce) throws IOException{
    System.out.println("in value change");
    System.out.println("New value-->"+vce.getNewValue().toString());
    String description = selectedValue.getDescription();

}
}


Best way would be to use Custom objects as values for your SelectItems and implement Custom Converter for them:
http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html

In JSF 2 you can implement custom convertors easily like this:
http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号