1.struts校验原理:
配置要校验的action响应路径,比如www.baidu.com/test.action
那么后台定要配置一个与之相应的test名称的action类,和test-validate.xml来进行校验
2.test-validate.xml中校验例子:
<!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="baseUnit.unitName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>单位名不能为空</message>
</field-validator>
</field>
<field name="baseUnit.unitClass">
<field-validator type="int">
<param name="min">1</param>
<param name="max">2</param>
<message>请选择单位分类</message>
</field-validator>
</field>
<field name="baseUnit.unitType">
<field-validator type="int">
<param name="min">1</param>
<param name="max">8</param>
<message>请选择单位类别</message>
</field-validator>
</field>
<field name="baseUnit.regionCode">
<field-validator type="stringlength">
<param name="minLength">0</param>
<param name="maxLength">6</param>
<message>注册码不能超过6位数 </message>
</field-validator>
<field-validator type="regex">
<param name="regexExpression"><![CDATA[[\w\s]+]]></param>
<message>注册码不可为汉字</message>
</field-validator>
</field>
<field name="baseUnit.createTimeStr">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>创建日期不能为空</message>
</field-validator>
<field-validator type="regex">
<param name="regexExpression"><![CDATA[([1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])( )( )?(([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])))]]></param>
<message>创建日期必须为2017-01-01 10:10:10格式</message>
</field-validator>
</field>
</validators>
3.校验结果在前台freemarker.ftl中显示:
<#if fieldErrors['baseUnit.unitName']??>
<font color="red" style="font-style: oblique;font-family: sans-serif;font-size: xx-small;">
<img src="${request.contextPath}/static/images/bit.PNG" style="width: 10PX"/>${fieldErrors['baseUnit.unitName']?default("")?html?replace("\r\n","<br>")}
</font>
<#else>
</#if>
4.另外,freemarker页面如何引入struts标签:
1.将struts-tags.ftl放入WEB-INF下
2.再在页面引用
5.struts后台,验证通过,将数据携带到执行下一个action
<result type="chain"></result>