« Introduction to Developing JDeveloper Extensions | Main | JDeveloper Blogs Around The World »

Executing An Action on JSF Page Startup with Pagedef

Someone asked this on the OTN forum:
Create an JSF page with an ADF Form and a Create button - now when you run it - you first have to click the create button to get an empty record in the screen that you can insert data into.
The question is how do I get the form to load up with an empty record.

Here is a trick that will do it.
Go to your JSF page's pagedef.xml file and look in the structure pane - you'll see this:
create1:

Notice that you have a binding already in place for the Create action.
Next add an invokeAction executable BEFORE the iterator executable.

Create2:

In the dialog that pop-up specify a name for the binding - and then specify the Create operation.

create003.gif:

Now when you'll run the page you'll get an empty record ready for you to work on.

[small update based on some comments]

However, if you'll run the page now you'll see that after you press commit - another new record is created. This is because the action is being executed again after the postback.

The solution for this is to restrict the execution of the method through the refresh condition of the action in the pagedef.
You set it to be ${!adfFacesContext.postback}

You also need to set the Refresh property to be renderModel (see chapter 10.5.5.3 in the ADF Developer Guide)

Create4:

Now run the page and you'll get an empty form when you start and after you press commit there is no additional insert.

Now that you have the understanding of how this technique works behind the scene, I should probably mention that there is a simpler way to create this form.

In your first step, instead of dropping the view as an ADF Form drop it as an "ADF Creation Form".
When you look at the code that was created you'll see the same thing we have created manually in this blog :-)

Comments (5)

hi,
also you can create a "adf creation form" since data control palette.

Regards

Lana:

I have a different problem. I get my new record when I run my page, but I also get two more, when I come back froma popup window my page is calling. Any suggestions?

Shay Shmeltzer:

Lana, I would suggest that you post your question on the JDev forum on OTN with a bit more details about what you are doing specifically.
As well as the information about the refresh condition set for your action binding.

Ebtehal:

Hi, well done.. i have a different problem if u can help me?? i need to set the where clause for my view objects at runtime , because they depend on a runtime variable, but i want this to be at pages load not according to a client invoke???

Shay Shmeltzer:

If this approach doesn't work for you (you can replace the create method in this sample with any AM service method that you expose). Then maybe go for sample 60 on Steve Muench's blog:
http://radio.weblogs.com/0118231/stories/2004/09/23/notYetDocumentedAdfSampleApplications.html#60

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)