Avoid the JSF preRenderView on CRUD edit mode

Long time ago I learned that the usual approach in CRUD is list, add, edit, delete. And right now I'm implementing the same thing in Jav...

Long time ago I learned that the usual approach in CRUD is list, add, edit, delete. And right now I'm implementing the same thing in JavaEE6, Glassfish, JSF 2.1, Primefaces. So what I did was:

1.) Create a single backing bean that will handle all the operations.

2.) Create a list page, where add, edit and delete action is defined. Delete do not need a page but a backing bean action that will delete the selected object and refresh the page.

3.) Create an edit page that will be render when the edit action is clicked on the list page. Now this edit page must submit the id of the object. And the id must be converted to object. See the code below.

On the list page we have a definition for the edit action:
<p:column headerText="#{msgs['common.update']}" style="width:50px">
 <p:commandButton action="update.xhtml" ajax="false"
  <f:param name="marketingCode" value="#{}"></f:param>

On the update.xhtml, we have to define a preRenderView event:
 <f:param name="dummy"></f:param>
 <f:viewParam name="marketingCode"
  converter="#{marketingCodeConverter}" />
 <f:event listener="#{marketingCodeBean.preRenderViewEdit()}"
  type="preRenderView" />

Define the event on the backing bean:
public void preRenderViewEdit() {
 if (marketingCode == null) {
  marketingCode = new MarketingCode();

And finally we have a save button on the edit page. But with this approach we will encounter the famous problem with the preRenderView event being called when the save action is invoke and validation failed. Or when a partial form is submitted. So you see we have to implement several checks on the preRender event method just to get away with it. My simple solution, that fits my requirement is to set a backing bean property inside the edit action definition. So in my case it'll be:
<p:commandButton action="update.xhtml" ajax="false"
  target="#{marketingCodeBean.marketingCode}" value="#{code}"></f:setPropertyActionListener>

And that solves my problem of transferring the id of the selected object from list page to update page.


jsf 5600379908256572110

Post a Comment Default Comments


Are you looking for freelancers in the Philippines? Get in touch.


If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.