隨筆-348  評論-598  文章-0  trackbacks-0
          UI控件、轉(zhuǎn)換器和驗證器實現(xiàn)了StateHolder接口表示組件具有了狀態(tài),可以保存一些組件自身的屬性。

          下面我們來看一個簡單的例子。
          這是一個正則表達式驗證器的例子:
          public class RegexValidator implements Validator
          {
              
          /**
               * The message identifier of the Message to be created if
               * the validation fails.
               
          */

              
          public static final String REGEX_INVALID_MESSAGE_ID =
                  
          "validator.Regex_Invalid";
              
              
              
          private String formatPatterns = null;
              
              
          /**
               * 出錯時的提示內(nèi)容名稱,例如郵編
               
          */

              
          private String errorPatternDisplayName = null;
              
              
          /**
               * 獲得樣式的配置文件
               
          */

              
          private static final ResourceBundle bundle = ResourceBundle.getBundle(Const.BUNDLE_ROOT + ".RegexPattern");
              
              
          /**
               * 資源配置文件中對應的樣式名稱
               
          */

              
          private String formatPatternName = null;
              
              
          public RegexValidator()
              
          {
                  
          super();
              }

              
              
          public RegexValidator(String formatPatternName)
              
          {
                  setFormatPatternName(formatPatternName);
              }

              
              
          public void validate(FacesContext context, UIComponent component, Object toValidate)
                      
          throws ValidatorException
              
          {
                  
                  
          if(context == null || component == null)
                      
          throw new NullPointerException();
                  
                  
          if(!(component instanceof UIOutput))
                      
          return;
                  
                  
          if(formatPatterns == null || formatPatterns.length() == 0 || null == toValidate)
                      
          return;
                  
                  String value 
          = toValidate.toString();
                  Pattern p 
          = Pattern.compile(this.formatPatterns);
                  Matcher m 
          = p.matcher(value);
                  
          boolean b = m.matches();
                  
          if(!b)
                  
          {
                      FacesMessage errMsg 
          = MessageFactory.getMessage(context, 
                              
          this.REGEX_INVALID_MESSAGE_ID, 
                              
          new Object[]{errorPatternDisplayName});

                      
          throw new ValidatorException(errMsg);
                  }

              }


              
          public String getFormatPatternName()
              
          {
                  
          return formatPatternName;
              }


              
          public void setFormatPatternName(String formatPatternName)
              
          {
                  
          this.formatPatternName = formatPatternName;
                  
          this.errorPatternDisplayName = bundle.getString(formatPatternName);
                  
          this.formatPatterns = bundle.getString(formatPatternName+"_patterns");
               
              }



          }

          它的Tag標簽:
          public class RegexValidatorTag extends ValidatorELTag
          {
              
          private String formatPatternName;
              
              
          public RegexValidatorTag()
              
          {
                  
          super();
              }

              
              
          /* (non-Javadoc)
               * @see javax.faces.webapp.ValidatorELTag#createValidator()
               
          */

              @Override
              
          protected Validator createValidator() throws JspException
              
          {
                  RegexValidator v 
          = new RegexValidator();;
                  v.setFormatPatternName(formatPatternName);
                  
          return v;
              }

              
          public String getFormatPatternName()
              
          {
                  
          return formatPatternName;
              }

              
          public void setFormatPatternName(String formatPatternName)
              
          {
                  
                  
          this.formatPatternName = formatPatternName;
              }


          }
          這個驗證標簽接受一個名稱作為參數(shù),通過此名稱可以從相關配置文件中查找到相應的正則表達式和其他一些配置信息。

          但如果你使用這個驗證器,你會發(fā)現(xiàn),每次都正確調(diào)用了,也都將參數(shù)傳進去了,但是在調(diào)用validate方法的時候卻發(fā)現(xiàn)自定義的幾個驗證器的屬性的值都為null。這是為什么呢?
          因為我們第一次調(diào)用的時候初始化了一下,參數(shù)都進去了,驗證器也被實例化了,但是這個驗證器卻是瞬時狀態(tài)的,剛被頁面實例化好就被釋放了。所以提交表單驗證的時候會重新被初始化,但這時只是調(diào)用了默認構造函數(shù),沒有將我們的正則表達式樣式作為參數(shù)傳進去。

          如何保存驗證器之前的狀態(tài)呢?或者說如何讓驗證器不是瞬時狀態(tài)呢。
          這就需要實現(xiàn)StateHolder接口,并且實現(xiàn)幾個方法,讓JSF知道,這個驗證器有自己的狀態(tài)需要保存。
          新的代碼:
          public class RegexValidator implements Validator, StateHolder
          {
              
          /**
               * The message identifier of the Message to be created if
               * the validation fails.
               
          */

              
          public static final String REGEX_INVALID_MESSAGE_ID =
                  
          "validator.Regex_Invalid";
              
              
              
          private String formatPatterns = null;
              
              
          /**
               * 出錯時的提示內(nèi)容名稱,例如郵編
               
          */

              
          private String errorPatternDisplayName = null;
              
              
          /**
               * 獲得樣式的配置文件
               
          */

              
          private static final ResourceBundle bundle = ResourceBundle.getBundle(Const.BUNDLE_ROOT + ".RegexPattern");
              
              
          /**
               * 資源配置文件中對應的樣式名稱
               
          */

              
          private String formatPatternName = null;
              
              
          public RegexValidator()
              
          {
                  
          super();
              }

              
              
          public RegexValidator(String formatPatternName)
              
          {
                  setFormatPatternName(formatPatternName);
              }

              
              
          public void validate(FacesContext context, UIComponent component, Object toValidate)
                      
          throws ValidatorException
              
          {
                  
                  
          if(context == null || component == null)
                      
          throw new NullPointerException();
                  
                  
          if(!(component instanceof UIOutput))
                      
          return;
                  
                  
          if(formatPatterns == null || formatPatterns.length() == 0 || null == toValidate)
                      
          return;
                  
                  String value 
          = toValidate.toString();
                  Pattern p 
          = Pattern.compile(this.formatPatterns);
                  Matcher m 
          = p.matcher(value);
                  
          boolean b = m.matches();
                  
          if(!b)
                  
          {
                      FacesMessage errMsg 
          = MessageFactory.getMessage(context, 
                              
          this.REGEX_INVALID_MESSAGE_ID, 
                              
          new Object[]{errorPatternDisplayName});

                      
          throw new ValidatorException(errMsg);
                  }

              }


              
          public String getFormatPatternName()
              
          {
                  
          return formatPatternName;
              }


              
          public void setFormatPatternName(String formatPatternName)
              
          {
                  
          this.formatPatternName = formatPatternName;
                  
          this.errorPatternDisplayName = bundle.getString(formatPatternName);
                  
          this.formatPatterns = bundle.getString(formatPatternName+"_patterns");
               
              }


              
          private boolean transientValue = false;
              
              
          public void setTransient(boolean transientValue)
              
          {
                  
          this.transientValue = transientValue;
              }

              
              
          public boolean isTransient()
              
          {
                  
          return this.transientValue;
              }


              
          public void restoreState(FacesContext context, Object state)
              
          {
                  Object values[] 
          = (Object[]) state;
                  formatPatterns 
          = (String) values[0];
                  errorPatternDisplayName 
          = (String) values[1];
              }


              
          public Object saveState(FacesContext context)
              
          {
                  Object[] values 
          = new Object[2];
                  values[
          0= formatPatterns;
                  values[
          1= errorPatternDisplayName;
                  
          return values;
              }


          }

          實現(xiàn)setTransient和isTransient兩個方法是為了標明這個驗證器不是瞬時狀態(tài),需要返回一個false。
          實現(xiàn)saveState和restoreState兩個方法是為了保存和還原狀態(tài),大家可以看下代碼,saveState保存了當前驗證器需要的幾個屬性參數(shù),而restoreState將這些參數(shù)重新還原給了驗證器,這樣,我們使用新代碼做驗證的時候,就會發(fā)現(xiàn)它起作用了。

          ---------------------------------------------------------
          專注移動開發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2008-11-05 14:15 TiGERTiAN 閱讀(1662) 評論(2)  編輯  收藏 所屬分類: JavaJSF

          評論:
          # re: JSF(Java Server Faces)的StateHolder的作用和使用方法 2008-11-06 01:05 | Lf0x
          暫存,待看,O(∩_∩)O哈哈~  回復  更多評論
            
          # re: JSF(Java Server Faces)的StateHolder的作用和使用方法 2008-11-06 09:18 | TiGERTiAN
          @Lf0x
          呵呵,自己總結的,見笑了。  回復  更多評論
            
          主站蜘蛛池模板: 仁怀市| 湘乡市| 云阳县| 灵璧县| 凤城市| 巴里| 遂溪县| 朝阳区| 揭西县| 汽车| 苏州市| 汕尾市| 昔阳县| 武乡县| 微山县| 句容市| 成武县| 兖州市| 寿宁县| 诏安县| 甘南县| 宜城市| 松阳县| 文安县| 林周县| 雷州市| 江山市| 临猗县| 祁阳县| 如东县| 潮州市| 镇安县| 池州市| 黑水县| 高台县| 延寿县| 彭泽县| 来凤县| 安龙县| 边坝县| 陇南市|