本文主要是介绍Struts 2 校验文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
校验是一件很繁琐的事,虽然也不是很难,但是就是重复同样的工作有时候让人觉得danteng,用Struts 2的校验框架的时候我是不会刻意去记那些校验器的,因为我觉得这些东西没必要记,用多了自然就会写出来,下面我记录的一个校验文件,我此刻记录的时候是可以起作用的,以后要用的时候就来这里拿了:
<?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><field name="email"><field-validator type="requiredstring"><message>Email不能为空</message></field-validator><field-validator type="email"><message>Email格式不正确</message></field-validator></field><field name="username"><field-validator type="requiredstring"><message>用户名必须填</message></field-validator><field-validator type="regex"><param name="expression"><![CDATA[(^[0-9a-zA-Z_\u4E00-\u9FA5\uF900-\uFA2D]{1,16}$)]]></param><message>用户名格式不符合要求</message></field-validator><field-validator type="stringlength"><param name="maxLength">16</param><param name="trim">true</param><message>用户名长度不能大于16</message></field-validator></field><field name="password"><field-validator type="stringlength"><param name="maxLength">20</param><param name="minLength">6</param><param name="trim">true</param><message>密码长度应为6至20个字符</message></field-validator></field><field name="password2"><field-validator type="stringlength"><param name="maxLength">20</param><param name="minLength">6</param><param name="trim">true</param><message>密码长度应为6至20个字符</message></field-validator><field-validator type="fieldexpression"><param name="expression"><![CDATA[(password == password2)]]></param><message>确认密码必须与密码相同</message></field-validator></field><field name="mobile"><field-validator type="regex"><param name="expression"><![CDATA[(^1[3458]\d{9}$)]]></param><message>手机号码不符合要求</message></field-validator></field><field name="qq"><field-validator type="stringlength"><param name="maxLength">16</param><param name="minLength">0</param><param name="trim">true</param><message>qq长度必须为0至16个字符</message></field-validator></field><field name="city_id"><field-validator type="required"><message>城市编号必须填</message></field-validator><field-validator type="int"><message>城市编号是整数</message></field-validator></field><field name="realname"><field-validator type="stringlength"><param name="maxLength">32</param><param name="minLength">0</param><param name="trim">true</param><message>真实姓名长度必须为0至32个字符</message></field-validator></field><field name="address"><field-validator type="stringlength"><param name="maxLength">100</param><param name="minLength">0</param><param name="trim">true</param><message>地址长度应为0至100个字符</message></field-validator></field><field name="zipcode"><field-validator type="stringlength"><param name="maxLength">6</param><param name="minLength">0</param><param name="trim">true</param><message>邮政编码长度应为0至6个字符</message></field-validator></field> </validators>
这篇关于Struts 2 校验文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!