gpt4 book ai didi

jsf - 为 Primefaces 表创建编辑表单

转载 作者:行者123 更新时间:2023-12-04 22:09:16 25 4
gpt4 key购买 nike

我想为 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表格消失。可能是因为我渲染并执行它。如何在不关闭表单的情况下执行表单及其下方?

最佳答案

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

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

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

    CommandButton

    CommandButton extends the standard h:commandButton with ajax, partial processing and skinning features.



    所以 commandBotton 已经启用了 ajax 并且做了 不需要有一个
    <f:ajax render=":form" execute=":form">

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

    关于jsf - 为 Primefaces 表创建编辑表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990094/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com