☰ See All Chapters |
JSF Action Event Listener
Action event get fired when user performs any action like click, mouse over, press any key on user interface.
<h:commandButton> and <h:commandLink> are the two tags which supports action event on client side. These two tags have an attribute called actionListener which can be used to attach the listeners.
<h:commandButton actionListener="#{likeDisLikeBean.likeCounter}" value="Like" />
<h:commandLink actionListener="#{likeDisLikeBean.likeCounter}" value="Like" />
actionListener attribute should be bound with the method that takes an ActionEvent parameter, with a return type of void, or to a public method that takes no arguments with a return type of void. When method takes no arguments, the method has no way of easily knowing where the event came from, but this can be useful in cases where a notification is needed that "some action happened".
When user clicks like button only like count will be incremened, rest of the things remain same in web page. So here like button fires an event, this event is handled by the application code in the server side.
JSF Action Event Listener Example
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>JSF_ActionEventListener</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.6</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.6</version> </dependency> </dependencies> <build> <finalName>JSF_ActionEventListener</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.9</source> <target>1.9</target> </configuration> </plugin> </plugins> </build> </project> |
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "https://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> <display-name>Archetype Created Web Application</display-name> <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>*.xhtml</url-pattern> </servlet-mapping> </web-app> |
demo.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:h="https://xmlns.jcp.org/jsf/html" xmlns:f="https://xmlns.jcp.org/jsf/core">
<h:head></h:head> <h:body style="background-color:yellow"> <h:form> <h1>Do You Like this Website</h1> <h:panelGrid columns="2">
<h:commandButton actionListener="#{likeDisLikeBean.likeCounter}" value=" Like " /> <h:outputText value="#{likeDisLikeBean.likeCount}" />
<h:commandButton actionListener="#{likeDisLikeBean.disLikeCounter}" value="DisLike" /> <h:outputText value="#{likeDisLikeBean.disLikeCount}" />
</h:panelGrid> </h:form> </h:body> </html> |
LikeDisLikeBean.java
package com.java4coding;
import javax.faces.bean.ManagedBean; import javax.faces.event.ActionEvent;
@ManagedBean public class LikeDisLikeBean { private static int likeCount; private static int disLikeCount; public int getLikeCount() { return likeCount; }
public void setLikeCount(int likeCount) { this.likeCount = likeCount; } public int getDisLikeCount() { return disLikeCount; }
public void setDisLikeCount(int disLikeCount) { this.disLikeCount = disLikeCount; }
public void likeCounter(ActionEvent ae) { System.out.println("Inside likeCounter() Method******************"); likeCount=likeCount+1;
} public void disLikeCounter(ActionEvent ae) { System.out.println("Inside disLikeCounter() Method******************"); disLikeCount=disLikeCount+1; } } |
Eclipse Project Directory Structure
Output
All Chapters