一 <register.jsp>:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>welcom register page !</title>

</head>

<s:fielderror></s:fielderror>

<body bgcolor="red">

<form action="register.action" method="post">

<table align="center" width="40%" border="1" bgcolor="cyan">

<tr>

<td>username:</td>

<td><input type="text" name="username"/></td>

</tr>

<tr>

<td>password</td>

<td><input type="password" name="password"/></td>

</tr>

<tr>

<td>re-password</td>

<td><input type="password" name="repassword"/></td>

</tr>

<tr>

<td>age</td>

<td><input type="text" name="age"/></td>

</tr>

<tr>

<td>birthday</td>

<td><input type="text" name="birthday"/></td>

</tr>

<tr>

<td>graduation</td>

<td><input type="text" name="graduation"/></td>

</tr>

<tr>

<td><input type="submit" value=" Submit "/></td>

<td><input type="reset" value=" Reset "/></td>

</tr>

</table>

</form>

</body>

</html>

--------------------------------------

二 编写Action类<RegisterAction>:

package com.struts2;

import java.util.Calendar;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {


private String username;


private String password;


private String repassword;


private int age;


private Date birthday;


private Date graduation;

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 getRepassword() {

return repassword;

}

public void setRepassword(String repassword) {

this.repassword = repassword;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public Date getGraduation() {

return graduation;

}

public void setGraduation(Date graduation) {

this.graduation = graduation;

}

public String execute() throws Exception {

return SUCCESS;

}

//验证form表单输入信息

public void validate() {

//名字为空 或者长度小于6大于10

if(null == username || username.length() < 6 || username.length() > 10){

this.addFieldError("username", "username invalid");

}

//密码为空或者长度小于6或者大于10

if(null == password || password.length() < 6 || password.length() > 10){

this.addFieldError("password", "password invalid");

//确认密码为空或者长度小于6或者大于10

}else if(null == repassword || repassword.length() < 6 || repassword.length() > 10){

this.addFieldError("repassword", "re-password invalid");

//密码和确认密码值不是一样的

}else if(!password.equals(repassword)) {

this.addFieldError("repassword", "re-password and password not accord");

}

//年龄长度小于1或者大于150

if(age <1 || age >150){

this.addFieldError("age", "age invalid");

}

//birthday 和 graduation 为空 或者 birthday 在 graduation 之前

if(null == birthday){

this.addFieldError("birthday","birthday invalid");

}

if(null == graduation){

this.addFieldError("graduation", "graduation invalid");

}

if(null != birthday && null != graduation){

Calendar c1 = Calendar.getInstance();

c1.setTime(birthday);


Calendar c2 = Calendar.getInstance();

c2.setTime(graduation);


if(c1.before(c2)){

this.addFieldError("graduation","birthday be forword graduation");

}


}

}


}

---------------------------------

三 配置<struts.xml>:

<?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>

<action name="register" class="com.struts2.RegisterAction">

<result name="success">/success.jsp</result>

<result name="input">/register.jsp</result>

</action>

</package>

</struts>

----------------------------------

四 <success.jsp>:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>welcom success page!</title>

</head>

<!-- 使用EL表达试取值 -->

<body>

<table align="center" width="40%" border="1" bgcolor="cyan">

<tr>

<td>username:</td>

<td>${requestScope.username }</td>

</tr>

<tr>

<td>password</td>

<td>${requestScope.password }</td>

</tr>

<tr>

<td>re-password</td>

<td>${requestScope.repassword }</td>

</tr>

<tr>

<td>age</td>

<td>${requestScope.age }</td>

</tr>

<tr>

<td>birthday</td>

<td>${requestScope.birthday }</td>

</tr>

<tr>

<td>graduation</td>

<td>${requestScope.graduation }</td>

</tr>

</table>

</body>

</html>