struts2的验证功能
struts2的验证可以通过继承ActionSupport类重写validate()方法实现,也可以通过校验框架实现。
优先级validation.xml > validate()
重写validate()
public void validate() { if (userName == null || userName.trim().equals("")) { addFieldError("username", "user.required"); } if (password == null || password.trim().equals("")) { addFieldError("password", "pass.required"); } }
validation.xml 的命名规则和放置路径:
文件名:<ActionClassName>-validation.xml
<ActionClassName>就是要验证的Action类的名字。
要将此文件放于Class文件相同的目录。
如果在Action类在struts配置中有多个action实例(action name),那么对应某个action的验证文件名规则如下:
文件名:<ActionClassName>-<aliasName>-validation.xml
例如:UserAction-login-validation.xml
(注意:上面的<aliasName>并不是method name,而是struts.xml中配置的action的name)
如:struts.xml
<action name="Login" class="com.sun.LoginAction" method="login"> <result name="success">/success.jsp</result> <result name="input">/login.jsp</result> </action>
注意:在跳转到action之前,先进行验证,若filedError里有值,请求将被转发到input逻辑视图处。
validation.xml 的内容示例:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <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="password"> <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> </validators>
Struts2 的验证规则大概有以下数种:
具体的使用方法可以GOOGLE,下面举例几种常用的验证规则:
常用的验证规则:
1.必填检验
<validators> <field name="username"> <field-validator type="required"> <message>指定检验失败的提示信息</message> </field-validator> </field> </validators>
<validators> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>指定检验失败的提示信息</message> </field-validator> </field> </validators>
<validators> <field name="age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>年纪必须在1到150之间</message> </field-validator> </field> </validators>
<validators> <field name="birth"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2050-02-21</param> <message key="birth.range"/> </field-validator> </field> </validators>
<validators> <field name="re_pass"> <field-validator type="fieldexpression"> <!--指定逻辑表达式 --> <param name="expression"> (pass eq re_pass)</param> <message>密码必须和确认密码相等</message> </field-validator> </field> </validators>
<validators> <field name="email"> <field-validator type="email"> <message>你的电子邮件地址必须是一个有效的电邮地址</message> </field-validator> </field> </validators>
<validators> <field name="url"> <field-validator type="url"> <message>你的主页地址必须是一个有效的网址</message> </field-validator> </field> </validators>
<validators> <field name="user"> <field-validator type="stringlength"> <param name="minlength">4</param> <param name="maxlength">20</param> <message>你的用户名长度必须在4到20之间</message> </field-validator> </field> </validators>
<validators> <field name="user"> <field-validator type="regex"> <param name="expression_r"><![CDATA[(\w{4,25})]]></param> <message>您输入的用户名只能是字母和数组,且长度必须在4到25之间</message> </field-validator> </field> </validators>