如何访问jsf selectOneMenu中选定的标签和选定的描述,其中selectOneMenu与javax.faces.model.SelectItem对象绑定?

发布于 2024-12-08 13:24:02 字数 1300 浏览 0 评论 0原文

这是针对 JSF 2.0 的(请注意 - 这是 mojerra 实现,我没有使用 Icefaces、myfaces 等)。

考虑我的表单中只有一个下拉菜单,并且该下拉菜单与存储值的 SelectItems 对象列表绑定,标签和说明。

在我的值更改操作侦听器事件中,如何访问选定的值、标签和描述。我只能访问选定的值吗?

示例代码 -

在我的 xhtml 中 - 下拉菜单是 -

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

的名称 -

其中 person 是 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 and 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

    }
}   

请帮助

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 and 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别理我 2024-12-15 13:24:02

在我的值更改操作侦听器事件中,如何访问选定的值、标签和描述。我只能访问选定的值?

这实际上也只能由 HTML

@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);
        // ...
    }

}

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);
        // ...
    }

}
酷到爆炸 2024-12-15 13:24:02

您可以通过为它们添加变量并为变量创建 getter 和 setter 来实现这一点

在您的 bean 中,例如。对于在您的 bean 中创建的描述

  private String description;

  public String getDescription(){
       return description;
  }

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

以及类似的任何变量。

抱歉,我误解了

您可以做的

//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();

}
}

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();

}
}
夜空下最亮的亮点 2024-12-15 13:24:02

最好的方法是使用自定义对象作为 SelectItems 的值并为它们实现自定义转换器:
http://balusc.blogspot.com/2007/09/objects-in -hselectonemenu.html

在 JSF 2 中,您可以像这样轻松实现自定义转换器:
http://www.mkyong.com/jsf2/custom-转换器-in-jsf-2-0/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文