index.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>请输入您的注册信息</title>
</head>
<body>
<H1>请输入您的注册信息</H1>
<s:form action="regist">
<s:textfield label="用户名" name="name"/>
<s:password label="密码" name="pass"/>
<s:password label="确认密码" name="rpass"/>
<s:textfield label="年龄" name="age"/>
<s:textfield label="电话" name="phone"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="lee" extends="struts-default"> <action name="regist" class="tom.jiafei.ZhuceAction"> <result name="success">/welcome.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
zhuceAction.java
package tom.jiafei;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
import java.util.regex.*;
import java.text.*;
public class ZhuceAction extends ActionSupport
{
private String name;
private String pass;
private String rpass;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getRpass() {
return rpass;
}
public void setRpass(String rpass) {
this.rpass = rpass;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String execute() throws Exception {
return SUCCESS;
}
}
<?xml version="1.0" encoding="GBK"?> <!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="name"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空</message> </field-validator> </field> <field name="pass"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>密码不能为空</message> </field-validator> </field> <field name="rpass"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>确认密码不能为空</message> </field-validator> <field-validator type="fieldexpression"> <param name="expression"><![CDATA[(pass==rpass)]]></param> <message>两次输入密码不一致</message> </field-validator> </field> <field name="phone"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>电话不能为空</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[(1\d{10})]]></param> <message>电话必为11位数字,且必以1开头</message> </field-validator> </field> </validators>