Action类
package action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport{
private String username;
private String password;
private String password1;
private Date birthday;
private String mobile;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String execute() {
return SUCCESS;
}
public void validate() {
if (getUsername() == null || getUsername().trim().equals("")) {
addFieldError("username", "请输入用户名");
}
if (getPassword() == null || getPassword().trim().equals("")) {
addFieldError("password", "请输入密码");
}
if (getPassword1() == null || getPassword1().trim().equals("")) {
addFieldError("password1", "请输入确认密码");
}
if (!getPassword().equals(getPassword1())) {
addFieldError("password", "密码和输入密码不一致");
}
if (getBirthday() == null) {
addFieldError("birthday", "请输入生日日期");
} else if (getBirthday().after(new Date())) {
addFieldError("birthday", "请输入正确日期");
}
if (getMobile().length() != 11) {
addFieldError("mobile", "请输入正确的手机号码");
}
if (getAge() < 1 || getAge() > 120) {
addFieldError("age", "请输入正确年龄");
}
}
}
Strust配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="register" class="action.RegisterAction" >
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
web配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts-validate</display-name>
<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>
Page主要界面
<%@ taglib uri="/struts-tags" prefix="s"%>
<body>
<s:form action="register.action">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:password name="password1" label="密码确认"></s:password>
<s:textfield name="birthday" label="生日"></s:textfield>
<s:textfield name="mobile" label="手机号码"></s:textfield>
<s:textfield name="age" label="年龄"></s:textfield>
<s:submit value="注册"></s:submit>
</s:form>
</body>