创建主 - 详细信息表和对话框,如何重用相同的对话框进行创建和编辑

klo*_*log 3 jsf dialog master-detail primefaces

我正在尝试创建一个对话框,用于创建对象和更新对象.因此,如果我碰巧点击"新建"按钮,我将看到一个包含要填充的空字段的对话框,或者如果我单击某个条目的编辑按钮,该条目的数据将显示在对话框中以进行更新.

按照版本5.2的primefaces展示中的示例,我可以以只读的outputText形式呈现数据,但是当我将其更改为inputText时,该字段保持为空.以下代码是我的例子:

<h:form id="form">    
  <p:dataGrid id="guestList" var="guest" value="${guestList.guests}" columns="3" paginator="true" rows="20">
    <f:facet name="header">
      Guest List
    </f:facet>

    <p:panel>
      <h:outputText value="${guest.name}" />
      <br />
      <h:outputText value="${guest.street}" />
      <br />
      <h:outputText rendered="#{guest.street2.length() gt 0}"
        value="${guest.street2}" />
      <h:panelGroup rendered="#{guest.street2.length() gt 0}">
        <br />
      </h:panelGroup>
      <h:outputText value="${guest.city}, " />
      <h:outputText value="${guest.state} " />
      <h:outputText value="${guest.zipCode}" />
      <p:commandButton update="@form:newGuestDetail" oncomplete="PF('newGuestDialog').show()" icon="ui-icon-edit" styleClass="ui-btn-inline">
        <h:outputText styleClass="ui-icon ui-icon-edit" style="margin:0 auto;" />
        <f:setPropertyActionListener value="#{guest}" target="#{guestList.selectedGuest}" />
      </p:commandButton>
    </p:panel>
  </p:dataGrid>

  <p:dialog header="#{guestList.hasSelected() ? 'Edit Guest' : 'New Guest'}" widgetVar="newGuestDialog" modal="true" showEffect="fade" hideEffect="fade">
    <p:outputPanel id="newGuestDetail">
      <h:outputText value="'#{guestList.selectedGuest.name}'"/>
      <p:inputText id="guestName" value="#{guestList.hasSelected() ? '' : guestList.selectedGuest.name}" pt:placeholder="Name"/>
      <p:commandButton value="#{guestList.selectedGuest == null ? 'Create Guest' : 'Update Guest'}"/>
    </p:outputPanel>
  </p:dialog>
</h:form>
Run Code Online (Sandbox Code Playgroud)

hasSelected()方法计算所选guest虚拟机是否为null,如果不为null则返回true.当单击commandButton时应该设置selectedGuest,以便对象可以检索对象,但是,对于selectedGuest的get/set中的tracers,我没有看到使用上面的代码片段调用的setter.如果我删除了inputText,那么即使hasSelected仍然返回false,并且因此"新访客"正在对话框中,outputText则会填充一个值.

我发现这个伟大的帖子谈论了关于动作,动作监听器等的执行顺序,但不要认为这是我的问题:动作和actionListener之间的差异.

所以最终的问题是,当我只有一个outputText时,为什么我的setter会被命令按钮调用,但是使用inputText,我从未在日志中看到它被调用?

我很感激时间和帮助,任何人都可以提供.

Bal*_*usC 5

即使我们解决了你的问题,这个结构

<p:inputText value="#{guestList.hasSelected() ? '' : guestList.selectedGuest.name}">
Run Code Online (Sandbox Code Playgroud)

永远都不会工作.它需要引用模型属性,而不是空字符串.

你最好只重用编辑表单,让创建按钮预先创建一个空实体.这将在视图方面简化很多.如果实体具有@Id仅在数据库中持久存在的属性,则会更容易.

这是一个启动示例:

<h:form id="entitiesForm">
    <p:dataTable id="entitiesTable" value="#{bean.entities}" var="entity">
        <p:column>#{entity.foo}</p:column>
        <p:column>#{entity.bar}</p:column>
        <p:column>
            <p:commandButton id="edit" value="Edit" 
                process="@this" action="#{bean.edit(entity)}"
                update=":entityDialog" oncomplete="PF('entityDialog').show()" />
            <p:commandButton id="delete" value="Delete" 
                process="@this" action="#{bean.delete(entity)}"
                update=":entitiesForm:entitiesTable" />
        </p:column>
    </p:dataTable>
    <p:commandButton id="add" value="Add" 
        process="@this" action="#{bean.add}" 
        update=":entityDialog" oncomplete="PF('entityDialog').show()" />
</h:form>

<p:dialog id="entityDialog" widgetVar="entityDialog" 
    header="#{empty bean.entity.id ? 'New' : 'Edit'} entity">
    <h:form id="entityForm">
        <p:inputText id="foo" value="#{bean.entity.foo}" />
        <p:inputText id="bar" value="#{bean.entity.bar}" />
        <p:commandButton id="save" value="#{empty bean.entity.id ? 'Create' : 'Update'} entity" 
            process="@form" action="#{bean.save}"
            update=":entitiesForm:entitiesTable" oncomplete="PF('entityDialog').hide()" />
    </h:form>
</p:dialog>
Run Code Online (Sandbox Code Playgroud)

有了这个@ViewScopedbean:

private List<Entity> entities; // +getter
private Entity entity; // +getter

@EJB
private EntityService entityService;

@PostConstruct
public void load() {
    entities = entityService.list();
    entity = null;
}

public void add() {
    entity = new Entity();
}

public void edit(Entity entity) {
    this.entity = entity;
}

public void save() {
    entityService.save(entity); // if (id==null) em.persist() else em.merge()
    load();
}

public void delete(Entity entity) {
    entityService.delete(entity); // em.remove(em.find(type, id))
    load();
}
Run Code Online (Sandbox Code Playgroud)

也可以看看: