Server-side Value Change Events / Listeners

Code 1 - ChangeRequestBean.java

package com.ibm.xsp.beans;

import javax.faces.component.UIComponent;
import javax.faces.event.ValueChangeEvent;

/*
 * @author Tony McGuckin, IBM
 */
public class ChangeRequestBean { 
	
	public ChangeRequestBean (){}
	
	public void valueChangeListener(ValueChangeEvent valueChangeEvent){
		
		// Note: valueChangeListener's are fired during the PROCESS_VALIDATIONS phase
		// but only after any binded validator(s) on the component succeeds...
		UIComponent c = valueChangeEvent.getComponent();
		System.out.println("Value changed component: " + c.getId());
		System.out.println("Old value: " + valueChangeEvent.getOldValue());
		System.out.println("New value: " + valueChangeEvent.getNewValue());
		
		// do something useful...
		//    perform second level check with a service...
		//    order a pizza using REST... whatever!
		/*
		if(ChangeRequestService.doesNotApproveThisNewlyValidatedValue(c.getId(), valueChangeEvent.getNewValue()){
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(
				c.getClientId(context), new FacesMessage(
					FacesMessage.SEVERITY_ERROR,
					ChangeRequestService.getMessage(),
					ChangeRequestService.changeRequest.getSummary()
				)
			);
			// failover to the RENDER_RESPONSE phase...
			FacesContext.getCurrentInstance().renderResponse();
		}
		*/
	}
	
} // end ChangeRequestBean


Code 2 - faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <managed-bean>
    <managed-bean-name>changeRequestBean</managed-bean-name>
    <managed-bean-class>com.ibm.xsp.beans.ChangeRequestBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
</faces-config>


Code 3 - XSP Markup

<xp:inputText id="inputText3" validator="#{validationBean.validate}"
	valueChangeListener="#{changeRequestBean.valueChangeListener}">
</xp:inputText>
<xp:message id="message3" for="inputText3"></xp:message>





Example illustrating use of a valueChangeListener by way of a Managed Bean method.

XPages
Tony McGuckin
April 28, 2012 7:15 AM
Rating
484

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...