为Primefaces表创建编辑表单

时间:2017-07-08 19:37:48

标签: jsf primefaces jsf-2

我想为Primefaces表创建编辑表单。我测试了这段代码:

<h:form id="form">
    <p:dataTable id="tbl" var="systemuser" value="#{systemusers.systemUsers}"                                 
                 selectionMode="single" selection="#{systemusers.selectedSystemUser}" rowKey="#{systemuser.username}" lazy="true"
                 resizableColumns="true"
                 >

        <p:ajax event="rowSelect" listener="#{systemusers.onRowSelect}" update=":form:carDetail" oncomplete="PF('carDialog').show()" />
        ....................
    </p:dataTable>

    <p:dialog id="wd" header="System User Details" widgetVar="carDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="true">
        <p:outputPanel id="carDetail" style="text-align:center;">
            <p:panelGrid  columns="2"  columnClasses="label,value">

                <h:outputLabel for="id" value="ID" />
                <p:outputLabel id="id" value="#{systemusers.selectedSystemUser.id}" rendered="#{not systemusers.editable}"/>
                <h:inputText value="#{systemusers.selectedSystemUser.id}" rendered="#{systemusers.editable}" />
                ........
            </p:panelGrid>
        </p:outputPanel>

        <f:facet name="footer">
            <p:commandButton value="Edit System User" rendered="#{not systemusers.editable}"
                             actionListener="#{systemusers.editRecord(false)}">
                <f:ajax render=":form:wd" execute=":form:wd"></f:ajax>
            </p:commandButton>&nbsp;

            <p:commandButton value="Cancel" rendered="#{systemusers.editable}" actionListener="#{systemusers.editRecord(true)}">
                <f:ajax render=":form" execute=":form"></f:ajax>
            </p:commandButton>
        </f:facet>
    </p:dialog>

    <p:contextMenu for="tbl">
        <p:menuitem value="View" update="carDetail" icon="fa fa-search" oncomplete="PF('carDialog').show()"/>
        <p:menuitem value="Delete" update="tbl" icon="fa fa-remove" actionListener="#{systemusers.deleteSystemUser}">
            <p:confirm header="Confirmation" message="Are you sure?" icon="fa fa-warning" />
        </p:menuitem>
    </p:contextMenu>

    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="fa fa-check" />
        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="fa fa-remove" />
    </p:confirmDialog>
</h:form>

管理bean:

@Named
@RequestScoped
public class Systemusers implements Serializable
{
.......
public void editRecord(boolean editable)
    {
        SessionFactory factory = HibernateUtils.getSessionFactory();
        Session session = factory.getCurrentSession();

        try
        {
            session.getTransaction().begin();

            SystemUsersModel obj = new SystemUsersModel();
            obj.setId(selectedSystemUser.getId());
            ..........
            session.update(obj);

            session.getTransaction().commit();

            this.editable = editable;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }
......
private boolean editable = false;

    public boolean isEditable()
    {
        return editable;
    }

    public void setEditable(boolean editable)
    {
        this.editable = editable;
    }
}

当我打开编辑表单并单击按钮Edit System User时,表单消失。可能是因为我渲染并执行它。如何在不关闭的情况下执行表单并在其下执行?

1 个答案:

答案 0 :(得分:1)

示例代码在很多方面都很糟糕。有两点突出:

  • 这不是[mcve]
  • <f:ajax render=":form" execute=":form">是多余的,甚至可能导致奇怪的行为

OP很可能不知道(引自PrimeFaces showcase,强调我的)

  

命令按钮

     

CommandButton使用ajax ,部分处理和蒙皮功能扩展了标准h:commandButton

所以commandBotton已经是ajax anabled并且不需要来hava a

<f:ajax render=":form" execute=":form">
在里面。这(很可能)使更新运行两次。一个@none(Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes)和一个更新表单。第二个最有可能没有任何内容。但尝试(和调试)将使事情变得清晰。如果这不是答案,那么原因就不能在代码中看到,因此应该是[mcve]