One of the most requested features in JSF 2.0 was a nice and smooth mechanism for achieving bookmarkability of JSF pages. As you will see in this recipe, this mechanism is finally provided by JSF 2.0 and is a very robust and easy-to-use solution.
In this chapter, you have already seen a recipe about JSF bookmarkability, but remember that we talked about a solution based on a JSF extension, while now we are talking about a JSF core solution.
We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
Now, let's get into the subject, and let's say that the JSF 2.0 bookmarkability mechanism is based on two new tags, named h:link
and h:button
. These tags will generate a URL based on the specified navigation outcome.
In JSF 2.0, we can make use of implicit navigation, therefore the outcome can be defined in the view or using common navigation rules.
OK, enough theory, let's see an example:
… <h:link outcome="page2" value="HelloToYou"> <f:param name="helloparam" value="#{bean.hello}"/> </h:link> …
In the previous example, we assume no navigation rule, therefore the outcome attribute indicates a navigation to page2.xhtml
(the FacesServlet
is mapped to *.xhtml
). The value
attribute indicates text that will be rendered as a link in the page. The f:param
will add a query parameter to the generated URL. The result of this component will be:
http://localhost:8080/ URLs_based_on_specified_navigation_outcome/faces/page2.xhtml?helloparam=Adrian
The Adrian
value comes from a simple managed bean:
package beans; import javax.enterprise.context.RequestScoped; import javax.faces.bean.ManagedBean; @ManagedBean @RequestScoped public class Bean { private String hello = "Adrian"; public Bean() { } public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } }
You can bookmark this page at any moment and conserve the URL. The h:button
works in the same manner except that it renders a button instead of a link.
Before the user uses the component—clicks on the hyperlink—the current view ID and the specified outcome are used to find the target view ID. Afterwards, it is translated into a bookmarkable URL and used as the hyperlink's target. Note that this is true even if the user never activates the component.
The target view ID is placed in the attribute named outcome on the new bookmarkable component tags, h:link
or/and h:button
(those components inherit from a component class named UIOutcomeTarget
). Notice that you are not targeting a view ID directly, but rather a navigation outcome, which may be interpreted as a view ID if the matching falls through to implicit navigation.
We consider that this is a good place and time to point out some methods of creating the query string parameters, therefore we present them in the order that they are processed:
<f:metadata>
of the target view ID)<f:param>
in UIOutcomeTarget
(such as, <h:link>
)<view-param>
in the navigation case <redirect>
element in faces-config.xml