标签:
输入校验分为客户端校验和服务器校验。Struts2可以同时完成这两种校验。
All input is evil.——《Writing Secure Code》
手动完成输入校验:
流程:
基本输入校验(推荐):
<?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> <!-- 必填校验 --> <field name="字段名"> <field-validator type="required" [short-circuit="true"]> <message>错误提示信息</message> </field-validator> </field> <!-- 必填字符串校验 --> <field name="字段名"> <field-validator type="requiredstring" [short-circuit="true"]> <param name="trim">默认为true</param> <!-- 是否在检验前截断被检验属性值前后的空白 --> <message key="没有填写密码!" /> </field-validator> </field> <!-- 整数校验 --> <field name="字段名"> <field-validator type="int" [short-circuit="true"]> <param name="min">最小值</param> <param name="max">最大值</param> <message>错误提示信息</message> </field-validator> </field> <!-- 日期校验 --> <field name="字段名"> <field-validator type="date" [short-circuit="true"]> <param name="min">1990-01-01</param> <param name="max">2010-01-01</param> <message>错误提示信息</message> </field-validator> </field> <!-- 表达式校验 --> <validator type="expression" [short-circuit="true"]> <param name="expression">逻辑表达式</param> <!-- 返回false则校验失败,输出信息 --> <message>错误提示信息</message> </validator> <!-- 字段表达式校验 --> <field name="字段名"> <field-validator type="fieldexpression" [short-circuit="true"]> <param name="expression">逻辑表达式</param> <!-- 返回false则校验失败,输出信息 --> <message>错误提示信息</message> </field-validator> </field> <!-- 邮件地址校验 --> <field name="字段名"> <field-validator type="email" [short-circuit="true"]> <message>错误提示信息</message> </field-validator> </field> <!-- 网址校验 --> <field name="字段名"> <field-validator type="url" [short-circuit="true"]> <message>错误提示信息</message> </field-validator> </field> <!-- Visitor校验:校验Action类里的非基本属性 --> <field name="字段名"> <field-validator type="visitor" [short-circuit="true"]> <param name="context">字段名Context</param> <!-- 调用Action-字段名Context-validation.xml --> <param name="appendPrefix">true</param> <!-- 指定检验失败后提示信息是否添加下面的前缀 --> <message>提示信息前缀</message> </field-validator> </field> <!-- 转换校验 --> <field name="字段名"> <field-validator type="conversion" [short-circuit="true"]> <param name="repopulateField">true</param> <!-- 转换失败后,返回输入页仍保留原来的错误输入 --> <message>错误提示信息</message> </field-validator> </field> <!-- 字符串长度校验 --> <field name="字段名"> <field-validator type="stringlength" [short-circuit="true"]> <param name="minLength">最小值</param> <param name="maxLength">最大值</param> <param name="trim">默认为true</param> <message>错误提示信息</message> </field-validator> </field> <!-- 正则表达式校验 --> <field name="字段名"> <field-validator type="regex" [short-circuit="true"]> <param name="expression">正则表达式</param> <param name="caseSensitive">默认为true</param> <!-- 是否区分大小写 --> <message>错误提示信息</message> </field-validator> </field> </validators>
标签:
原文地址:http://www.cnblogs.com/hippiebaby/p/4507436.html