JSF 2 metadata Tag

JSF 2 metadata Tag


Posted in : Java Posted on : June 7, 2012 at 7:08 PM Comments : [ 0 ]

In this tutorial you will learn about the JSF 2 metadata tag.

JSF 2 metadata Tag

In this tutorial you will learn about the JSF 2 metadata tag.

Metadata is a data about data/data information. In JSF <f:metadata> tag is also used for this purpose it declares the view's metadata aspect. While using <f:metadata> tag it is care about that this tag must be a child of <f:view> tag. This tag declares the metadata facet. This tag should not be used in the XHTML templates it is essential to use this tag inside the top level XHTML file (the root page) for the given viewId. This tag must be implemented on the UIPanel (UIComponent for managing the layout of its child components) even, on a single child and the id of UIPanel must also be set.

There is no attribute defined for this tag.

Example :

Here an example is being given will demonstrate you about how to use the JSF f:metadata tag. In this example I have used the f:viewParam as a child tag of the f:metadata tag. f:viewParam tag is a tag which is just work like <h:inputText>. When you will use this tag, the value is passed into the URL and is look like the URL as the form submitted with method="get" however, using with this tag it can be possible with method="post". for example /jsfMetadata/jsfPages/inputPage.jsf?name=deepak . To complete this example I have created a Java Bean class named NameBean with a data member "name" and its corresponding getter setter method. Then created a JSF page where used the <f:metadata> tag. And in last make changes into the auto generated web.xml welcome-file-list as <welcome-file>jsfPages/inputPage.xhtml</welcome-file>.

Directory Structure

NameBean.java

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

@ManagedBean(name="nameBean")
@RequestScoped
public class NameBean {

String name;

public String getName() {
return name;
}

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

inputPage.xhtml

<!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 metadata Tag</title>
</h:head>
<h:body>
<f:view>
<f:metadata>
<f:viewParam name="nm" value="#{nameBean.name}"></f:viewParam>
</f:metadata>
<h:outputText value="Parameter Value : #{nameBean.name}"></h:outputText>
</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>jsfMetadata</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/run your server then use the following link on the web browser http://localhost:8080/jsfMetadata/jsfPages/inputPage.jsf

Output :

When you will execute this example you will get the output as follows :

But when you will give the value in URL as given into the image below then the output will be as follows :

In the URL value given as ?name=deepak here, '?' is used to associate the variable name used in input text, 'nm' is name of <f:viewParam> and the 'deepak' is its value.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics