struts2表单验证里field-validator type值一共可以取哪些?都什么含义?
int 整数;
double 实数;
date 日期;
expression 两数的关系比较;
email Email地址;
url
visitor
conversion
regex 正则表达式验证;
required 是否为空;
requiredstring 必须字符;
stringlength 输入字符长度限制;
优点:无须写代码,简化工作量,修改验证无须修改代码,利于后期维护。
在那个需要验证的action中写xml文件 文件名有讲究啊 那个action的类名-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"> <validators> <!-- 设置字段的验证规则,name表示字段名,要求与表单元素的name一致 --> <field name="userName"> <!-- 设置在字段上实施的验证规则,type表示验证规则类型名称,requiredstring验证字段不能使空引用或空字符串 --> <field-validator type="requiredstring"> <!-- trime参数表示在判断前先调用trim()方法去掉字符串头尾空格 --> <param name="trim">true</param> <!-- 设置错误提示信息 --> <message>请输入用户名</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">12</param> <message>用户名长度必须在4-12之间</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="required.password"/> </field-validator> </field> <field name="rePwd"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>请确认密码</message> </field-validator> <field-validator type="fieldexpression"> <param name="expression">password==rePwd</param> <message>两次输入的密码必须一致</message> </field-validator> </field> <field name="job.id"> <field-validator type="required"> <message key="required.job"/> </field-validator> </field> <field name="birthday"> <field-validator type="required"> <message>请输入生日</message> </field-validator> </field> <field name="email"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>请输入email</message> </field-validator> <field-validator type="email"> <message>Email格式不正确</message> </field-validator> </field> <field name="url"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>请输入URL</message> </field-validator> <field-validator type="url"> <message>URL格式不正确</message> </field-validator> </field>
<!-- 在这一个字段里边配置多个验证规则会都执行,解决办法:可以设置个特殊的开关 叫短路属性
short-circuit="true"意思是一旦执行了第一个,则就不执行第二个了
-->
<field name="vcode">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
<message key="required.vcode"/>
</field-validator>
<field-validator type="fieldexpression">
<!-- 判断用户输入的vcode和#session.KAPTCHA_SESSION_KEY相等即可 -->
<param name="expression">vcode==#session.KAPTCHA_SESSION_KEY</param>
<message key="invalid.vcode"/>
</field-validator>
</field> </validators>