XHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>PickList Test</title>
</h:head>
<h:body>
<h:form id="form">
<p:pickList value="#{pickListBean.employeeList}" var="employee" itemLabel="#{employee.employeeName}" itemValue="#{employee.employeeCode}" />
<p:commandButton value="Save" action="#{pickListBean.message}" style="margin-left: 12px;"/>
</h:form>
</h:body>
</html>
豆
@ManagedBean
@RequestScoped
public class PickListBean {
@EJB
private BussinessList bl = new BussinessList();
private DualListModel<Employee> employeeList;
private Employee employee;
/**
* Creates a new instance of PickListBean
*/
public PickListBean() {
List<Employee> source = new ArrayList<Employee>();
List<Employee> target = new ArrayList<Employee>();
source = bl.getEmployee();
employeeList = new DualListModel<Employee>(source, target);
}
public void message(){
System.out.println("CommandButton is working");
}
public DualListModel<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(DualListModel<Employee> employeeList) {
this.employeeList = employeeList;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee= employee;
}
}
当我在命令按钮中单击时,不会调用message方法,但是当我从xhtml中删除选项列表时,commandbutton会调用message方法。
我正在使用jsf 2.2,primefaces 4.0 ...
答案 0 :(得分:0)
此代码上有很多错误
在开始出错之前,我可以在名为getEmployeePickList的方法中看到2个严重不良的命名选择
现在,您的xhtml可能会遗漏声明中的内容。我尝试了你的代码,即使我删除了pickList,你的命令按钮也不会触发。尝试从此开始(删除xml声明)
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
此外,Employee Class不是String,因此如果没有转换器,pickList将无法运行。请参阅example
最后,您的PickList将在您的类中搜索setEmployeePickList()方法。我尝试使用你的pickList使用String而不是Employee来解决转换器问题,当然,我收到了这个错误:
javax.el.PropertyNotFoundException: test.xhtml @21,140 value="#{pickListBean.employeePickList}": Property 'employeePickList' not writable on type org.primefaces.model.DualListModel
对此的解决方案是,使用#{pickListBean.employeeList}而不是#{pickListBean.employeePickList}来提供pickList。您可以在其他地方填充Employee List,例如在PickListBean构造函数中,而不是在getEmployeePickList中重新创建。