我正在使用JSF 2.0和Primefaces 3.4.2
我有两个p:selectOneMenu
,第一个父亲和第二个孩子,根据父值,子组件被填充。
父p:selectOneMenu
<p:selectOneMenu id="empl" value="#{empMB.employee}">
<f:selectItems value="#{empMB.employeeList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeNumber}"/>
<p:ajax update="department" />
</p:selectOneMenu>
儿童p:selectOneMenu
<p:selectOneMenu id="department" value="#{deptMB.department}">
<f:selectItems value="#{deptMB.loadDepartments(<??>)}" var="dept"
itemLabel="#{dept.departmentName}" itemValue="#{dept.departmentCode}"/>
</p:selectOneMenu>
我在部门ManagedBean中有一个名为loadDepartments的方法,其中包含一个参数
public void loadDepartments(String employeeNumber)
如何将值传递给子组件中的loadDepartments,以便它根据父组件中选择的代码加载所有部门?
如果我代替#{deptMB.loadDepartments(empMB.employee.employeeCode)}
我正在
Error Parsing: #{deptMB.loadDepartments({empMB.employee.employeeCode})}
任何帮助都非常值得赞赏?
答案 0 :(得分:4)
我会这样做:
这通常是我做这类事情的方式,它应该适合你。
修改强>
页面代码:
<p:selectOneMenu id="empl" value="#{empMB.employee}" converter="#{employeeConverter}">
<f:selectItems value="#{empMB.employeeList}" var="emp" itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeNumber}"/>
<p:ajax update="department" listener="#{empMB.onEmployeeSelect}" process="@this"/>
</p:selectOneMenu>
<p:selectOneMenu id="department" value="#{deptMB.department}" converter="#{departmentConverter}">
<f:selectItems value="#{empMB.departmentList}" var="dept" itemLabel="#{dept.departmentName}" itemValue="#{dept.departmentCode}"/>
</p:selectOneMenu>
bean的片段:
public class EmpMB{
...
private List<Department> departmentList;
private Employee employee;
public List getDepartmentList(){
return departmentList;
}
public void onEmployeeSelect(){
departmentList = someService.getDepartmentsForEmployee(employee);
}
...
}
转换器示例(请注意,它是一个弹簧组件,以便我可以将服务层注入其中,但您无需这样做):
@Component("employeeConverter")
public class EmployeeConverter implements Converter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
//TODO: implement this
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
//TODO: implement this
}
}