JSF 2 composite actionSource

JSF 2 composite actionSource


Posted in : Java Posted on : September 29, 2012 at 10:42 AM Comments : [ 0 ]

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

JSF 2 composite actionSource

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

<composite:actionSource> tag specifies the implementation of javax.faces.component.ActionSource2. This tag is used to attach the object value in the using page that the component is used in the composite component. This tag is declared inside the <composite:interface> tag with some value of the 'name' attribute and/or 'targets'. Inside the <composite:implementation> tag if any component's object value is attached to the actionSource then their names must be matched.

Attributes of JSF 2 composite actionSource

  • name : This is a required attribute. Value of this attribute is used for attaching the objects value of the component/s. If the targets attribute is not used with this attribute then it is also represent the component ID of the target component inside the <composite:implementation> tag.
  • targets : This is not a required attribute. This attribute is used to specify more than one values that can be attached to the component's object value/s

Example :

Here I am giving a simple example which will demonstrate you about how to use the JSF 2 composite actionSource tag. In this example I have created the composite component pages where into one of which actionSourceCC.xhtml page I have exposed the submit button as submitButton using the <composite:actionSource> tag and attached to the <h:commandButton> component and in the using page input.xhtml attached with the <f:actionListener> component. When you will execute this example then a web page will be opened and when you will click on the given 'submit' button then a listener will be called and a message will be displayed on the console.

Directory Structure

NameBean.java

package devmanuals;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

@ManagedBean(name="nameBean")
@SessionScoped
public class NameBean implements ActionListener {

public String name; 

public String getName() {
return name;
}

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

public String action(){
return "output";
}

@Override
public void processAction(ActionEvent event) throws AbortProcessingException {

System.out.println("Submit button has been clicked");
} 
}

Composite Component Pages

actionSourceCC.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"
xmlns:composite="http://java.sun.com/jsf/composite"
>
<composite:interface>
<composite:actionSource name="submitButton"
targets="form:submitButton"/>
<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 id="form">
<h:panelGrid columns="2" id="textPanel">

#{cc.attrs.nmLbl} : 
<h:inputText id="nameVal" value="#{cc.attrs.nmVal}" 
label="Textbox">
<f:validateLength minimum="6" for="nameVal" />
</h:inputText><br/>
<h:message for="nameVal" style="color:red" /> 
</h:panelGrid>

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

</h:form>

</composite:implementation>
</html>

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

<composite:interface>

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

</composite:interface>

<composite:implementation>

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

</composite:implementation>

</html>

Using Pages

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

<h:body> 

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

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

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

<f:actionListener for="submitButton" type="devmanuals.NameBean"/>
</dIn:actionSourceCC>
</h:body>

</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 actionSource tag</title>
</head>
<h:body>
<h3>JSF 2 composite actionSource Tag Example</h3>
<dOut:out
nmLbl="Your Name : " 
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>jsfCompositeActionSource</display-name>
<welcome-file-list>
<welcome-file>/xhtmlPages/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>
<url-pattern>*.jsp</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 you will execute this example successfully then the output will be as follows :

1. The main page will be look like as follows :

2. When you will click on the submit button then a message will be displayed on the console like as follows :

3. And when you will enter the characters into the given textbox then the output will be as follows :

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics