<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 5,  comments - 7,  trackbacks - 0
    UI控件、轉換器和驗證器實現了StateHolder接口表示組件具有了狀態,可以保存一些組件自身的屬性。

    下面我們來看一個簡單的例子。
    這是一個正則表達式驗證器的例子:
    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
    ;
        
        
    /**
         * 出錯時的提示內容名稱,例如郵編
         
    */

        
    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;
        }


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

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

    如何保存驗證器之前的狀態呢?或者說如何讓驗證器不是瞬時狀態呢。
    這就需要實現StateHolder接口,并且實現幾個方法,讓JSF知道,這個驗證器有自己的狀態需要保存。
    新的代碼:
    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
    ;
        
        
    /**
         * 出錯時的提示內容名稱,例如郵編
         
    */

        
    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;
        }


    }

    實現setTransient和isTransient兩個方法是為了標明這個驗證器不是瞬時狀態,需要返回一個false。
    實現saveState和restoreState兩個方法是為了保存和還原狀態,大家可以看下代碼,saveState保存了當前驗證器需要的幾個屬性參數,而restoreState將這些參數重新還原給了驗證器,這樣,我們使用新代碼做驗證的時候,就會發現它起作用了。
    posted on 2008-11-23 01:04 Vincent-chen 閱讀(240) 評論(0)  編輯  收藏 所屬分類: JSF
    主站蜘蛛池模板: 国产成人无码免费网站| 亚洲制服在线观看| 一个人免费观看视频在线中文| 四虎国产精品免费久久| 亚洲图片校园春色| 可以免费看黄的网站| 亚洲狠狠ady亚洲精品大秀| 曰批全过程免费视频网址 | 男女猛烈无遮掩视频免费软件| 永久免费观看的毛片的网站| 亚洲av日韩精品久久久久久a| 成年女人永久免费观看片| 午夜在线亚洲男人午在线| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲中文字幕不卡无码| 久久精品免费网站网| 亚洲va无码手机在线电影| 日韩av无码久久精品免费| 亚洲天堂福利视频| 在线免费观看污网站| 免费国产在线精品一区| 久久精品国产亚洲av麻| 欧洲乱码伦视频免费| 亚洲日韩精品无码专区加勒比 | 看免费毛片天天看| 成人午夜亚洲精品无码网站| 99热免费在线观看| 亚洲精品无码久久久久牙蜜区| 免费人成在线观看网站视频| 国产一级a毛一级a看免费视频| 亚洲视频在线观看网址| 日韩免费无砖专区2020狼| 一级毛片在线播放免费| 亚洲精品在线不卡| 免费一级国产生活片| 99精品视频免费观看| 久久亚洲精品成人无码| 人人狠狠综合久久亚洲88| 毛片免费在线观看网站| 免费无码又爽又刺激网站直播 | 96免费精品视频在线观看|