如何访问jsf selectOneMenu中选定的标签和选定的描述,其中selectOneMenu与javax.faces.model.SelectItem对象绑定?
这是针对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上也只能由 HTML
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
SelectItem
s yourself.您可以通过为它们添加变量并为变量创建 getter 和 setter 来实现这一点在您的 bean 中,例如。对于在您的 bean 中创建的描述
以及类似的任何变量。抱歉,我误解了
您可以做的
you can do that by having variables for them and creating getters and setters for the variablesin your bean for eg. for the description create in your bean
and similarly for any variable.Sorry i misunderstood
you can do
最好的方法是使用自定义对象作为 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/