7.2. Page actions
A Seam page action is an event occurring immediately before a page is rendered. Declare page actions in
WEB-INF/pages.xml
. We can define a page action for a particular JSF view ID:
<pages> <page view-id="/hello.jsp" action="#{helloWorld.sayHello}"/> </pages>
Or we can use a
*
wildcard as a suffix to the view-id
to specify an action that applies to all view IDs that match that pattern:
<pages> <page view-id="/hello/*" action="#{helloWorld.sayHello}"/> </pages>
Note
If the
<page>
element is defined in a fine-grained page descriptor, the view-id
attribute can be omitted, as it is already implied.
If multiple wildcarded page actions match the current view-id, Seam will call all the actions, in order of least-specific to most-specific.
The page action method can return a JSF outcome. If the outcome is not null, Seam uses the defined navigation rules to navigate to a view.
The view ID mentioned in the
<page>
element need not correspond to a real JSP or Facelets page. This way, we can reproduce the functionality of a traditional action-oriented framework like Struts or WebWork using page actions. This is useful for performing complex actions in response to non-Faces requests like HTTP GET.
Multiple or conditional page actions can be specified with the
<action>
tag:
<pages> <page view-id="/hello.jsp"> <action execute="#{helloWorld.sayHello}" if="#{not validation.failed}"/> <action execute="#{hitCount.increment}"/> </page> </pages>
Page actions are executed on both an initial (non-Faces) request and a postback (Faces) request. If you use the page action to load data, it may conflict with the standard JSF actions being executed on a postback. One way to disable the page action is to set up a condition that resolves to
true
only upon an initial request.
<pages> <page view-id="/dashboard.xhtml"> <action execute="#{dashboard.loadData}" if="#{not FacesContext.renderKit.responseStateManager .isPostback(FacesContext)}"/> </page> </pages>
This condition consults the
ResponseStateManager#isPostback(FacesContext)
to determine if the request is a postback. The ResponseStateManager is accessed using FacesContext.getCurrentInstance().getRenderKit(). getResponseStateManager()
.
Seam offers a built-in condition that accomplishes this result less verbosely. You can disable a page action on a postback by setting the
on-postback
attribute to false
:
<pages> <page view-id="/dashboard.xhtml"> <action execute="#{dashboard.loadData}" on-postback="false"/> </page> </pages>
The
on-postback
attribute defaults to true
to maintain backwards compatibility. However, you are more likely to use false
more often.