input.jsp
<s:form action="pointConverter">
<s:textfield name="point.x" label="x"></s:textfield>
<s:textfield name="point.y" label="y"></s:textfield>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="date" label="birthday"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
regist.jsp
<body>
<s:fielderror></s:fielderror>
<form action="register.action" method="post">
<table align="center" width="40%" border="1">
<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>
success.jsp
<table align="center" width="40%" border="1">
<tr>
<td>
username
</td>
<td>
${requestScope.username}
</td>
</tr>
<tr>
<td>
password
</td>
<td>
${requestScope.password}
</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>
RegistAction.java
package com.test.action;
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;
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
@Override
public void validate()
{
System.out.println("validate~~~~~~~~~~~~~~~~~~~");
if(null == username || username.length() < 6 || username.length() > 10)
{
this.addFieldError("username","username invalid");
}
if(null == password || password.length() < 6 || password.length() > 10)
{
this.addFieldError("password","password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10)
{
this.addFieldError("repassword","re-password invalid");
}
else if(!password.equals(repassword))
{
this.addFieldError("password","two passwords not the same");
}
if(age < 1 || age > 150)
{
this.addFieldError("age","age invalid");
}
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("birthday","birthday should be before graduation");
}
}
}
}
比较两个时间的先后可以用Calendar这个类。此类没有构造方法,因此可以用Calendar.getInstance()方式来获取一个实例。
c1.setTime(birthday)可为Calendar设置值,然后用c1.before(c2)或c1.after(c2)来比较两个时间的先后顺序。
也可用birthday.getTime()来获取毫秒数,然后比较大小。但不如Calendar方式规范。