JSF 2 actionListener Example

JSF 2 actionListener Example


Posted in : Java Posted on : May 29, 2012 at 8:04 PM Comments : [ 0 ]

In this example you will learn about JSF core tag actionListener.

JSF 2 actionListener Example

In this example you will learn about JSF core tag actionListener.

actionListener tag is used to register an instance of ActionListener for the UIComponent which is closely related with the parent UIComponent custom action.

Attributes of actionListener

There are following attributes of actionListener :

  • type : This attribute is not a required attribute, it is used for specifying the name of the Java class that implements the ActionListener interface to create and register the ActionListener instance on the UIComponent.
  • binding : This is not a required attribute, it is used for specifying the expression that binds the value which evaluates to an object implemented the javax.faces.event.ActionListener.
  • for : This is not a required attribute, and it specifies the value of one of the displayed joined objects among the composite component within which this tag is nested.

actionListener can be implemented by the two way :

1. As an attribute, this process may call up the method binding, in this process you can use it as an attribute of button or link component. Here a Java bean method can be specified directly in this attribute. for example :

In the Java bean class method can be as follows :

public void processIt(ActionEvent event)
{
System.out.println("Command Button has been clicked");

}

And it can be used as an attribute of button as follows :

<h:commandButton action="greeting" value="submit" actionListener="#{nameBean.processIt }"/>

2. Using as a tag, In this process you will have to crate a Java bean class that have implemented the ActionListener interface and overrides its method. And use the <f:actionListener> tag as a child tag of the button or link component on which you want to register the instance of ActionListener.

Example :

Here I am giving a simple example which will demonstrate you about how an ActionListener instance can be registered on a UIComponent. In this example I have used the <f:actionListener> tag. At first I have created a NameBean.java file that implements an ActionListener interface then created an index.jsp page which will be called first at the time of deployment of this application. In this page I have simply used the <jsp:forward> action which will transfer the control to the inputname.jsf page. In actual an inputname.jsf is a JSP page this page will be read by context using url mapping into the web.xml file. In the inputname.jsp page I have used the inputText tag of html tag library to take input in the text box and commandButton of html tag library which will work as a submit button and its action attribute controls the navigation of files which will send the submitted value to the specified page and used the <f:actionListener> tag to register the instance of ActionListener on the commandButton, this listener will be called when the commandButton will be clicked. Then created a greeting.jsp page where I have used the outputText tag of jsf html tag library to show the output on the browser and this page will opened when the commandButton on inputname.jsp page will be clicked. Then created a web.xml file to map the FacesServlet.

Directory Structure of this example

NameBean.java

package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

@ManagedBean(name="nameBean")
@RequestScoped

public class NameBean implements ActionListener {

String name;

public String getName() {
return name;
}

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

@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
System.out.println("Command Button has been clicked");

} 
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<jsp:forward page="/pages/inputname.jsf" />
</body>
</html>

inputname.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Input page</title>
</head>
<body>
<f:view>
<h:form>
Enter Text Here : <h:inputText value="#{nameBean.name}" />
<h:commandButton action="greeting" value="submit">
<f:actionListener type="devmanuals.NameBean"/>
</h:commandButton>
</h:form>
</f:view>
</body>
</html>

greeting.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Output Page</title>
</head>
<body>
<f:view>
Hello, <h:outputText value="#{nameBean.name}"/>!
</f:view>
</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_2_5.xsd"
id="WebApp_ID" version="2.5">

<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>

<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

<!-- Servlet -->
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Mapping Of Servlet -->
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

</web-app>

There is no need of faces-config.xml file

Output :

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

1. At first the a page will be displayed you to enter the text ( I have entered the text already) as follows :

And the console will be displayed as follows :

2. When you will enter the text as above and clicked on button then the output will be as follows :

And on console will be looked as follows :

Download Source Code(WAR File)

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics