【发布时间】:2011-12-01 23:44:30
【问题描述】:
这是针对 JSF 2.0 的(请注意 - 这是 mojerra 实现,我没有使用 Icefaces、myfaces 等)
考虑一下,我的表单中只有一个下拉列表,并且下拉列表绑定了一个 SelectItems 对象列表,其中存储了值、标签和描述。
在我的 Value change actionlistener 事件中,如何访问选定的值、标签和描述。我只能访问选定的值吗?
示例代码-
在我的 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
}
}
请帮忙
【问题讨论】: