声明式验证步骤
一 、编写Action类
- package com.tg.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class CheckAction extends ActionSupport {
- /**
- *
- */
- private static final long serialVersionUID = 7982435824916880105L;
- private int age ;
- private String username;
- private String email;
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public static long getSerialversionuid() {
- return serialVersionUID;
- }
- @Override
- public String execute() throws Exception {
- System.out.println(”age”+age);
- return SUCCESS;
- }
- }
package com.tg.action;
import com.opensymphony.xwork2.ActionSupport;
public class CheckAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 7982435824916880105L;
private int age ;
private String username;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String execute() throws Exception {
System.out.println("age"+age);
return SUCCESS;
}
}
二、在struts.xml中配置该Action
- <?xml version=“1.0” encoding=“UTF-8” ?>
- <!DOCTYPE struts PUBLIC
- ”-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
- ”http://struts.apache.org/dtds/struts-2.3.dtd”>
- <struts>
- <constant name=”struts.devMode” value=“true” />
- <constant name=”struts.action.extension” value=“action,do,,”/>
- <constant name=”struts.enable.DynamicMethodInvocation” value=“false” />
- <constant name=”struts.devMode” value=“true” />
- <package name=“default” namespace=“/” extends=“struts-default”>
- <span style=”color:#FF0000;”><action name=“check” class=“com.tg.action.CheckAction”>
- <result name=”success”>/success.jsp</result>
- <result name=”input”>/validation.jsp</result>
- </action></span>
- </package>
- </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="action,do,,"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<span style="color:#FF0000;"><action name="check" class="com.tg.action.CheckAction">
<result name="success">/success.jsp</result>
<result name="input">/validation.jsp</result>
</action></span>
</package>
</struts>
三、编写验证规则的配置文件
去struts2-blank\WEB-INF\src\java\example 下找到 Login-validation.xml 复制 然后重命名
命名必须为 必须放置在Action类的同目录下
<ActionClassName>-validation.xml
<ActionClassName>-<ActionAliasName>-validation.xml
其中<ActionAliasName>就是struts.xml中<ation>的name属性值。
本例子 为 CheckAction-validation.xml
- <!DOCTYPE validators PUBLIC
- ”-//Apache Struts//XWork Validator 1.0.2//EN”
- ”http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd”>
- <span style=”color:#FF0000;”><span><span class=“comments”><!– 校验文件的根元素 –></span><span></span></span></span>
- <validators>
- <span style=”color:#FF0000;”><span> <span class=“comments”><!– 校验Action的username属性 –></span><span> </span></span></span>
- <field name=”username”>
- <field-validator type=”requiredstring”>
- <param name=”trim”>true</param>
- <message>username is required</message>
- </field-validator>
- </field>
- <field name=”email”>
- <field-validator type=”email”>
- <message>Must provide a valid email</message>
- </field-validator>
- </field>
- <span><span style=”color:#FF0000;”><span class=“comments”><!– 指定name属性必须满足必填规则 –></span></span><span><span style=“color:#FF0000;”> </span>
- </span></span>
- <field name=”age”>
- <field-validator type=”conversion” short-circuit=“true”>
- <message>Conversion Error (Integer Wanted)</message>
- </field-validator>
- <span style=”color:#FF0000;”><span> <span class=“comments”><!– 指定age属性必须在指定范围内–</span></span>></span>
- <field-validator type=”int”>
- <param name=”min”>20</param>
- <param name=”max”>50</param>
- <message>Age needs to be between {min} and {max}</message>
- </field-validator>
- </field>
- </validators>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<span style="color:#FF0000;"><span><span class="comments"><!-- 校验文件的根元素 --></span><span></span></span></span>
<validators>
<span style="color:#FF0000;"><span> <span class="comments"><!-- 校验Action的username属性 --></span><span> </span></span></span>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username is required</message>
</field-validator>
</field>
<field name="email">
<field-validator type="email">
<message>Must provide a valid email</message>
</field-validator>
</field>
<span><span style="color:#FF0000;"><span class="comments"><!-- 指定name属性必须满足必填规则 --></span></span><span><span style="color:#FF0000;"> </span>
</span></span>
<field name="age">
<field-validator type="conversion" short-circuit="true">
<message>Conversion Error (Integer Wanted)</message>
</field-validator>
<span style="color:#FF0000;"><span> <span class="comments"><!-- 指定age属性必须在指定范围内--</span></span>></span>
<field-validator type="int">
<param name="min">20</param>
<param name="max">50</param>
<message>Age needs to be between ${min} and ${max}</message>
</field-validator>
</field>
</validators>
一个Action类可以应答多个action请求 ,多个action 请求使用不同的验证规则 怎么办?
解决方案
为每个不同的action请求 定义其对应的验证文件:ActionClassName-AliasName-
validation.xml不带别名的配置文件:ActionClassName-validation.xml
中的验证规则依然会起作用 可以吧各个action公有的验证规则配置在其中 但需要注意的是
只适应于某一个action的请求的验证规则就不要再这里
配置了
字段验证
字段校验器配置格式:
<field name=”被校验的字段”>
<field-validator type=”校验器名”>
<!–此处需要为不同校验器指定数量不等的校验规则–>
<param name=”参数名”>参数值</param>
………………..
<!–校验失败后的提示信息,其中key指定国际化信息的key–>
<message key=”I18Nkey”>校验失败后的提示信息</message>
<!–校验失败后的提示信息:建议用getText(“I18Nkey”),否则可能出现Freemarker template Error–>
</field-vallidator>
<!– 如果校验字段满足多个规则,下面可以配置多个校验器–>
</field>
非字段验证
非字段校验器配置格式:
<validator type=”校验器名”>
<param name=”fieldName”>需要被校验的字段</param>
<!–此处需要为不同校验器指定数量不等的校验规则–>
<param name=”参数名”>参数值</param>
<!–校验失败后的提示信息,其中key指定国际化信息的key–>
<message key=”I18Nkey”>校验失败后的提示信息</message>
<!–校验失败后的提示信息:建议用getText(“I18Nkey”),否则可能出现Freemarker template Error–>
</validator>
(1)字段校验代码,例如下面的代码:
Xml代码
<field name=”….”>
<field-validator type=”…”></field-validator>
</filed>
(2)非字段校验代码,例如下面的代码:
Xml代码
<validators>
<validator type=”required”>
<param name=”fieldName”>username</param>
<message>用户名必须填写</message>
</validator>
</validators>
struts2的校验大部分是围绕Action组件的字段属性来实现的,主要是判断某个字段属性的输入是否有效,因此又把这种内置的校验器成为字段校验器。还有一种校验器它不是针对某个字段,而是针对多个字段的输入值之间的逻辑关系进行校验,典型的例子是再次输入密码的判断。内置的expression校验器就是一种非字段校验器。
<validator type=”expression”>
<param name=”expression”><![CDATA[(password==repassword)]]></param>
<message>两次输入的密码必须完全一致!</message>
</validator>
非字段校验:先指定校验器:由谁来校验,来校验谁!
字段校验器:先指定校验的属性:我来校验谁,由谁来校验!
短路验证
加 short-circuit=”true” 表示当前验证器没有通过后 直接返回input视图 不会验证后面的验证器
- <validators>
- <field name=”age”>
- <field-validator type=”conversion” >
- <message>Conversion Error (Integer Wanted)</message>
- </field-validator>
- <field-validator type=”int” <span style=“color:#FF0000;”>short-circuit=“true”</span> >
- <param name=”min”>20</param>
- <param name=”max”>50</param>
- <message>Age needs to be between {min} and {max}</message>
- </field-validator>
- </field>
- <field name=”username”>
- <field-validator type=”requiredstring” >
- <param name=”trim”>true</param>
- <message>username is required</message>
- </field-validator>
- </field>
- <field name=”email”>
- <field-validator type=”email”>
- <message>Must provide a valid email</message>
- </field-validator>
- </field>
- </validators>
<validators>
<field name="age">
<field-validator type="conversion" >
<message>Conversion Error (Integer Wanted)</message>
</field-validator>
<field-validator type="int" <span style="color:#FF0000;">short-circuit="true"</span> >
<param name="min">20</param>
<param name="max">50</param>
<message>Age needs to be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="username">
<field-validator type="requiredstring" >
<param name="trim">true</param>
<message>username is required</message>
</field-validator>
</field>
<field name="email">
<field-validator type="email">
<message>Must provide a valid email</message>
</field-validator>
</field>
</validators>