struts系列:校验(一)XML校验和函数方法校验

一、jsp示例

<form action="login" method="post">
      <div class="form-group has-feedback">
          <s:fielderror></s:fielderror>  //重点,放一块报错信息的空间
      </div>
      <div class="form-group has-feedback">
        <input name="name" type="text" class="form-control" placeholder="Email" value="">
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input name="password" type="password" class="form-control" placeholder="Password" value="">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-8">
          <div class="checkbox icheck">
            <label>
              <input type="checkbox"> Remember Me
            </label>
          </div>
        </div>
        <!-- /.col -->
        <div class="col-xs-4">
          <button type="submit" class="btn btn-primary btn-block btn-flat"><s:text name="loginSubmit"/></button>
        </div>
        <!-- /.col -->
      </div>
    </form>

二、action示例

@SuppressWarnings("serial")
    public class LoginAction extends ActionSupport {
    private static final transient Logger logger = LogManager.getLogger(LoginAction.class);
    private String name;
    private String password;

    public String execute() {
        logger.debug("name={},password={}", name, password);
        HttpServletRequest request = ServletActionContext.getRequest();
        logger.debug("get locale = {}", request.getLocale());
        if ("lings".equals(name)) {
            return SUCCESS;
        } else {
            return INPUT;
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

三、struts.xml示例

     <action name="login" method="execute" class="com.sanro.strutsDemo.action.LoginAction">
            <result name="success" type="tiles">tiger</result>  //成功的跳转
            <result name="input">/index.jsp</result>            //校验失败的跳转
        </action>

四、校验方式

1. XML校验

可以针对每个action建立一个校验文件,例如以示例action为例,需要建立一个LoginAction-validation.xml。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
          "-//Apache Struts//XWork Validator 1.0.3//EN"
          "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
        <!-- DTD文件信息见struts2-core-2.x.x.jar中的具体情况 -->
<validators>
    <!--验证的每一个字段用filed表示 -->
    <!--验证用户名 -->
    <field name="name">
        <!--指定使用的验证器,此处选择非空验证 -->
        <field-validator type="requiredstring">
            <!--验证失败的错误提示信息 -->
            <message>用户名不能为空</message>
        </field-validator>
    </field>

    <!--验证pwd -->
    <field name="password">
        <!--密码非空 -->
        <field-validator type="requiredstring">
            <message>密码不能为空</message>
        </field-validator>

        <!--密码长度验证 -->
        <field-validator type="stringlength">
            <param name="minLength">6</param>
            <param name="maxLength">8</param>
            <message>密码必须为6-8位!</message>
        </field-validator>
    </field>
</validators>

2. 在继承ActionSupport的action类中覆写validate()方法

@SuppressWarnings("serial")
    public class LoginAction extends ActionSupport {
    .....
    @Override
    public void validate() {
        logger.debug("firstly, validate!!!");
//        super.validate();
        if (!password.equals("123456")) {
            this.addFieldError("password", "对不起,password is error!");
        }
        
    }
    ......

附录一、更多的内置校验器

struts2-core-2.x.x.jar\com\opensymphony\xwork2\validator\validators\default.xml
<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="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator" /> 
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator" /> 
    <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="creditcard" class="com.opensymphony.xwork2.validator.validators.CreditCardValidator" /> 
    <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" /> 
  </validators>

附录二、自定义校验器

XML自带的校验器有时不能满足我们的校验需求,可以考虑自建一个校验器。
 
posted @ 2018-07-17 22:46  一沙世界  阅读(533)  评论(0编辑  收藏  举报