JSF actionListener

It can be implemented with two ways:

1) For the button\link component we have to specify a specific bean's method in actionListener attribute.

2) We need to implement ActionListener and specify the same for a specific input component.

Example for 1st way: Dynamic Jsf Web Project.

index.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">
<body>
<h:form>
 
    #{msg.name}: <h:inputText value="#{userData.name}" />
  <br/>
  <h:commandButton action="#{userData.actionMethod()}" value="Hit"  
        actionListener="#{userData.actionListenerMethod()}"/>
   </h:form>
</body>
</html>

UserData Bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "userData")
@RequestScoped
public class UserData {
 
private String name;
private String niceDay;

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

public String getName() {
return name;
}

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

//Using Listener we are initializing niceDay variable and 
//will be displaying the same in output page
public void actionListenerMethod(){
niceDay = "Have a Nice Day";
}

public String getNiceDay() {
return niceDay;
}

public void setNiceDay(String niceDay) {
this.niceDay = niceDay;
}
}

Output page:

<!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"
      >
<body>
<h:form>
#{msg.urname}: <h:outputText value="#{userData.name}" id = "abc"/>
<br/>
<h:outputText value="#{userData.niceDay}" id = "abcd"/>
</h:form>
</body>
</html>

Run Application:

Enter Name and click on button:

Example for 2nd way: Dynamic Jsf Web Project.

index.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">
<body>
<h:form>
 
    #{msg.name}: <h:inputText value="#{userData.name}" />
  <br/>
  <h:commandButton action="#{userData.actionMethod()}" value="Hit"  >
  <f:actionListener type="UserChangeListener"></f:actionListener>
  </h:commandButton>
   </h:form>
</body>
</html>

ActionListener class:

import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

public class UserChangeListener implements ActionListener {

@Override
public void processAction(ActionEvent arg0) throws AbortProcessingException {
// TODO Auto-generated method stub
UserData userData = (UserData) FacesContext.getCurrentInstance()
.getExternalContext().getRequestMap().get("userData");
userData.setNiceDay("Have a Nice Day");
}
}

UserData Bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "userData")
@RequestScoped
public class UserData {
 
private String name;
private String niceDay;

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

public String getName() {
return name;
}

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

public String getNiceDay() {
return niceDay;
}

public void setNiceDay(String niceDay) {
this.niceDay = niceDay;
}
}

Output Page:

<!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"
      >
<body>
<h:form>
#{msg.urname}: <h:outputText value="#{userData.name}" id = "abc"/>
<br/>
<h:outputText value="#{userData.niceDay}" id = "abcd"/>
</h:form>
</body>
</html>

Run Application: Output will be same as shown above for ist way.