引入服务器端表单验证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 >
<required-validator>
<message>必须填写 ${displayName}</message>
</required-validator>
</services:field>
</services:group>
</services:form>
</beans:beans>
相关文章: