JSF 2 composite implementation

JSF 2 composite implementation


Posted in : Java Posted on : September 7, 2012 at 10:43 AM Comments : [ 0 ]

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

JSF 2 composite implementation

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

<composite:implementation> tag of JSF composite tag library is used for implementing the composite component declared in the composite:interface. To access the components interface attributes an expression #{cc.attrs.attribute_name) is used ( cc is a reserved keyword in JSF). A corresponding <composite:implementation> tag will must be required in case of the <composite:interface> tag is used in the composite component page and in case the <composite:interface> tag is not used in the composite component page then the use of <composite:implementation> tag is optional.

No Attributes for JSF 2 composite implementation tag is defined.

Example

Here I am giving a simple example which will demonstrate you about how to use the JSF 2 composite implementation tag. In this example I have created to composite components one for the input (in.xhtml) and other for the output (out.xhtml). In both of the composite components you will see that I have used the <composite:interface> tag and the corresponding <composite:implementation> tag.

Directory Structure

NameBean.java

<!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:dOut="http://java.sun.com/jsf/composite/dev/compositeComponent"
>
<head>
<title>JSF 2 composite interface tag</title>
</head>
<h:body>

<h3>JSF 2 composite implementation Tag Example</h3>

<dOut:out
nmLbl="Your Name is " 
nmVal="#{nameBean.name}" 
/>

</h:body> 
</html>

in.xhtml (cc file)

<!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"
xmlns:composite="http://java.sun.com/jsf/composite"
>

<composite:interface>

<composite:attribute name="nmLbl" />
<composite:attribute name="nmVal" />

<composite:attribute name="submitBtnText" />
<composite:attribute name="submitBtnAction" 
method-signature="java.lang.String action()" />

</composite:interface>

<composite:implementation>

<h:form>

<h:panelGrid columns="2" id="textPanel">

#{cc.attrs.nmLbl} : 
<h:inputText id="name" value="#{cc.attrs.nmVal}" />

</h:panelGrid>

<h:commandButton action="#{cc.attrs.submitBtnAction}" 
value="#{cc.attrs.submitBtnText}"
/>

</h:form>

</composite:implementation>

</html>

input.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:dIn="http://java.sun.com/jsf/composite/dev/compositeComponent"
>

<h:body>

<h3>JSF 2 composite implementation Tag Example</h3>

<dIn:in 
nmLbl="Enter Your Name" 
nmVal="#{nameBean.name}" 

submitBtnText="submit" 
submitBtnAction="#{nameBean.submit}"
/>

</h:body>

</html>

out.xhtml (cc file)

<!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"
xmlns:composite="http://java.sun.com/jsf/composite"
>

<composite:interface>

<composite:attribute name="nmLbl" />
<composite:attribute name="nmVal" />

</composite:interface>

<composite:implementation>

<h:panelGrid columns="2" id="textPanel">

#{cc.attrs.nmLbl} : 
<h:outputText id="name" value="#{cc.attrs.nmVal}" />

</h:panelGrid>

</composite:implementation>

</html>

output.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:dOut="http://java.sun.com/jsf/composite/dev/compositeComponent"
>
<head>
<title>JSF 2 composite interface tag</title>
</head>
<h:body>

<h3>JSF 2 composite implementation Tag Example</h3>

<dOut:out
nmLbl="Your Name is " 
nmVal="#{nameBean.name}" 
/>

</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>jsfCompositeImplementation</display-name>
<welcome-file-list>
<welcome-file>input.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 execute this example

To execute this example you can download a WAR file example form here and import this war file into your eclipse by File -> import -> web -> war file and simply execute this example by selecting the imported project as follows :- Select your project -> Right Click -> Run As -> Run On Server.

Output :

When the above project will be executed successfully then the first page will be look like as follows :

The main page will look like as follows :

When you will enter the text into the textbox ( I have given the value "Deepak" ) and click on submit button then the output will be as follows :

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics