In the previous example, I created a Backing class: HtmlInput2, which is a class that represents the top-level object NamingContainer of Composite Component. This gives me the opportunity to override the encode/decode method, so that I can use Java code to enhance Composite Compnent behavior.
In this example, create another Managed Bean to receive user input, intercept click button events, and display user input. Note that this new Managed Bean is in my jsfex project, so it is actually part of the internal implementation of Composite Compnent and does not require user-defined. This example is just to show that adding Managed Bean is no different from normal JSF development.
Now modify the htmlinput2.xhtml code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! 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: f = "http://java.sun.com/jsf/core"
Xmlns: h = "http://java.sun.com/jsf/html"
Xmlns: ui = "http://java.sun.com/jsf/facelets"
Xmlns: composite = "http://java.sun.com/jsf/composite">
<Composite: interface componentType = "HtmlInput2">
<! -EditableValueHolder is desigend for validator->
<Composite: editableValueHolder name = "inputField" target = "in"/>
<Composite: valueHolder name = "outputField" target = "out"/>
</Composite: interface>
<Composite: implementation>
<H: inputText id = "in" value = "# {inputBean. value}" required = "true"/>
<H: commandButton id = "clickButton" value = "Click Me! "ActionListener =" # {inputBean. print} "/>
<H: outputText id = "out" value = "# {inputBean. value}"/>
</Composite: implementation>
</Html>
Create an InputBean class under the com. freebird. component package.
Package com. freebird. component;
Import javax. faces. bean. ManagedBean;
Import javax. faces. bean. RequestScoped;
Import java. util. logging. Level;
Import java. util. logging. Logger;
Import javax. faces. event. ActionEvent;
/**
* Describe class InputBean here.
*
*
* Created: Fri Jan 7 14:15:36 2011
*
* @ Author <a href = "chenshumailto: chenshu @ csdesktop"> chenshu </a>
* @ Version 1.0
/
@ ManagedBean (name = "inputBean ")
@ RequestScoped
Public class InputBean {
/*
* Describe value here.
/
Private String value;
/*
* Creates a new InputBean instance.
*
/
Public InputBean (){
}
Private Logger getLogger (){
Return Logger. getLogger (getClass (). getName ());
}
Public void print (ActionEvent event ){
GetLogger (). info ("enter print method ");
}
/*
* Get the Value value.
*
* @ Return a String value
/
Public final String getValue (){
GetLogger (). info ("enter getValue:" + value );
Return value;
}
/*
* Set the Value value.
*
* @ Param newValue The new Value value.
/
Public final void setValue (final String newValue ){
GetLogger (). info ("enter setValue:" + newValue );
This. value = newValue;
}
}
In addition, remove the encodeBegin code in HtmlInput2.java. Note that the print method for blocking click events only prints a log. It is just to show that we can easily handle events internally.