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

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

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

    隨筆-348  評論-598  文章-0  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將這些參數重新還原給了驗證器,這樣,我們使用新代碼做驗證的時候,就會發現它起作用了。

    ---------------------------------------------------------
    專注移動開發

    Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
    posted on 2008-11-05 14:15 TiGERTiAN 閱讀(1643) 評論(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
    呵呵,自己總結的,見笑了。  回復  更多評論
      
    主站蜘蛛池模板: 色www永久免费视频| 99久久国产免费中文无字幕| 成年性生交大片免费看| 337p欧洲亚洲大胆艺术| 麻豆精品成人免费国产片| 国产成人麻豆亚洲综合无码精品 | 91禁漫免费进入| 亚洲免费视频网站| 久久精品人成免费| 亚洲大片免费观看| 黄色免费网站网址| 亚洲精品123区在线观看| 免费精品国偷自产在线在线| 亚洲三级中文字幕| 我想看一级毛片免费的| 国产在亚洲线视频观看| 亚洲精品无码AV中文字幕电影网站| 一级做a爰片久久毛片免费陪 | 2048亚洲精品国产| 国色精品va在线观看免费视频| 亚洲AV无码一区二区二三区入口 | 国产亚洲一区区二区在线| 三年片在线观看免费| 久久亚洲AV成人无码电影| 四虎免费影院ww4164h| 亚洲人成电影网站免费| mm1313亚洲国产精品美女| 中文在线观看永久免费| 亚洲国产精品热久久| 四虎国产精品免费久久| 黄页视频在线观看免费| 亚洲精品蜜桃久久久久久| 国产电影午夜成年免费视频| 亚洲av无码专区国产不乱码| 在线观看国产区亚洲一区成人 | 一级做受视频免费是看美女| 久久久久亚洲av无码尤物| 免费无码AV电影在线观看| 免费一级毛suv好看的国产网站| 久热综合在线亚洲精品| 午夜免费福利影院|