Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何使用Omnifaces转换器将selectOneMenu中的对象保存到数据库中?_Jsf_Primefaces_Converter_Omnifaces - Fatal编程技术网

Jsf 如何使用Omnifaces转换器将selectOneMenu中的对象保存到数据库中?

Jsf 如何使用Omnifaces转换器将selectOneMenu中的对象保存到数据库中?,jsf,primefaces,converter,omnifaces,Jsf,Primefaces,Converter,Omnifaces,使用带有primefaces selectOneMenu的OmniFaces转换器时出现问题。我正在primefaces数据表中显示一个部门列表,其中一列显示部门名称,另一列显示教员名称。 编辑时,SelectOne菜单会正确显示要从中选择的学院列表,但在提交时不会保存。当我删除datatable中的faculty列时,部门名称会被保存,不会出现问题。有人帮我找出我无法保存faculty name的原因 这是我的数据表代码 <p:dataTable id="deptTable" var="

使用带有primefaces selectOneMenu的OmniFaces转换器时出现问题。我正在primefaces数据表中显示一个部门列表,其中一列显示部门名称,另一列显示教员名称。 编辑时,SelectOne菜单会正确显示要从中选择的学院列表,但在提交时不会保存。当我删除datatable中的faculty列时,部门名称会被保存,不会出现问题。有人帮我找出我无法保存faculty name的原因

这是我的数据表代码

<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>
下面是onEdit方法 public void onEdit(RowEditEvent事件){

下面是更新方法-使用hibernate save()方法

最后是我的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;
}

}问错了问题。JSF是一个表示框架,它不处理存储/业务逻辑

在支持bean中,appropiate方法应该负责存储,但它将忽略bean的来源。您可以手动(JDBC)或使用ORM框架(Hibernate,JPA)

顺便说一句,如果您的问题是“为什么所选对象没有存储在
部门MB.selectedFaculty
?”,那么转换器没有在
中实现
getAsObject()
,您需要在当前迭代的行上设置所选的faculty,而不是在父支持bean上

换言之,替换

<p:dataTable ... value="#{departmentMB.departmentList}" var="department">
    ...
    <p:selectOneMenu ... value="#{departmentMB.selectedFaculty}">

...


...

这个具体问题与转换器无关。它的工作做得很好。

谢谢你的回答。getAsObject()是在Omnifaces基础转换器SelectItemsConverter中实现的,我在转换器中扩展了该转换器。控制台中是否出现任何错误?如果这听起来很明显,很抱歉,但在调用update方法之前,您可能缺少一个
部门。setFaculty(selectedFaculty)
。@eljunior有一点,
实际上应该是
{department.faculty}
而不是
{departmentMB.selectedFaculty}
!试试看。@BalusC这就是我犯的错误,就像eljunior说的,这看起来很明显。我非常感谢你。。。阿桑提尼!
public void updateDepartment(Department department) {
    try {
        getDepartmentService().updateDepartment(department);

    } catch (DataAccessException e) {
        e.printStackTrace();
    }

}
@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;
}
<p:dataTable ... value="#{departmentMB.departmentList}" var="department">
    ...
    <p:selectOneMenu ... value="#{departmentMB.selectedFaculty}">
<p:dataTable ... value="#{departmentMB.departmentList}" var="department">
    ...
    <p:selectOneMenu ... value="#{department.faculty}">