Avoid the JSF preRenderView on CRUD edit mode
Tech-Today

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 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']}" >
<p:commandButton action="update.xhtml" ajax="false"
icon="ui-icon-pencil">
<f:param name="marketingCode" value="#{code.id}"></f:param>
</p:commandButton>
</p:column>

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

Define the event on the backing bean:
public void preRenderViewEdit() {
log.debug("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"
icon="ui-icon-pencil">
<f:setPropertyActionListener
target="#{marketingCodeBean.marketingCode}" value="#{code}"></f:setPropertyActionListener>
</p:commandButton>

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




- Javaee Development
JavaEE6How to create a javaee6 web app using jboss maven war archetypeCreate a simple javaee6 web app with maven, glassfish and postgresqlHow to validate a JavaEE6 Bean in a jobHow to add JavaEE 6 archetypes in eclipse keplerHow to create a custom bean...

- Java Generics And Multiple Parameterized Class With Inheritance
This tutorial will try to explain how to use multiple generic parameters in Java and also along the way the advantage of using this feature. Normally I use this when I have a method in a base class that returns a generic type. For example: public class...

- Loading Locale From Post/get Parameter
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter.  1.) In your mail xhtml template, make sure that you define f:view locale before the h:body tag: <f:view locale="#{languageBean.currentLocale}" />...

- How To Auto Wire A Spring Bean From A Jsf Managed Bean In Liferay Portlet
There are 2 ways I know to auto-wire a spring bean into a jsf managed bean: 1.) Is through WebApplicationContext, invoke during jsf managed bean constructor: Below are the most vital files to perform this action: web.xml <?xml version="1.0"?>...

- Specify On Delete No Action Or On Update No Action, Or Modify Other Foreign Key Constraints
Have you encountered this error: "Introducing FOREIGN KEY constraint 'x_y_Target' on table 'z_UsersInRoles' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints."...



Tech-Today








.