HTML and CSS Reference
In-Depth Information
<f:viewAction />
A new view metadata tag, <f:viewAction /> , was introduced in JSF 2.2. The purpose of the tag is to allow for
preprocessing before the response is rendered. Preprocessing may include fetching data from the database or checking
conditions that alter the navigation flow. As an example you could use the viewAction to load the entity to display from
the database. If the requested entity does not exist in the database, you can redirect the user to a view stating that the file
no longer exists. Listing 5-25 shows a Facelets file that has a viewAction that loads a record upon viewing the page.
Listing 5-25. Facelet View Using f:viewAction to Load the Record to Display
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
<html xmlns=" http://www.w3.org/1999/xhtml "
xmlns:h=" http://java.sun.com/jsf/html "
xmlns:f=" http://java.sun.com/jsf/core " >
<f:view>
<f:metadata>
<f:viewParam name="id" value="#{recordDisplay.id}" />
<f:viewAction execute="#{recordDisplay.load}" onPostback="false" />
</f:metadata>
</f:view>
<h:head>
<title>View Record #{recordDisplay.id}</title>
</h:head>
<h:body>
<h1>Record ##{recordDisplay.id}</h1>
<h:panelGrid columns="2">
<h:outputText value="Name:" />
<h:outputText value="#{recordDisplay.record.name}" />
<h:outputText value="Description:" />
<h:outputText value="#{recordDisplay.record.description}" escape="false" />
</h:panelGrid>
</h:body>
</html>
Listing 5-26 contains the navigation rule that should be used when trying to access a non-existing record.
The rule states that if false is returned the browser should be redirected to the /not-found.xhtml file.
Listing 5-26. Navigation Case in faces-config.xml that Redirects the User if the Entity Could Not Be Loaded
<faces-config>
<navigation-rule>
<navigation-case>
<from-action>#{recordDisplay.load}</from-action>
<from-outcome>false</from-outcome>
<to-view-id>/not-found.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
</faces-config>
 
Search WWH ::




Custom Search