一个简单的strutseb2服务器端验证,竟然花了一个多小时才完成。。。。找了半天原来是一个方法名写错了。。笔误。。
把getUsername().写成了getUserame(),然后一直取不出username的值。。。。
1.注册页面(register.jsp)
(4)struts2配置文件(struts.xml)
OK...
把getUsername().写成了getUserame(),然后一直取不出username的值。。。。
1.注册页面(register.jsp)
<%
@ page language="java" contentType="text/html; charset=GBK"
%>
<% @taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 请输入您的注册信息 </ title >
</ head >
< body >
< H1 > 请输入您的注册信息 </ H1 >
< s:fielderror />
< FORM METHOD ="POST" ACTION ="regist.action" >
用户名: < INPUT TYPE ="text" NAME ="username" >< br >
密 码: < INPUT TYPE ="text" NAME ="pass" >< br >
年 龄: < INPUT TYPE ="text" NAME ="age" >< br >
生 日: < INPUT TYPE ="text" NAME ="birthday" >< p >
< INPUT TYPE ="submit" value ="注册" >
</ FORM >
</ body >
</ html >
(2)Action类(RegisterAction.java)
<% @taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 请输入您的注册信息 </ title >
</ head >
< body >
< H1 > 请输入您的注册信息 </ H1 >
< s:fielderror />
< FORM METHOD ="POST" ACTION ="regist.action" >
用户名: < INPUT TYPE ="text" NAME ="username" >< br >
密 码: < INPUT TYPE ="text" NAME ="pass" >< br >
年 龄: < INPUT TYPE ="text" NAME ="age" >< br >
生 日: < INPUT TYPE ="text" NAME ="birthday" >< p >
< INPUT TYPE ="submit" value ="注册" >
</ FORM >
</ body >
</ html >
package
my;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class RegisterAction extends ActionSupport
{
private String username;
private String pass;
private int age;
private Date birthday;
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
public void setPass(String pass)
{
this.pass = pass;
}
public String getPass()
{
return this.pass;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Date getBirthday()
{
return this.birthday;
}
public String execute()throws Exception
{
return SUCCESS;
}
}
(3)验证文件.RegisterAction-validator.xml
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class RegisterAction extends ActionSupport
{
private String username;
private String pass;
private int age;
private Date birthday;
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
public void setPass(String pass)
{
this.pass = pass;
}
public String getPass()
{
return this.pass;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Date getBirthday()
{
return this.birthday;
}
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 ="username" >
< field-validator type ="requiredstring" >
< param name ="trim" > true </ param >
< message > please input your username </ message >
</ field-validator >
</ field >
< field name ="pass" >
< field-validator type ="requiredstring" >
< param name ="trim" > true </ param >
< message > please input your password </ message >
</ field-validator >
</ field >
< field name ="age" >
< field-validator type ="int" >
< param name ="min" > 1 </ param >
< param name ="max" > 100 </ param >
< message > age must between 1 and 100 </ message >
</ field-validator >
</ field >
< field name ="birthday" >
< field-validator type ="date" >
< param name ="min" > 1900-01-01 </ param >
< param name ="max" > 2050-01-01 </ param >
< message > birtday is error </ message >
</ field-validator >
</ field >
</ validators >
<! 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 ="username" >
< field-validator type ="requiredstring" >
< param name ="trim" > true </ param >
< message > please input your username </ message >
</ field-validator >
</ field >
< field name ="pass" >
< field-validator type ="requiredstring" >
< param name ="trim" > true </ param >
< message > please input your password </ message >
</ field-validator >
</ field >
< field name ="age" >
< field-validator type ="int" >
< param name ="min" > 1 </ param >
< param name ="max" > 100 </ param >
< message > age must between 1 and 100 </ message >
</ field-validator >
</ field >
< field name ="birthday" >
< field-validator type ="date" >
< param name ="min" > 1900-01-01 </ param >
< param name ="max" > 2050-01-01 </ param >
< message > birtday is error </ message >
</ field-validator >
</ field >
</ validators >
(4)struts2配置文件(struts.xml)
<?
xml version="1.0" encoding="GBK"
?>
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="my" extends ="struts-default" >
< action name ="regist" class ="my.RegisterAction" >
< result name ="success" > /show.jsp </ result >
< result name ="input" > /register.jsp </ result >
</ action >
</ package >
</ struts >
success页面(show.jsp)
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="my" extends ="struts-default" >
< action name ="regist" class ="my.RegisterAction" >
< result name ="success" > /show.jsp </ result >
< result name ="input" > /register.jsp </ result >
</ action >
</ package >
</ struts >
<%
@ page language="java" contentType="text/html; charset=GBK"
%>
<% @taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GBK" />
< title > 校验成功 </ title >
</ head >
< body >
< H3 > 校验成功 </ H3 >
用户名: < s:property value ="username" />< br >
密码: < s:property value ="pass" />< br >
年龄: < s:property value ="age" />< br >
生日: < s:property value ="birthday" />< br >
</ body >
</ html >
<% @taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GBK" />
< title > 校验成功 </ title >
</ head >
< body >
< H3 > 校验成功 </ H3 >
用户名: < s:property value ="username" />< br >
密码: < s:property value ="pass" />< br >
年龄: < s:property value ="age" />< br >
生日: < s:property value ="birthday" />< br >
</ body >
</ html >
OK...