Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Seam supports the concept of a page action, which is a method-binding expression that is invoked before a view is rendered. In a typical use case of page actions, you need to invoke a component method upon the first entry into a page. The method might query the data access layer for data needed on the page and possibly subsequent pages. This operation is expected to be expensive enough that you only want to perform it once (which is why you are using a page action in the first place).

However, if the page contains a JSF form, you may want to avoid repeating the heavy lifting again. You essentially need a conditional page action. Seam offers two was to accomplish this.

First, you can simply disable page actions on any JSF postback using the on-postback attribute.

<action execute="#{dashboard.loadData}" on-postback="false"/>

Underneath, this uses the ResponseStateManager#isPostback(FacesContext) to determine if the request is a postback, which is available through FacesContext.getCurrentInstance().getRenderKit().getResponseStateManager(). Thankfully, in JSF 2.0 you can simply call the method FacesContext.getCurrentInstance().isPostback().

If you need more contextual logic, you can supply your own condition:

<action execute="#{dashboard.loadData}" if="#{empty data}"/>

The condition must evaluate to true for the action to be invoked.