struts2配置文件实现文本校验

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36759073/article/details/77852996

struts2提供了一个强大的组件,可以拦截请求过来的信息进行验证,下面就详细了解一下如何实现的。
1、首先创建一个需要校验的action,这里就以登录校验为例:
ValidateAction.java文件

package cn.jieou.struts.action;

import com.opensymphony.xwork2.ActionSupport;
public class ValidateAction extends ActionSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    //模拟登陆方法,成功返回success字符串
    public String login(){
        System.out.println("login: "+username+" "+password);
        return "success";
    }
}

2、在相应的action层下面创建一个ValidateAction-validation.xml文件,记住必须以需要验证的类的简称即类名+“-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">
<validators>
    <!-- name:指定校验action类中的属性名 -->
    <field name="username">
        <!-- type:struts2中提供的校验组件的名字 -->
        <field-validator type="requiredstring">
            <!-- name:校验类中对应参数 -->
            <param name="trim">true</param>
            <!-- 校验错误返回的信息 -->
            <message><![CDATA[用户名不能为空]]></message>
        </field-validator>
    </field>
    <field name="password">
        <field-validator type="stringlength">
            <param name="trim">true</param>
            <param name="maxLength">8</param>
            <param name="minLength">3</param>
            <message><![CDATA[密码必须在3~8位之间]]></message>
        </field-validator>
    </field>
</validators>

说明:如何写该配置文件
在源码中复制
这里写图片描述
查看struts2提供的校验方法
在xwork-core-2.3.33.jar下的com.opensymphony.xwork2.validator.validators下的default.xml文件中
这里写图片描述

下面是struts2默认的校验类

<!-- START SNIPPET: validators-default -->
<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="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>
<!--  END SNIPPET: validators-default -->

可以在该包下找到相应的校验类,查看类中的属性, validate()方法就是执行校验的方法

这里写图片描述

下面我列了几个比较常用的校验配置

<?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">
<validators>
    <!-- name:指定校验action类中的属性名 -->
    <field name="username">
        <!-- type:struts2中提供的校验组件的名字 -->
        <field-validator type="requiredstring">
            <!-- name:校验类中对应参数 -->
            <param name="trim">true</param>
            <!-- 校验错误返回的信息 -->
            <message><![CDATA[用户名不能为空]]></message>
        </field-validator>
    </field>
    <!-- 密码校验 -->
    <field name="password">
        <field-validator type="stringlength">
            <param name="trim">true</param>
            <param name="maxLength">8</param>
            <param name="minLength">3</param>
            <message><![CDATA[密码必须在3~8位之间]]></message>
        </field-validator>
    </field>
    <!-- 第二次输入密码校验 -->
    <field name="repsw">
        <field-validator type="fieldexpression">
            <param name="expression"><![CDATA[password==repsw]]></param>
            <message><![CDATA[两次输入密码必须一致]]></message>
        </field-validator>
    </field>
    <!-- 手机号码格式校验 -->
    <field name="phone">
        <field-validator type="regex">
            <param name="regex"><![CDATA[^1[3|4|5|8][0-9]\d{4,8}$]]></param>
            <message><![CDATA[手机号码格式不对]]></message>
        </field-validator>
    </field>
    <!-- 邮箱校验 -->
    <field name="email">
        <field-validator type="email">
            <message><![CDATA[必须是邮箱]]></message>
        </field-validator>
    </field>
</validators>
展开阅读全文

没有更多推荐了,返回首页