Andy 胡

导航

Struts2:效验器——注解

效验器三类:

编程式——Java代码

声明式——xml

注释法——@

注解验证可以修饰属性的getter方法,
也可以修饰执行方法
Action中校验失败时,返回input逻辑视图

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>                                    
<!DOCTYPE struts PUBLIC                                    
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"                                
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <package name="p1" namespace="/" extends="struts-default">
        <action name="*Action" class="org.ah.s2.C1" method="{1}">
            <result name="success" type="dispatcher">
                /inputMsg.jsp
            </result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

 

 

Action:(没有:C1-validation.xml)

package org.ah.s2;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.CustomValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class C1 extends ActionSupport {
    private String uname;
    private String pwd;
    private String pwd2;
    private Date birth;
    private int weight;

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPwd2() {
        return pwd2;
    }

    public void setPwd2(String pwd2) {
        this.pwd2 = pwd2;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    // 不能写在Model上
    // set方法不能丢
    // parameters是个数组,用{}包含
    @RequiredStringValidator(trim = true, message = "必须输入名字")
    @CustomValidator(type = "cn", parameters = { @ValidationParameter(name = "model", value = "all") }, message = "要中文")
    public String getUname() {
        return uname;
    }

    @Validations(
  requiredStrings
= { @RequiredStringValidator(fieldName = "pwd", message = "密码必须输入") },
  intRangeFields = { @IntRangeFieldValidator(fieldName = "weight", min = "1", max = "300", message = "体重必须在${min}和${max}之间") }
  )
public String m1() { return "success"; } }

 

自定义效验器:

package org.ah.s2;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class MyValidator extends FieldValidatorSupport {
    private String model;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    public void validate(Object obj_i) throws ValidationException {
        // 字段名
        String fieldName = getFieldName();
        // 字段值
        Object fieldValue = getFieldValue(fieldName, obj_i);

        if (fieldValue instanceof String) {
            String strV = (String) fieldValue;
            /*
             * 中文unicode none:无中文 all :全中文
             */
            if ("none".equals(model)) {
                // compile:编译
                Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
                Matcher m = p.matcher(strV);

                if (m.find()) {// 找到中文
                    // 效验失败(显示不出来,随便写写,不写不报错)
                    addFieldError(fieldName, obj_i);
                }
            } else if ("all".equals(model)) {
                if (!Pattern.matches("[\u4e00-\u9fa5]+", strV)) {
                    // 不是全中文
                    addFieldError(fieldName, obj_i);// 效验失败
                }
            }
        }
    }

}

src下配置自定义的效验器:

validators.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Definition 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">
<validators>
    <validator name="cn" class="org.ah.s2.MyValidator" />
</validators>

表示层:

index.jsp

    <s:form action="m1Action" method="post">
        <s:textfield name="uname" label="用户名" />
        <s:textfield name="pwd" label="密码" />
        <s:textfield name="pwd2" label="确认密码" />
        <s:textfield name="birth" label="生日" />
        <s:textfield name="weight" label="体重" />
        <s:submit value="提交"></s:submit>
    </s:form>

inputMsg.jsp 略

运行结果:

 

posted on 2017-01-03 00:29  talkwah  阅读(182)  评论(0编辑  收藏  举报