一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Struts2框架驗(yàn)證

 擎天豬mpnlajkd 2017-06-14
參考項(xiàng)目

整體目錄 
 
前臺(tái)頁面
<body>
<s:actionerror/>
用戶注冊
<s:form action="/registerAction" method="post">
<s:textfield name="user.userName" label="用戶名"></s:textfield>
<s:textfield name="user.name" label="真實(shí)姓名"></s:textfield>
<s:textfield name="user.age" label="年齡"></s:textfield>
<s:textfield name="user.email" label="郵件"></s:textfield>
<s:textfield name="user.homePage" label="主頁"></s:textfield>
<s:submit value="注冊"></s:submit>
</s:form>
</body>

Action層
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}

@Override
public String execute() throws Exception {
}
}

驗(yàn)證
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts./dtds/xwork-validator-1.0.2.dtd">

<validators>
    <field name="user.userName">
        <field-validator type="requiredstring">
            <message>請輸入用戶名</message>
        </field-validator>
         <field-validator type="stringlength">
          <param name="minLength">6</param>
          <param name="maxLength">10</param>
            <message>用戶名必須在${minLength}和${maxLength}之間</message>
        </field-validator>
    </field>
    <field name="user.name">
    <field-validator type="requiredstring">
            <message>請輸入姓名</message>
        </field-validator>
    </field>
    <field name="user.age">
    <field-validator type="int">
    <param name="min">18</param>
            <message>年齡必須滿18周歲</message>
        </field-validator>
    </field>
    <field name="user.email">
    <field-validator type="requiredstring">
            <message>請輸入郵件</message>
        </field-validator>
        <field-validator type="email">
            <message>郵件格式不對</message>
        </field-validator>
    </field>
    <field name="user.homePage">
    <field-validator type="requiredstring">
            <message>請輸入主頁</message>
        </field-validator>
        <field-validator type="url">
            <message>主頁格式不對</message>
        </field-validator>
    </field>  
    <validator type="expression">
    <param name="expression"><![CDATA[!user.name.equals(user.userName)]]></param>
    <message>用戶名和真實(shí)姓名不能相同</message>
    </validator>
</validators>

自定義驗(yàn)證
整體目錄
 
validators.xml 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Config 1.0//EN"
        "http://struts./dtds/xwork-validator-config-1.0.dtd">
<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
    <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
    
    <validator name="sensitive" class="com.java1234.validators.SensitiveWordValidators"/>
</validators>

Action層
import com.opensymphony.xwork2.ActionSupport;
public class ValidationAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
}
}

action-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts./dtds/xwork-validator-1.0.2.dtd">

<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>請輸入姓名</message>
        </field-validator>
        
        <field-validator type="sensitive">
            <message>有敏感詞匯</message>
        </field-validator>
    </field>
    
</validators>

自定義驗(yàn)證
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class SensitiveWordValidators extends FieldValidatorSupport{

@Override
public void validate(Object object) throws ValidationException {
String fieldName=this.getFieldName();
String value=this.getFieldValue(fieldName, object).toString();
if(!check(value)){
this.addFieldError(fieldName, object);
}
}
public boolean check(String value){
String sensitiveWords[]={"操","草"};
for(int i=0;i<sensitiveWords.length;i++){
if(value.indexOf(sensitiveWords[i])>-1){
return false;
}
}
return true;
}
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多

    亚洲日本韩国一区二区三区| 亚洲免费黄色高清在线观看| 亚洲国产欧美久久精品| 欧美六区视频在线观看| 91亚洲精品亚洲国产| 老熟妇2久久国内精品| 夜夜嗨激情五月天精品| 欧美三级不卡在线观线看| 亚洲国产黄色精品在线观看| 91精品日本在线视频| 字幕日本欧美一区二区| 日韩黄片大全免费在线看| 日韩夫妻午夜性生活视频| 美女黄色三级深夜福利| 欧美成人一区二区三区在线| 亚洲欧美日本国产不卡| 91精品日本在线视频| 日韩人妻免费视频一专区| 91人妻人人澡人人人人精品| 国产一区二区熟女精品免费| 激情图日韩精品中文字幕| 欧美区一区二在线播放| 国产av一区二区三区麻豆| 精品欧美日韩一区二区三区| 精品al亚洲麻豆一区| 国产亚洲欧美日韩国亚语| 色哟哟精品一区二区三区| 成人午夜在线视频观看| 亚洲av熟女国产一区二区三区站| 国自产拍偷拍福利精品图片| 国产精品福利一二三区| 久久精品伊人一区二区| 国产亚洲精品久久久优势| 日本人妻丰满熟妇久久| 一二区中文字幕在线观看| 精品日韩av一区二区三区| 国产三级欧美三级日韩三级| 日本在线视频播放91| 欧美日韩国产一级91| 精品午夜福利无人区乱码| 国产亚洲欧美日韩精品一区 |