Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsf Jsftraining Value Change Event Listener

Related Tutorials


 
 

Share on Google+Share on Google+

Value Change Event Listener

Advertisement
Value Change Listener can be implemented in 2 ways. If you use the first one then it will limit you to use only one listener for the component but second way permits you to include more than one listeners.

Value Change Event Listener

        

Value Change Listener  can be implemented in 2 ways. If you use the first one then it will limit you to use only one listener for the component but second way permits you to include more than one listeners.

1. Using "valueChangeListener" attribute of the component tag

<h:selectOneMenu id="songList" value="#{ValueChangeBean.selectedSong}" valueChangeListener="#{ValueChangeBean.processMyValueChange}" immediate="true" onchange="this.form.submit()">
  <f:selectItems value="#{ValueChangeBean.songList}"/>
</h:selectOneMenu>

Sample Code for processMyValueChange method in "ActionBean":

public void processMyValueChange(ValueChangeEvent vce){
   songvalue = (String) vce.getNewValue(); 
   setSongDetail((String)detail.get(songvalue));

   /*Render the response*/
   FacesContext.getCurrentInstance().renderResponse();
}

 

2. Using <f:valueChangeListener /> tag

The ValueChangeListener interface must be implemented to capture an ValueChangeEvent. This interface provides processValueChange() method which is called when the ValueChangeEvent action observed by this listener occurs. So processValueChange() method should be implemented for the value change event listeners.

processValueChange method signature:

public void processValueChange(ValueChangeEvent event) throws AbortProcessingException

For example:(In JSP)

<h:inputText id="name" immediate="true" onchange="this.form.submit()" required="true"> 
  <f:valueChangeListener type="roseindia.MyValueChangeBean" />
</h:inputText>

The code for MyValueChangeBean.java:

package roseindia;

import java.util.*;
import javax.faces.event.*;
import javax.faces.context.FacesContext;

public class MyValueChangeBean extends Object implements ValueChangeListener {
    public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {  
  if (null != event.getNewValue()) {
   FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("name", event.getNewValue());
  }
   }
}

Download code for all examples

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 30, 2008

Related Tutorials

Discuss: Value Change Event Listener  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com