JSF Examples


Facet Application
Previous Home Next

Tag description: JSF <f:facet> Tag is register a named facet on the UIComponent associate with the enclosing parent tag. FacetTag is the JSP mechanism for denoting a UIComponent is to be added as a facet to the component associated with its parent. A FacetTag must have one and only one child. This child must be a UIComponentTag instance representing a single UIComponent instance.




Step 1: Welcome page of Example

Name= welcomeJSF.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri=""%>
<%@taglib prefix="h" uri=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
This file is an entry point for JavaServer Faces application.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<h1><h:outputText value="Tag <f:facet> Example"/></h1>
<h:form id="facet">
<%-- Display Error message --%>
<h:message for="Fname" style="color:red" /><Br/>
<h:message for="Lname" style="color:red" /><Br>
<h:message for="age" style="color:red" /><Br>
<h:panelGrid columns="2" cellpadding="2" cellspacing="3" border="2" style="color:blue">
<%-- Create header of table --%>
<f:facet name="header">
<h:outputText value="Header Message: Fill all Detail"/>
<%-- Insert data into table --%>
<h:outputText value="Enter First Name"/>
<h:inputText id="Fname" size="25" required="true" value="#{facet.fname}"/>
   <h:outputText value="Enter Last Name"/>
<h:inputText id="Lname" size="25" required="true" value="#{facet.lname}"/>
  <h:outputText value="Enter Age"/>
<h:inputText id="age" size="25" required="true" value="#{facet.age}"/>
   <h:commandButton value="Submit" action="#{facet.submit()}" />
<h:commandButton value="Reset" action="#{facet.reset()}" />
<%-- Create footer of table --%>
<f:facet name="footer">
<h:outputText value="Footer Message" />
<%-- Display result --%>
<h:panelGrid columns="5"  rendered="#{facet.flag!= false}">
<h:outputLabel value="Hello, Mr"/>
<h:outputText value="#{facet.fullName}"/>,
<h:outputLabel value="Next year you will be"/>
<h:outputText value="#{facet.age}" />

Step 2: ManagedBean class for provide logic in program.

 * Save as a
package r4r.JSF2;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.webapp.FacetTag;
@ManagedBean(name = "facet")
public class facetBean extends FacetTag {
private String table, Fname, Lname, fullName;
private Integer age;
private boolean flag = false;
public String getTable() {
return table;
public void setTable(String table) {
this.table = table;
public String getFname() {
return Fname;
public void setFname(String Fname) {
this.Fname = Fname;
public String getLname() {
return Lname;
public void setLname(String Lname) {
this.Lname = Lname;
public Integer getAge() {
return age;
public void setAge(Integer age) {
this.age = age;
public boolean isFlag() {
return flag;
public void setFlag(boolean flag) {
this.flag = flag;
public String getFullName() {
return fullName;
public void setFullName(String fullName) {
this.fullName = fullName;
/* -- submit method -- */
public String submit() {
flag = true;
fullName = Fname + " " + Lname;
age = age + 1;
return "submit";
 /* -- reset method -- */
public String reset() {
flag = false;
age = null;
Fname = null;
Lname = null;
return "reset";

Output :

Previous Home Next