引入服务器端表单验证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>

Webx之表单验证的更多相关文章

  1. jQuery学习之路(8)- 表单验证插件-Validation

    ▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...

  2. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  3. form表单验证-Javascript

    Form表单验证: js基础考试内容,form表单验证,正则表达式,blur事件,自动获取数组,以及css布局样式,动态清除等.完整代码如下: <!DOCTYPE html PUBLIC &qu ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(33)-MVC 表单验证

    系列目录 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可或缺的,微软的东西还是做得比较人性化的,从webform到MVC,都做到了双向验证 单单的用js实现的前端 ...

  5. 实现跨浏览器html5表单验证

    div:nth-of-type(odd){ float: left; clear: left; } .origin-effect > div:nth-of-type(even){ float: ...

  6. jQuery Validate 表单验证 — 用户注册简单应用

    相信很多coder在表单验证这块都是自己写验证规则的,今天我们用jQuery Validate这款前端验证利器来写一个简单的应用. 可以先把我写的这个小demo运行试下,先睹为快.猛戳链接--> ...

  7. jquery validate表单验证插件-推荐

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  8. 表单验证插件之jquery.validate.js

    提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...

  9. 走进AngularJs 表单及表单验证

    年底了越来越懒散,AngularJs的学习落了一段时间,博客最近也没更新.惭愧~前段时间有试了一下用yeoman构建Angular项目,感觉学的差不多了想做个项目练练手,谁知遇到了一系列问题.yeom ...

随机推荐

  1. TS格式解析

    1.TS格式介绍 TS:全称为MPEG2-TS.TS即"Transport Stream"的缩写.它是分包发送的,每一个包长为188字节(还有192和204个字节的包).包的结构为 ...

  2. asp.net 用jquery判断fileupload上传文件的大小和类型和名字

    <script language="javascript" type="text/javascript"> //检查上传文件大小和获取文件名 fun ...

  3. iOS--检测野指针

    定位野指针除了使用Malloc Scribble(内存涂鸦)外,还可以使用僵尸对象.所谓的僵尸对象,就是将被释放的对象标记为僵尸,系统不会回收这些对象的内存,并让这些内存无法被重用,因而也就不会被覆写 ...

  4. (转)所有iOS设备的屏幕分辨率

    Phone: iPhone 1G 320x480 iPhone 3G 320x480 iPhone 3GS 320x480 iPhone 640x960 iPhone 4S 640x960 iPhon ...

  5. IQueryable接口与IEnumberable区别

    IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,take 这些 ...

  6. ajax的访问 WebService 的方法

    如果想用ajax进行访问 首先在web.config里进行设置 添加在 <webServices> <protocols> <add name= "HttpPo ...

  7. HTML5 声明兼容IE的写法

    <!DOCTYPE html> <!–[if IE]> <meta http-equiv=”X-UA-Compatible” content=”IE=8″ /> & ...

  8. php图片上传代码

    使用copy函数 if (!empty($_FILES)) { //图片 if(isset($_FILES['image'])) { $img_data = $_FILES['image']['tmp ...

  9. mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句

    http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases;           ...

  10. a href="#"与a href="####"的区别是什么

    前提是 :有滚动条. [感谢  黎明就在眼前 博客园园友] '#' 是有特殊意义,如果 '#' 后有内容会被认为是一个标签而从页面找到相应标签跳转到该处,找不到时会跳到页首,通常情况下使用“#ID”来 ...