我是primefaces框架的初学者,我希望我的commandButton验证所选项是否为“All”以调用特定方法allBooks(),如果选择了另一个项目:调用另一个方法:loadBook()
<p:selectOneMenu value="#{bookBean.selectedBook.id}">
<f:selectItem itemLabel="Select a book :" itemValue="" />
<f:selectItem itemLabel="All" />
<f:selectItems value="#{bookBean.selectedBooksItems}" />
<p:ajax execute="bookSelect" event="change" listener="#{bookBean.loadBook}" />
</p:selectOneMenu>
<p:commandButton id="validate" action="#{bookBean.requestBook}" value="Validate"/>
答案 0 :(得分:1)
在actionListener方法
中执行此操作<p:selectOneMenu value="#{bookBean.selection}">
<f:selectItem itemLabel="Select a book :" itemValue="#{null}" />
<f:selectItem itemLabel="All" itemValue="#{'ALL'}" />
<f:selectItems value="#{bookBean.options}" />
<p:ajax/>
</p:selectOneMenu>
<p:commandButton actionListener="#{bookBean.loadButtonActionListener}" value="Load"/>
public void loadButtonActionListener(ActionEvent event){
if(this.selection.equals("ALL")) {
this.allBooks();
} else {
this.loadBook(this.selection);
}
}
答案 1 :(得分:0)
commandButton默认情况下是ajaxified,所以这个代码片段按预期工作:
<h:form>
<p:selectOneMenu value="#{myBean.selected}">
<f:selectItem itemLabel="ALL" itemValue="ALL" />
<f:selectItem itemLabel="NONE" itemValue="NONE" />
</p:selectOneMenu>
<p:commandButton value="Validate" actionListener="#{myBean.doAction}" />
</h:form>
这里是声明bean:
@Named
@RequestScoped
public class MyBean {
private String selected;
public MyBean() {
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public void doAction() {
if (selected.equals("ALL")) {
System.out.println("ALL Called!");
} else if (selected.equals("NONE")) {
System.out.println("NONE Called");
}
}
}
更新:
如果要将{ajax更改事件添加到selectOneMenu
,只需将此行嵌套在selectOneMenu
元素中
<p:ajax listener="#{myBean.doAction}" />