博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Webx之表单验证
阅读量:5015 次
发布时间:2019-06-12

本文共 7142 字,大约阅读时间需要 23 分钟。

引入服务器端表单验证service,是通过在webx.xml中通过服务引入的方式完成的。例如,在user相关信息的表单验证的产生过程是这样的:webx-user.xml通过 <beans:import resource="user/form.xml" />引入form,xml表单验证配置,在form.xml中,通过services:form开始了表单的验证工作。 

Webx表单验证服务主张验证逻辑和页面表现逻辑完全分离。所有的验证规则都写在一个单独的配置文件中 —— 页面模板是不需要关心这些验证规则的。当你需要修改验证规则时,只需要修改独立的配置文件就可以了,并不用修改页面模板。 
开始配置表单验证服务。 
每个表单验证服务可包含多个groups。 
每个group可包含多个fields。 
每个field可包含多个validators。 
 
user/form.xml具体配置如下: 
<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:services="http://www.alibaba.com/schema/services" 
    xmlns:fm-conditions="http://www.alibaba.com/schema/services/form/conditions" 
    xmlns:fm-validators="http://www.alibaba.com/schema/services/form/validators" 
    xmlns="http://www.alibaba.com/schema/services/form/validators" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
        http://www.alibaba.com/schema/services http://localhost:8080/schema/services.xsd 
        http://www.alibaba.com/schema/services/form/conditions http://localhost:8080/schema/services-form-conditions.xsd 
        http://www.alibaba.com/schema/services/form/validators http://localhost:8080/schema/services-form-validators.xsd 
        http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd 
    "> 
    <services:form postOnlyByDefault="true"> 
        <!-- 
          - =============================================== 
          - 用来检查csrf token。 
          - =============================================== 
         --> 
        <services:group name="csrfTokenCheckGroup"> 
            <services:field name="csrfToken"> 
                <csrf-validator> 
                    <message>提交的数据已过期</message> 
                </csrf-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 登录表单 
          - =============================================== 
         --> 
        <services:group name="login" extends="csrfTokenCheckGroup"> 
            <services:field name="loginError"> 
                <custom-error id="invalidUserOrPassword"> 
                    <message>用户名或密码不正确,请重新输入</message> 
                </custom-error> 
            </services:field> 
            <services:field name="userId" displayName="登录名"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="password" displayName="密码"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 注册表单 
          - =============================================== 
         --> 
        <services:group name="register" extends="csrfTokenCheckGroup"> 
            <services:field name="registerError"> 
                <custom-error id="duplicatedUserId"> 
                    <message>登录名“${userId}”已经被人注掉了,请尝试另一个名字</message> 
                </custom-error> 
            </services:field> 
            <services:field name="userId" displayName="登录名"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <regexp-validator pattern="^[A-Za-z_][A-Za-z_0-9]*$"> 
                    <message>${displayName} 必须由字母、数字、下划线构成</message> 
                </regexp-validator> 
                <string-length-validator minLength="4" maxLength="10"> 
                    <message>${displayName} 最少必须由${minLength}个字组成,最多不能超过${maxLength}个字</message> 
                </string-length-validator> 
            </services:field> 
            <services:field name="password" displayName="密码"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <string-length-validator minLength="4" maxLength="10"> 
                    <message>${displayName} 最少必须由${minLength}个字组成,最多不能超过${maxLength}个字</message> 
                </string-length-validator> 
                <string-compare-validator notEqualTo="userId"> 
                    <message>${displayName} 不能与 ${userId.displayName} 相同</message> 
                </string-compare-validator> 
            </services:field> 
            <services:field name="passwordConfirm" displayName="密码验证"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <string-compare-validator equalTo="password"> 
                    <message>${displayName} 必须和 ${password.displayName} 相同</message> 
                </string-compare-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 注册account信息表单 
          - =============================================== 
         --> 
        <services:group name="registerContact" extends="csrfTokenCheckGroup"> 
            <services:field name="firstName" displayName="名字" propertyName="account.firstName"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="lastName" displayName="姓氏" propertyName="account.lastName"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="address1" displayName="地址" propertyName="account.address1"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="address2" displayName="地址" propertyName="account.address2"> </services:field> 
            <services:field name="city" displayName="城市" propertyName="account.city"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="zip" displayName="邮编" propertyName="account.zip"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <number-validator> 
                    <message>${displayName} 必须是数字</message> 
                </number-validator> 
            </services:field> 
            <services:field name="state" displayName="省份" propertyName="account.state"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="country" displayName="国家" propertyName="account.country"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="phone" displayName="电话" propertyName="account.phone"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="email" displayName="电子邮件" propertyName="account.email"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <mail-address-validator> 
                    <message>${displayName} 必须是合法的e-mail</message> 
                </mail-address-validator> 
            </services:field> 
        </services:group> 
        <!-- 
          - =============================================== 
          - 注册credit信息表单 
          - =============================================== 
         --> 
        <services:group name="registerCredit" extends="csrfTokenCheckGroup"> 
            <services:field name="creditCardNumber" displayName="信用卡号码" 
                propertyName="account.creditCardNumber"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
                <number-validator> 
                    <message>${displayName} 必须是数字</message> 
                </number-validator> 
            </services:field> 
            <services:field name="creditCardType" displayName="信用卡类型" 
                propertyName="account.creditCardType"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="creditCardExpiryMonth" displayName="信用卡过期月份" 
                propertyName="account.creditCardExpiryMonth"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
            <services:field name="creditCardExpiryYear" displayName="信用卡过期年份" 
                propertyName="account.creditCardExpiryYear"> 
                <required-validator> 
                    <message>必须填写 ${displayName}</message> 
                </required-validator> 
            </services:field> 
        </services:group> 
    </services:form> 
</beans:beans> 

转载于:https://www.cnblogs.com/wzyxidian/p/5593612.html

你可能感兴趣的文章
ArrayList的使用方法
查看>>
面向对象高级
查看>>
Bitwise And Queries
查看>>
打印Ibatis最终的SQL语句
查看>>
HBase之八--(3):Hbase 布隆过滤器BloomFilter介绍
查看>>
oracle连接问题ORA-00604,ORA-12705
查看>>
NOI 2019 退役记
查看>>
java的几个日志框架log4j、logback、common-logging
查看>>
Java从零开始学十三(封装)
查看>>
Python2和Python3中的rang()不同之点
查看>>
MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)...
查看>>
UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
查看>>
记忆--1.致我们不可缺少的记忆
查看>>
lintcode28- Search a 2D Matrix- easy
查看>>
react项目
查看>>
C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰(转)
查看>>
A Simple Tree Problem
查看>>
Modular Inverse [ZOJ 3609]
查看>>
MySQL性能测试工具之mysqlslap使用详解
查看>>
深入理解jsonp跨域请求原理
查看>>