I am attempting to create a dialog that will serve the purpose of both creating objects and updating them. So if I happen to click the 'new' button I will be presented a dialog containing empty fields to be filled or if I click on an edit button for an entry, that entry's data will presented in the dialog for update.
Following the example in the primefaces showcase for version 5.2, I can present the data in a read only outputText form, however when I change it to an inputText, the field remains empty. The following code is an example of what I have:
<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>
The hasSelected() method evaluates whether the selected guest is null or not, returning true if not null. The selectedGuest should be set when the commandButton is clicked so that an object is available for retrieval by the dialog, however, with tracers in the get/set for selectedGuest, I am not seeing the setter called with the above snippet. If I remove the inputText
, then even though the hasSelected
is still returning false, and thus the 'New Guest' is heading the dialog, the outputText
is filled with a value.
I found this great post talking about the order of execution with respect to the action, action listener, etc., but don't think this quite my issue: Differences between action and actionListener.
So the ultimate question is why will my setter get called with the command button when I only have an outputText, but with an inputText, I never see it called in the log?
I appreciate the time and help anyone can provide.
value="#{guestList.hasSelected() ? '' : guestList.selectedGuest.name}"
During edition the value will be empty because it is not connected to any field. – Geinmachi#{guestList.hasSelected() ? guestList.selectedGuest.name : ''}
. The logic was originally reversed. Even with this change, thesetPropertyActionListener
isn't getting fired as it does without theinputText
on the dialog. – kloginputText
. If it is empty then why does it exist? You won't be able to do anything with it anyway. If you type there something it won't be saved because value is not bound to any bean. Maybe this causes some error and stops further processing, check server logs if you have any errors, likejavax.faces.component.UpdateModelException: javax.el.PropertyNotWritableException:
– Geinmachivalue="#{guestList.selectedGuest.name}"
? I have tried that, but that didn't seem to help. Currently I was thinking that I'd pass null to differentiate 'new' from 'edit', but that seems like a bad approach with what you are saying. As for the log, I see only tracers, my get is returning null, there are no exceptions. With my new value="..." this exception is thrown:javax.el.PropertyNotFoundException: /index.xhtml @78,103 value="#{guestList.selectedGuest.name}": Target Unreachable, 'null' returned null
. Meaning the set didn't happen. – klogselectedGuest
instance usingnew Guest()
in add button's action method? How exactly did you otherwise intend to collect/convert/validate the submitted values? – BalusC