一 视图
1 form.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>请输入您的用户信息</title>
<s:head/>
</head>
<body>
<h3>请输入您的用户信息</h3>
<s:form action="loginPro">
<s:textfield label="用户名" name="name"/>
<s:password label="密码" name="pass"/>
<s:textfield label="年龄" name="age"/>
<s:textfield label="生日" name="birth"/>
<tr><td colspan="2">
<s:submit value="注册" theme="simple"
οnclick="this.form.action='registPro';"/>
<s:submit value="登录" theme="simple"
οnclick="this.form.action='loginPro';"/>
</td></tr>
</s:form>
</body>
</html>
2 show.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>校验成功</title>
</head>
<body>
<H3>校验成功</H3>
用户名:<s:property value="name"/><br/>
密码:<s:property value="pass"/><br/>
年龄:<s:property value="age"/><br/>
生日:<s:property value="birth"/><br/>
</body>
</html>
二 配置文件
<?xml version="1.0" encoding="GBK"?>
<!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.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<!--配置一个名为*Pro的Action,
对应的处理逻辑为RegistAction的{1}方法-->
<action name="*Pro" class="org.crazyit.app.action.RegistAction"
method="{1}">
<result name="input">/WEB-INF/content/form.jsp</result>
<result>/WEB-INF/content/show.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
三 action
1 BaseAction
package org.crazyit.app.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class BaseAction extends ActionSupport
{
private String name;
// name的setter和getter方法
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return (this.name);
}
}
2 RegistAction
package org.crazyit.app.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class RegistAction extends BaseAction
{
private String pass;
private int age;
private Date birth;
// pass的setter和getter方法
public void setPass(String pass)
{
this.pass = pass;
}
public String getPass()
{
return this.pass;
}
// age的setter和getter方法
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
// birth的setter和getter方法
public void setBirth(Date birth)
{
this.birth = birth;
}
public Date getBirth()
{
return this.birth;
}
public String regist()
{
return SUCCESS;
}
public String login()
{
return SUCCESS;
}
}
四 资源文件
xwork.default.invalid.fieldvalue={0}字段无效
name.requried=您必须输入用户名!
name.regex=您输入的用户名只能是字母和数组,且长度必须在4到25之间!
pass.requried=您必须输入密码!
pass.regex=您输入的密码只能是字母和数组,且长度必须在4到25之间!
age.range=您的年龄必须在${min}和${max}之间!
birth.range=您的生日必须在${min}和${max}之间!
nameexp=用户名和密码必须相同
五 校验文件
BaseAction-validation.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="name.requried"/>
</field-validator>
<field-validator type="regex">
<param name="regex"><![CDATA[(\w{4,25})]]></param>
<message key="name.regex"/>
</field-validator>
</field>
</validators>
RegistAction-validation.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="pass">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="pass.requried"/>
</field-validator>
<field-validator type="regex">
<param name="regex"><![CDATA[(\w{4,25})]]></param>
<message key="pass.regex"/>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message key="age.range"/>
</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 key="birth.range"/>
</field-validator>
</field>
</validators>
RegistAction-loginPro-validation.xml
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<!-- 校验规则文件的根元素 -->
<validators>
<!-- 校验name属性 -->
<field name="name">
<!-- 使用表达式校验器校验name属性 -->
<field-validator type="fieldexpression">
<!-- 指定name属性和pass属性必须相等 -->
<param name="expression"><![CDATA[(name == pass)]]></param>
<message key="nameexp"/>
</field-validator>
</field>
</validators>
六 测试