9.4. Handling JSF Input by Extending HtmlInputTextThe ZipCode class itself is a Java bean that transfers data between a JSF page and the application. It extends HtmlInputText, which handles the text from an input element such as a text field. ZipCode consists mostly of accessor methods for various properties, such as cityId and stateId. The important part of ZipCode is the constructor, which sets the rendererType to "ZipCode" through a call to the parent class's setRendererType( ) method. That rendererType must be the same as that returned by ZipCodeTag.getRendererType( ). The ZipCode class and the ZipCodeTag class look almost identical, but both are needed for this application. The ZipCode class gets input from the form, whereas the ZipCodeTag class configures the fields in the tag. Because the two classes are working with the same form fields, they have many of the same class variables, but they each extend different classes and have different methods (ZipCode extends HtmlInputText, and ZipCodeTag extends UIComponentTag). ZipCode is wired into the rest of the application by the faces-config.xml file, which must define the component that handles zip codes. A <component> tag in faces-config.xml contains a <component-type> tag that assigns the name "oreilly.ajax.ZipCode". ZipCodeTag has a method called getComponentType( ) that must return a matching component type: the string "oreilly.ajax.ZipCode". The <component-type> tag is really just an ID, and it can be assigned any value as long as it matches the string returned by ZipCodeTag.getComponentType( ). The code for the ZipCode class is presented in Example 9-9. Example 9-9. ZipCode.java
|