JSF自定义组件之三 JSF实现-Component

 本篇将介绍Compmonent类的开发。

    Tag类主要作用是从页面标签接收值,Component类的主要作用为在Server端保存及恢复组件状态。

    在Tag类中主要需实现getComponentType,getRendererType,setProperties及属性的set方法,Component类主要需实现的方法包括:getFamily,saveState,restoreState和属性的get/set方法。

    本例中Component类直接继承HtmlInputText类,代码如下:


  1. package net.moon.jsf.customer.component;

  2. import javax.faces.component.html.HtmlInputText;
  3. import javax.faces.context.FacesContext;

  4. public class HtmlDropdownList extends HtmlInputText {
  5.     private static final String FAMILY_NAME = "DropdownList";

  6.     private String valueList = null;
  7.     public void setValueList(String valueList) {
  8.         this.valueList = valueList;
  9.     }

  10.     private String image = null;

  11.     public String getValueList() {
  12.         return valueList;
  13.     }

  14.     public String getImage() {
  15.         return image;
  16.     }

  17.     public void setImage(String image) {
  18.         this.image = image;
  19.     }

  20.     /**
  21.      * @param args
  22.      */
  23.     public static void main(String[] args) {
  24.         // TODO Auto-generated method stub

  25.     }

  26.     public HtmlDropdownList() {
  27.         super();
  28.         // TODO Auto-generated constructor stub
  29.     }
  30.     
  31.     @Override
  32.     public String getFamily() {
  33.         return FAMILY_NAME;
  34.     }

  35.     @Override
  36.     public void restoreState(FacesContext _context, Object _state) {
  37.         // TODO Auto-generated method stub
  38.         Object[] value = (Object[]) _state;
  39.         super.restoreState(_context, value[0]);
  40.         valueList = (String) value[1];
  41.         image = (String) value[2];
  42.     }

  43.     @Override
  44.     public Object saveState(FacesContext _context) {
  45.         // TODO Auto-generated method stub
  46.         Object[] value = new Object[3];
  47.         value[0] = super.saveState(_context);
  48.         value[1] = valueList;
  49.         value[2] = image;
  50.         return value;
  51.     }
  52. }

匿名2008-09-01 20:43:3959.41.222.*

快速查询