struts2 validation.xml 验证

当使用struts2的validation.xml进行验证时,需要注意如下几个方面:
1.表单一般使用struts2标签<s:form validate="false"/>关闭客户端验证,使用<s:textfield type="text" name="name"/>name对应action中的属性
2.在Action配置中一定要设置input返回页面<result name="input"/>

3.添加验证只要创建验证的xml文件,在Action同包下,创建:Action类名-validation.xml

4.对于int型和Date型如果客户端传来的格式不对,后台会提示警告报错,需要用javascript正则表达式验证。

<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.2//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
	<field name="name">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>请填写用户名</message>
		</field-validator>
		<field-validator type="stringlength">
			<param name="minLength">4</param>
			<param name="maxLength">32</param>
			<message>用户名长度应在4到32个字符间</message>
		</field-validator>
	</field>
	<field name="pass">
		<field-validator type="requiredstring">
			<message>请填写密码</message>
		</field-validator>
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">32</param>
			<message>为了您账号的安全,请设置6个字母以上的密码(最长可设置32个字母)</message>
		</field-validator>
	</field>
	<field name="age">
		<field-validator type="int">
			<param name="min">1</param>
			<param name="max">70</param>
			<message>请输入1-70之间</message>
		</field-validator>
	</field>
	<field name="birth">
		<field-validator type="date">
			<param name="min">1900-01-01</param>
			<param name="max">2050-02-21</param>
			<message>生日必须在{min}到{max}之间</message>
		</field-validator>
	</field>
</validators>



你可能感兴趣的:(struts2,正则表达式,输入校验,xml验证)