<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控件、轉(zhuǎn)換器和驗(yàn)證器實(shí)現(xiàn)了StateHolder接口表示組件具有了狀態(tài),可以保存一些組件自身的屬性。

    下面我們來看一個(gè)簡單的例子。
    這是一個(gè)正則表達(dá)式驗(yàn)證器的例子:
    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;
        
        
    /**
         * 出錯(cuò)時(shí)的提示內(nèi)容名稱,例如郵編
         
    */

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

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

        
    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標(biāo)簽:
    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;
        }


    }
    這個(gè)驗(yàn)證標(biāo)簽接受一個(gè)名稱作為參數(shù),通過此名稱可以從相關(guān)配置文件中查找到相應(yīng)的正則表達(dá)式和其他一些配置信息。

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

    如何保存驗(yàn)證器之前的狀態(tài)呢?或者說如何讓驗(yàn)證器不是瞬時(shí)狀態(tài)呢。
    這就需要實(shí)現(xiàn)StateHolder接口,并且實(shí)現(xiàn)幾個(gè)方法,讓JSF知道,這個(gè)驗(yàn)證器有自己的狀態(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;
        
        
    /**
         * 出錯(cuò)時(shí)的提示內(nèi)容名稱,例如郵編
         
    */

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

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

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


    }

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

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

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

    評論:
    # re: JSF(Java Server Faces)的StateHolder的作用和使用方法 2008-11-06 01:05 | Lf0x
    暫存,待看,O(∩_∩)O哈哈~  回復(fù)  更多評論
      
    # re: JSF(Java Server Faces)的StateHolder的作用和使用方法 2008-11-06 09:18 | TiGERTiAN
    @Lf0x
    呵呵,自己總結(jié)的,見笑了。  回復(fù)  更多評論
      
    主站蜘蛛池模板: 免费精品国产自产拍在线观看| 国产91色综合久久免费分享| 国产成人无码a区在线观看视频免费| 亚洲国产综合在线| 91精品国产免费久久国语蜜臀| 亚洲日本va中文字幕久久| 人人鲁免费播放视频人人香蕉| 中文字幕在线免费播放| 四虎永久免费观看| 国产亚洲精品成人久久网站| 免费在线观看a级毛片| 亚洲AV无码专区在线厂| 免费一级成人毛片| 一级中文字幕免费乱码专区| 亚洲女久久久噜噜噜熟女| 九九精品成人免费国产片| 久久狠狠高潮亚洲精品| 免费不卡在线观看AV| 亚洲AV综合色区无码二区爱AV| 成年女人免费视频播放77777| 亚洲日韩在线中文字幕综合| 亚洲国产精品第一区二区三区| JLZZJLZZ亚洲乱熟无码| 久久精品无码免费不卡| 97亚洲熟妇自偷自拍另类图片| www视频免费看| 亚洲AV日韩AV永久无码色欲 | 最近中文字幕mv免费高清在线| 亚洲天堂一区二区三区四区| 在线免费观看一级毛片| 黄色a三级三级三级免费看| 国产AⅤ无码专区亚洲AV | 在线看免费观看AV深夜影院 | 亚洲情综合五月天| 免费播放一区二区三区| 亚洲人成色777777精品| 国产偷国产偷亚洲高清日韩| 亚洲一区免费在线观看| 国产精品成人亚洲| 亚洲综合一区二区精品导航| 国产免费变态视频网址网站|