The Java EE 6 Tutorial, Volume I

Creating Bookmarkable URLs with h:button and h:link Tags

Bookmarkability or the ability to create bookmarkable URLs refers to the ability to generate hyperlinks based on specified navigation outcome and component parameters. Bookmarkable URLs are supported in JavaServer Faces 2.0.

In HTTP protocol, by default most browsers send GET requests for URL retrieval and POST requests for data processing. The GET requests can have query parameters and can be cached while it is not advised for POSTs which send data to the external servers. The other JavaServer faces tags capable of generating hyperlinks use either simple GET requests as in the case of h:outputlink, or POST requests as in the case of h:commandLink or h:commandButton tags. Get requests with query parameters provide finer granularity to URL strings. These URLs are created with a one or more name=value parameters appended to the simple URL after a ? character and separated by either &; or & strings.

Bookmarkable URLs or can be created with the help of the OutcomeTarget component, which is rendered as one of the following two HTML tags:

Both of these tags are capable of generating a hyperlink based on the outcome attribute of the component. For example:

<h:link outcome="response" value="Message">
<f:param name="Result" value="#{sampleBean.result}"/>
 </h:link>

The h:link tag will generate a URL link that points to the response.xhtml file on the same server, appended with the single query parameter created by the f:param tag. When processed, the parameter Result is assigned the value of backing bean's result method #{sampleBean.result}. A sample HTML generated from the above set of tags is as follows assuming the value of the parameter is success:


<a href="http://localhost:8080/guessNumber/guess/response.xhtml?Result=success">Response</a>

This is a simple GET request. To create more complex GET requests and utilize the h:link tag's functionality, you may use View Parameters.

Using View Parameters

The core tags metadata and f:viewparam, are used as a source of parameters for configuring the URLs. View parameters are declared as part of metadata for a page as shown in the following example:

<h:body>
<f:metadata>
<f:viewParam id="name" name="Name" value="#{sampleBean.username}"/>
<f:viewParam id="ID"  name="uid" value="#{sampleBean.useridentity}"/>

</f:metadata>
<h:link outcome="response" value="Message" includeViewParams="true">
 </h:link>
</h:body>

View parameters are declared with f:viewparam tag and are placed inside the f:metadata tag. If includeViewParams attribute is set on the component, the view parameters are added to the hyperlink.

The resulting URL will look like this:


http://localhost:8080/guessNumber/guess/response.xhtml?Name=Duke&;uid=2001

As the URL can be the result of various parameter values, the order of the URL creation has been predefined. The order in which the various parameter values are read is as under:

  1. Component

  2. Navigation-case parameters

  3. View parameters

When there is a GET request for the page, the Restore View and Render Response phases (sub phases of JavaServer Applications request lifecycle) are executed immediately. In case the page is using view parameters for creating a bookmarkable URL, the post-back request lifecycle is executed with all phases being processed.