我正在尝试创建一个对话框,该对话框将用于创建对象和更新对象。因此,如果我碰巧单击“新建”按钮,我将看到一个包含要填充的空字段的对话框,或者如果我单击某个条目的编辑按钮,该条目的数据将显示在对话框中进行更新。
按照 primefaces 展示版本 5.2 中的示例,我可以以只读 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>
hasSelected() 方法评估所选访客是否为 null,如果不为 null,则返回 true。单击 commandButton 时应设置 selectedGuest,以便对话框可检索对象,但是,在 selectedGuest 的 get/set 中使用跟踪器时,我没有看到使用上述代码片段调用的 setter。如果我删除inputText
,那么即使hasSelected
仍然返回 false,因此“新来宾”正在对话中,outputText
充满了一个值。
我发现这篇很棒的文章讨论了有关操作、操作监听器等的执行顺序,但不认为这完全是我的问题:动作和动作监听器之间的区别 https://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener.
所以最终的问题是,当我只有一个输出文本时,为什么我的设置器会被命令按钮调用,但有了输入文本,我从来没有在日志中看到它被调用?
我很感谢您的时间和任何人都可以提供的帮助。
即使我们解决了你的问题,这个构造
<p:inputText value="#{guestList.hasSelected() ? '' : guestList.selectedGuest.name}">
永远不会起作用。它需要引用模型属性,而不是空字符串。
您最好只重用编辑表单并让创建按钮预先创建一个空实体。这会在视图方面简化很多。如果该实体有一个,那就更容易了@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>
有了这个@ViewScoped
bean:
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();
}
也可以看看:
- 为实体创建主从页面、如何链接它们以及选择哪个 bean 范围 https://stackoverflow.com/questions/8459903/creating-master-detail-pages-for-entities-how-to-link-them-and-which-bean-scope
- 提交后发生验证错误时保持 p:dialog 打开 https://stackoverflow.com/questions/9195756/keep-pdialog-up-when-a-validation-error-occurs-after-submit
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)