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 tag and outcome attribute
2. Pass the Query Parameter values to a Java bean class using JSF
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.
- The first & second tag uses outcome attribute. The outcome refers the view ID. The
- Use the & to separate query parameters
- We can also use to pass the query parameters
JSF Bookmarking - Example
Below is the employee.xhtml page; This page display the employeeID and employeeName from the Managed Bean class.
- The employee page looks like http://localhost../../employee.xhtml?id=1&name=govind
- At first, the
tag fetch the ID from the URL (Query parameter) and pass it to the managed bean method User.setEmployeeid() & The second tag fetch the NAME(Query Parameter) from the URL and pass it to the managed bean method User.setEmployeename() - The
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.
package com.catgovind; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name="user") @RequestScoped public class User { private String employeename; private String employeeid; private String greeting; public void onload(){ // } public void setEmployeename(String employeename) { this.employeename = employeename; } public void setEmployeeid(String employeeid) { this.employeeid = employeeid; } public String getGreeting(){ String id = "EmployeeID = "+this.employeeid; String name = "EmployeeName = "+this.employeename; this.greeting = id+" & "+name; return this.greeting; } public String getEmployeename() { return employeename; } public String getEmployeeid() { return employeeid; } }
This is how the Page looks like in the browser
Download the Project: JSF_View_Param_Example
This post first appeared on Oracle ADF, BPM, BI And Primavera P6 Tutorials, please read the originial post: here