1. register.jsp <s:form action="register" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="password" label="password"></s:textfield>
<s:textfield name="repassword" label="repassword"></s:textfield>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="birthday" label="birthday"></s:textfield>
<s:textfield name="graduation" label="graduation"></s:textfield>
<s:submit label="提交" ></s:submit>
</s:form>2.  struts.xml
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="struts2" extends="struts-default">
<action name="register" class="test.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>3. src 下的message.properties
 xwork.default.invalid.fieldvalue={0} error4. RegisterAction.java中的validate()方法
public void validate() {
System.out.println("validate");
if (null == username || this.username.length() < 6
|| this.username.length() > 10) {
this.addFieldError("username", "username is invaild");
} if ( null == password) 
{
this.addFieldError("password", "password is null");
} else if ( null == repassword) 
{
this.addFieldError("password", "repassword is null");
} else if( !password.equals(repassword)){
this.addFieldError("password","password and repassword not the same");
}else{

} if (age < 1 || age > 150) {
this.addFieldError("age", "age should between 1  and 150");
} 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 must before graduation");
}
}
}

解决方案 »

  1.   

    当表单中的age填写一个abc字符串时,
    由于RegisterAction 中定义的age为int类型,
    遇到了类型转换错误的时候,struts2框架应该自动生成一条错误信息,
    并将该错误信息放到addFieldError里面,并且显示:age error 
    而我的程序不能显示第一行内容,只能显示在action中自己定义的错误内容
      

  2.   

    我知道怎么回事了,struts2.0.12-all与struts2.0.11-all不一致,在视频中风中叶老师是用的struts2.0.11,换成其相应的jar包后,问题解决。请问,如果用struts2.0.12,该怎么样解决?请高手指点!!