In this tutorial you will learn about how to use JSF 2 f viewParam tag.
JSF 2 f viewParam
In this tutorial you will learn about how to use JSF 2 f viewParam tag.
<f:viewParam> tag of JSF core tag library is used within the <f:metadata> tag. Use of this tag provides the facility to set the GET request parameters.
Attributes of <f:viewParam> tag
- converter : This is not a required attribute. This attribute specifies the Converter instance for the component
- converterMessage : This is not a required attribute. This attribute is used for showing the converter message, it replaces the message (if any) that comes from the converter.
- id : This is not a required attribute. This is an identifier which provides the facility to make the component unique within the closest parent component.
- required : This is not a required attribute. This attribute acted like flag which 'true' value specifies that the submitted value is required.
- requiredMessage : This is not a required attribute. This attribute is used to display the validation message for the 'required' facility (if used). For this attribute you can use the ValueExpression for evaluating the java.lang.String.
- validator : This is not a required attribute. This
attribute is used to specifies the validator method which is called when the
validation (if any) is required to perform on the component.
- validatorMessage : This is not a required attribute but, is used to display the validation message (if any). It replaces the message (if any) comes from the validator.
- value : This is not a required attribute but, is used to set the current value to component.
- valueChangeListener : This is not a required attribute.
This attribute specifies the method for value changing listener that acted when
new value has been assigned to the input component.
- maxlength : This is not a required attribute but, is used when the maximum number of character is required to fix for the field to be entered.
- binding : This is not a required attribute but, is used to link the component with the properties of backing bean.
- for : This is not a required attribute but, is used for referring the exposed objects of the composite component within which this tag is nested.
Example :
Here I am giving a simple example which will demonstrate you about how may a <f:viewparam> tag be used in a JSF page. For this I have created a simple JSF page that contains the <f:viewParam> tag which is nested inside the <f:metadata> tag. This tag can be nested inside only the <f:metadat> tag. When this tag is used the form can be submitted as the "get" method i.e the value can be given in the URL. In this example I will show you that how to give value in the URL and how it can be fetched. Below I am giving the directory structure of this example and the source code.
Directory Structure
inputPage.xhtml
This is a very simple page that contains the viewParam tag inside the <f:metadata> tag and the <h:outputText> for showing the output.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>JSF viewParam Tag</title> </h:head> <h:body> <f:view> <f:metadata> <f:viewParam name="vp" /> </f:metadata> <h:outputText value="Parameter Value : #{vp}" /> </f:view> </h:body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>jsfViewParam</display-name> <welcome-file-list> <welcome-file>jsfPages/inputPage.xhtml</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> <url-pattern>*.jsf</url-pattern> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>resources.application</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> </web-app>
How to Run
At first start your server then use this url http://localhost:8080/jsfViewParam/jsfPages/inputPage.xhtml on your web browser.
Output :
When you will execute the above example using the above url you will get the output as follows :
But when you will give the URL with the value as http://localhost:8080/jsfViewParam/jsfPages/inputPage.xhtml?vp=deepak then the output will be as follows :
[ 0 ] Comments