I am looking for best-practice guidance on how to implement the following. I am using JSF2, Spring 3, PrimeFaces 3.3, MyBatis.
REQUIREMENT:
I have a data entry screen with input fields linked to VO object which is a property on the managed bean. The user can either create a new record, or search for existing records to edit.
When the edit an existing record option is selected, a dialog box appears where the user is able to search and retrieve a list of potential records to select from.
They are only able to select one record to edit at a time. Once they have made their selection, the dialog box closes, and the VO object is populated with the selected data ready for the user to edit.
PROBLEM:
When the search form is submitted to retrieve a list of potential records for editing, the VO object has not yet been initialised and causes the ProcessValidationPhase to fail the process with a “Target Unreachable”.
What is the best way to implement this scenario? Should the VO object be initialised with blank values in the managed bean? Or should there be an initialise method on the VO object that is called during the initialisation of the managed bean?
DataEntry.XHTML
The main data entry fields:
<p:panelGrid id="dataEntry">
<p:row>
<p:column style="width:200px;">
<h:outputLabel value="Field 1:"/></p:column>
<p:column>
<p:inputText value="#{managedBean.dataEntryVo.field1}"
style="width:50px;"
disabled="true"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel value="Field 2:"/>
</p:column>
<p:column>
<p:inputText value="#{managedBean.dataEntryVo.field2}"
style="width:50px;"
disabled="true"/>
</p:column>
</p:row>
</p:panelGrid>
The command button on the dialog box used to retrieve a list of possible records for selection to edit:
<p:commandButton id="retrieveDataCb"
value="Retrieve"
actionListener="#{managedBean.retrieveDataEntryList}"
update=":dataEntryForm:retrievedList">
</p:commandButton>
Many thanks
Initialising the search criterion to a dummy object will definitely work. Not very neat but it works. On the other hand, you could try using a data container component like a
<p:dataGrid/>or<p:dataTable/>. There is avarattribute that will let you get away with null references like the case that you are trying to achieve. Thevarattribute compensates for empty datasets. That being said, you can now have aList<DataEntryVo>in your backing bean and then in your view:Another alternative, you could avoid binding the input fields to an object in your managed bean altogether, instead binding the input fields to the managed bean directly as in
<p:inputText binding="#{vo.field1Input}" style="width:50px;" disabled="true"/>and in your backing bean you will haveUIcomponent field1Input = new HtmlInputText();(or whatever the class is for primefaces and then callfield1Input.getValue()orfield1Input.setValue()according to your needs. It’s also a good idea to make your bean a ViewScoped bean to avoid unnecessary trouble getting this solution to work.