JSF Bookmarkable View Parameters Example

Prior to JSF 2.0 it was hard to accomplish bookmarkable URL’s. Now since JSF 2.0 they have made a good effort. We now have a new category of parameters. JSF Bookmarkable View Parameters. These are implemented by the UIViewParameter class that extends UIInput. Let’s take a look at an example how to achieve bookmarkable URL’s in JSF 2.x.

Managed Bean

We are using this managed bean to pass a view parameter from one page to another.

package com.memorynotfound;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

public class UserBean {

    private int id;
    private String name;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

Include JSF Bookmarkable View Parameters

View parameters can be included in links by using the includeVidwParams=”true”.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"

    <h1>JSF 2.2 Bookmarkable View Parameters</h1>

    <h:link value="click link with parameters" outcome="result" includeViewParams="true">
        <f:param name="id" value="13"/>
        <f:param name="name" value="John Doe"/>


Map and validate Bookmarkable View Parameters

JSF gets the request parameter’s values by name from the page URL and applies optional specified converters/validators. After conversion/validation succeeds and before the view is rendered, JSF binds the values of the request parameters to the managed bean properties by calling the appropriate setter methods.

Since the view parameters are of type UIViewParameter and extend the UiInput class, this means that it inherits all attributes such as required and requiredMessage. When you need to ensure the url must contain certain attributes and the application flow is correct you can inform the user with the appropriate message, by using the <h:messages>.

View Parameters extend from

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
        <f:viewParam name="id" value="#{userBean.id}" required="true" requiredMessage="id is required"/>
        <f:viewParam name="name" value="#{userBean.name}"/>

    <h:messages style="color: red"/>

    <h1>JSF 2.2 Bookmarkable View Parameters</h1>

    <h:panelGrid columns="2">
        <h:outputLabel value="Requested id: "/>
        <h:outputText value="#{userBean.id}"/>

        <h:outputLabel value="Requested name: "/>
        <h:outputText value="#{userBean.name}"/>


Servlet Descriptor

Don’t forget to register the FacesServlet in the servlet descriptor.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">


    <!-- Change to "Production" when you are ready to deploy -->

    <!-- Welcome page -->

    <!-- JSF mapping -->
        <servlet-name>Faces Servlet</servlet-name>

    <!-- Map these files with JSF -->
        <servlet-name>Faces Servlet</servlet-name>



URL: http://localhost:8080/jsf-view-parameters/

jsf bookmarkable view parameters

URL: http://localhost:8080/jsf-view-parameters/result.xhtml?id=13&name=John+Doe

jsf bookmarkable view parameters

URL: http://localhost:8080/jsf-view-parameters/result.xhtml?name=John+Doe

jsf bookmarkable view parameters



You may also like...