Section 9.4. Handling JSF Input by Extending HtmlInputText


9.4. Handling JSF Input by Extending HtmlInputText

The 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

 public class ZipCode extends HtmlInputText {     private String zipcodeId = "0";     private String stateId = "";     private String cityId = "";     private String url = "";     public ZipCode( ) {         super( );         setRendererType("ZipCode");     }     public String getCityId( ) {         return cityId;     }     public void setCityId(String cityId) {         this.cityId = cityId;     }     public String getStateId( ) {         return stateId;     }     public void setStateId(String stateId) {         this.stateId = stateId;     }     public String getUrl( ) {         return url;     }     public void setUrl(String url) {         this.url = url;     }     public String getZipcodeId( ) {         return zipcodeId;     }     public void setZipcodeId(String zipcodeId) {         this.zipcodeId = zipcodeId;     } } 




Ajax on Java
Ajax on Java
ISBN: 0596101872
EAN: 2147483647
Year: 2007
Pages: 78

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net