97 total views, 4 views today

How to Bookmark URL in JSF using GET method and ViewParam – Example

By default, JSF makes POST request to the server. The HTTP method POST is useful for INSERT and UPDATE operation. For example, GMAIL signup form. When a user fills and submits the form. A new user will get inserted into the database.

The HTTP method GET is useful for SELECT operation, which retrieves a data from the server and displays it in the UI. For example, GOOGLE search result, Search something in google, Copy the URL and paste it in the different tab, you will get the same result. The URL can be bookmarkable. Here below is a GET URL look like

http://www.catgovind.com/?s=jsf

In the above URL, The right side of the ? symbol is Query parameters. It contains one or more name=value pairs; ‘s’ is the name and ‘jsf‘ is value here.

In this section, we walk through on the following topics

1. Create a Bookmark URL in JSF  using <h:link> tag and outcome attribute

2. Pass the Query Parameter values to a Java bean class using JSF <f:ViewParam>

 

Create a bookmarkable URL in JSF – Example

 

In this example project, I have created two JSF pages. Index.xhtml has list of GET URL’s contains employee ID and name as query parameters; On clicking the URL will go to employee.xhtml page displays employee ID and name from the QUERY parameters

 

Below is the index.xhtml page. This contains a list of GET URL. Those URL;s contains two query parameters; ID and name. We can generate this URL’s dynamically through Java List, MAP or some another ways. 

 

  1. The first & second <h:link>tag uses outcome attribute. The outcome refers the view ID. The <h:link>renders like below when the pge loads 
    1. <a href=”employee.xhtml?id=1&amp;name=govind”>Govind</a>..
  2. Use the &amp; to separate query parameters 
  3. We can also use <f:param> to pass the query parameters 

 

 

Below is the employee.xhtml page; This page display the employeeID and employeeName from the managed bean class. 

  1. The employee page looks like http://localhost../../employee.xhtml?id=1&name=govind
  2. At first, the  <f:viewParam> tag fetch the ID from the URL (Query parameter) and pass it to the managed bean method User.setEmployeeid() & The second <f:viewParam> tag fetch the NAME(Query Parameter) from the URL and pass it to the managed bean method User.setEmployeename()
  3. The <f:viewAction> tag calls the User.onload() method during postBack lifrcycle 

Also notice, I used xmlns:f=”http://java.sun.com/jsf/core” instead of xmlns:f=”http://xmlns.jcp.org/jsf/core” . 

 

 

Below is the Managed Bean java class. 

 

 

This is how the Page looks like in the browser 

 

JSF_BOOKMARK_GET_VIEWPARAM How to Bookmark URL in JSF using GET method and ViewParam - example

 

 

Download Source Code

govindan How to Bookmark URL in JSF using GET method and ViewParam - example
Connect me

Govind

Thank you for visiting my personal blog. Myself Govindan, Software Developer by profession since 2006 and hence I started this blog early in 2016 and ever since I've been writing about technologies experienced and learnings of everyday life.

The views expressed on this blog are my personal views and do not necessarily reflect the views of my employer.

Please feeling free to reach me on any comments and feedbacks you have. Would be more than glad to listen and reply 🙂
govindan How to Bookmark URL in JSF using GET method and ViewParam - example
Connect me

Latest posts by Govind

By | 2017-10-22T22:27:14+00:00 October 14th, 2017|Categories: JSF|Tags: , , , , , , |0 Comments
Like us on Facebook.
Connect!