我在使用带有 primefaces selectOneMenu 的 OmniFaces 转换器时遇到问题。我在具有单元内编辑功能的 primefaces 数据表中显示部门列表,一列显示部门名称,而另一列显示教员名称。编辑时,selectOneMenu 正确显示了一个可供选择的学院列表,但在提交时不会保存,当我删除数据表中的学院列时,部门名称保存没有问题,有人帮我找出为什么我不能保存教师姓名。
这是我的数据表代码
<p:dataTable id="deptTable" var="department"
value="#{departmentMB.departmentList}" editable="true"
rowIndexVar="rowIndex">
<p:ajax event="rowEdit" listener="#{departmentView.onEdit}"
update=":deptForm:messages" />
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{department.departmentName}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{department.departmentName}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Faculty">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{department.faculty.facultyName}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu id="iterator"
value="#{departmentMB.selectedFaculty}"
converter="facultyConverter" label="Faculty">
<f:selectItem itemLabel="Select one" noSelectionOption="true" />
<f:selectItems value="#{facultyMB.facultyList}" var="faculty"
itemLabel="#{faculty.facultyName}" itemValue="#{faculty}" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<p:rowEditor />
</p:column>
</p:dataTable>
这是部门主管
@ManagedBean(name = "departmentMB")
@RequestScoped
public class DepartmentController implements Serializable {
@ManagedProperty(value = "#{DepartmentService}")
IDepartmentService departmentService;
private static final long serialVersionUID = 1L;
private int department_id;
private String departmentName;
private Faculty selectedFaculty;
.
.
.//getters and setters
这是 onEdit 方法 public void onEdit(RowEditEvent event) {
try {
Department department = (Department) event.getObject();
DepartmentController departmentController = (DepartmentController) FacesContext
.getCurrentInstance().getExternalContext().getRequestMap()
.get("departmentMB");
departmentController.updateDepartment(department);
} catch (Exception e) {
e.printStackTrace();
}
}
这是更新方法 - 使用休眠 save() 方法
public void updateDepartment(Department department) {
try {
getDepartmentService().updateDepartment(department);
} catch (DataAccessException e) {
e.printStackTrace();
}
}
最后是我的 OmniFaces 转换器
@FacesConverter("facultyConverter")
public class FacultyConverter extends SelectItemsConverter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Integer id = (value instanceof Faculty) ? ((Faculty) value).getFacultyId() : null;
return (id != null) ? String.valueOf(id) : null;
}
}