亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

JSF1.2自定義驗證器的實現

系統 1802 0

這兩天有個朋友在做jsf自定義驗證器時遇到了一些問題,問了我。我整了好久也沒能搞明白,后來發現可能是他實現自定義驗證器時使用的類太老(項目用的是jsf1.2,自定義驗證器時卻用的jsf1.1的類-ValidatorTag,這個類在jsf1.2中已經被建議不使用,這位朋友在實現時用的tld標簽也是2.0之上的,我也不確定問題是否出在這里)。

?

下班后沒事就在jsf1.2的庫上,在參考網上一些資料的情況下,自己寫了一個自定義驗證器的demo。該例子用途是驗證email格式。現貼出代碼:)

?

package test.managebean下的class:

    package test.managebean;

/**
 *
 * @author SailingLee
 */
public class TestManageBean {

    private String postCode;

   
   
    public void doSomeThing(){
        System.out.println("============doSomeThing============");
    }

    /**
     * @return the postCode
     */
    public String getPostCode() {
        return postCode;
    }

    /**
     * @param postCode the postCode to set
     */
    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }
}

  

?

package test.validator下的class:

    package test.validator;

import java.io.Serializable;
//import java.text.MessageFormat;
import java.util.regex.Pattern;
//import java.util.Locale;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.application.FacesMessage;

/**
 *
 * @author SailingLee
 */
public class CustomValidator implements Validator, Serializable {

    private String length;
    private String regex;
    private String errorSummary;
    private String errorDetail;

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        FacesMessage message = null;
        if (value == null || value.toString().trim().length() == 0) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "輸入不能為空!", "您的輸入為空,請輸入!");
            //return;
        }else if (value.toString().length() > Integer.parseInt(getLength())) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "輸入長度超過" + getLength() + "位!", "輸入長度超過" + getLength() + "位!請重新輸入!");
            //return;
        }else if (regex != null && !regex.equals("")) {
            boolean b = Pattern.matches(regex, value.toString());
            if (!b) {
                message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "輸入的郵編格式錯誤!", "輸入的郵編格式錯誤!請確認!");
                //return;
            }
        }
        throw new ValidatorException(message);

    }



    /**
     * @return the errorSummary
     */
    public String getErrorSummary() {
        return errorSummary;
    }

    /**
     * @param errorSummary the errorSummary to set
     */
    public void setErrorSummary(String errorSummary) {
        this.errorSummary = errorSummary;
    }

    /**
     * @return the errorDetail
     */
    public String getErrorDetail() {
        return errorDetail;
    }

    /**
     * @param errorDetail the errorDetail to set
     */
    public void setErrorDetail(String errorDetail) {
        this.errorDetail = errorDetail;
    }

    /**
     * @return the regex
     */
    public String getRegex() {
        return regex;
    }

    /**
     * @param regex the regex to set
     */
    public void setRegex(String regex) {
        this.regex = regex;
    }

    /**
     * @return the length
     */
    public String getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(String length) {
        this.length = length;
    }


}

  

?

package test.tag下的class:

    package test.tag;

import test.validator.CustomValidator;
import javax.faces.validator.Validator;
import javax.faces.webapp.ValidatorELTag;
import javax.faces.context.FacesContext;
import javax.servlet.jsp.JspException;
import javax.el.ValueExpression;
import javax.el.ELContext;

/**
 *
 * @author SailingLee
 */
public class CustomValidatorTag extends ValidatorELTag {

    private ValueExpression length;
    private ValueExpression regex;
    private ValueExpression errorSummary;
    private ValueExpression errorDetail;

    @Override
    protected Validator createValidator() throws JspException {
        
        CustomValidator validator = new CustomValidator();
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        validator.setLength((String) length.getValue(elContext));
        validator.setRegex((String) regex.getValue(elContext));
        if (errorSummary != null)
            validator.setErrorSummary((String) errorSummary.getValue (elContext));
        if (errorDetail != null)
            validator.setErrorDetail((String) errorDetail.getValue (elContext));
        return validator;
    }

    /**
     * @param length the length to set
     */
    public void setLength(ValueExpression length) {
        this.length = length;
    }

    /**
     * @param regex the regex to set
     */
    public void setRegex(ValueExpression regex) {
        this.regex = regex;
    }

    /**
     * @param errorSummary the errorSummary to set
     */
    public void setErrorSummary(ValueExpression errorSummary) {
        this.errorSummary = errorSummary;
    }

    /**
     * @param errorDetail the errorDetail to set
     */
    public void setErrorDetail(ValueExpression errorDetail) {
        this.errorDetail = errorDetail;
    }

    public void release() {
        length = null;
        regex = null;
        errorSummary = null;
        errorDetail = null;
    }
}

  

?

    <?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config version="1.2" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

<managed-bean>
    <managed-bean-name>testManageBean</managed-bean-name>
    <managed-bean-class>test.managebean.TestManageBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<validator><!--看見網上很多例子都在這里加上下面兩行,但經過測試,沒有這兩行也完全沒影響。(看了下,發現這里的配置只在jsf1.1中需要) -->
    <validator-id>test.validator.CustomValidator</validator-id>
    <validator-class>test.validator.CustomValidator</validator-class>
</validator>
</faces-config>

  

?

    <?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
    <tlib-version>1.0</tlib-version>
    <short-name>customvalidator</short-name>
    <uri>/WEB-INF/CustomValidator</uri>
    <tag>
        <name>email</name>
        <tag-class>test.tag.CustomValidatorTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>length</name>
            <required>true</required>
            <deferred-value>
                <type>java.lang.String</type>
            </deferred-value>
        </attribute>
        <attribute>
            <name>regex</name>
            <required>true</required>
            <deferred-value>
                <type>java.lang.String</type>
            </deferred-value>
        </attribute>
        <attribute>
            <name>errorSummary</name>
            <deferred-value>
                <type>java.lang.String</type>
            </deferred-value>
        </attribute>
        <attribute>
            <name>errorDetail</name>
            <deferred-value>
                <type>java.lang.String</type>
            </deferred-value>
        </attribute>
    </tag>
</taglib>

  

?

        <jsp-config>
        <taglib>
            <taglib-uri>http://email.com</taglib-uri>
            <taglib-location>/WEB-INF/CustomValidator.tld</taglib-location>
        </taglib>
    </jsp-config>
  

?

頁面代碼:

    <%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="custom" uri="http://email.com"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Custom Validate Email</title>
        </head>
        <body>
            
            <f:view>
                <h:form id="myform">
                    Email:<h:inputText id="postCode" value="#{testManageBean.postCode}">
                        <custom:email length="10" regex="[a-z]*@[a-z]*.[a-z]*"/>
                    </h:inputText>
                    <h:commandButton action="#{testManageBean.doSomeThing}" value="validate email"/>
                </h:form>
                <h:messages/>
            </f:view>
        </body>
    </html>


  

?

使用類庫是jsf1.2版本的。

效果圖如下:

自定義Email格式驗證器

:該例子中關于自定義errorSummary和errorDetail的功能沒有實現。

JSF1.2自定義驗證器的實現


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产农村妇女毛片精品久久 | 香蕉观看在线视频成人 | 神马午夜剧场 | 久久毛片免费看一区二区三区 | 日韩黄色录像 | 美女啪啪免费网站 | 亚洲欧洲视频在线 | 第一福利在线观看永久视频 | 欧美日韩综合高清一区二区 | 夜夜摸视频网 | 亚洲综合伦理一区 | 天天干天天射天天插 | 亚洲福利精品一区二区三区 | 99色视频在线观看 | 国产成人永久免费视 | a级做爰片毛片视频 | 农村寡妇一级毛片免费播放 | 国产亚洲综合色就色 | 日韩成人免费一级毛片 | 午夜视频精品 | 国产精品1区2区3区 国产精品1区2区3区在线播放 | 欧美伊香蕉久久综合类网站 | 亚洲va国产日韩欧美精品色婷婷 | 久草精品在线播放 | 国产亚洲精品免费 | 亚洲图色视频 | 日本香蕉网 | 亚洲精品不卡视频 | 中文字幕久久综合 | 日韩欧美毛片免费看播放 | 久草网在线观看 | 韩国日本三级在线观看 | 国产精品中文 | 国产亚洲欧美成人久久片 | 天天做天天做天天综合网 | 久久激情五月 | 亚洲日本欧美产综合在线 | 免费乱人伦 | 久久久久女人精品毛片 | 四虎永久网址在线观看 | 成人国产视频在线观看 |