类型转换输入校验执行流程:
1)首先执行类型转换
2)然后执行输入校验
3)如果以上任一项出现错误将不执行excute方法。直接跳转到本action对相应的名为input的result页面
action:
@Override public void validate() { //姓名校验 if (null==username||username.length()<3||username.length()>10){ this.addActionError(" username invalid"); this.addFieldError("username", "username invalid in field!"); } //密码校验 if (null==password||password.length()<3||password.length()>6){ this.addActionError("password invalid!!"); }else if (repassword==null||repassword.length()<3||repassword.length()>6){ this.addActionError("repassword invalid!!"); } else if (!password.equals(repassword)){ this.addActionError("The passwords not same!!"); } //年龄校验 if(age<10||age>60){ this.addActionError("age invalid"); } //出生日期校验 if (null==birthday){ this.addActionError("birthday invalid!!"); } //毕业日期校验 if(null==graduate){ this.addActionError("graduate invalid!!"); } //如果出生日期在毕业之前 if (birthday!=null&&graduate!=null){ Calendar c1=Calendar.getInstance(); c1.setTime(birthday); Calendar c2=Calendar.getInstance(); c2.setTime(graduate); if (c1.after(c2)){ this.addActionError("Birthday not befor graduate!!"); } }
struts标签库:
<s:actionerror cssStyle="color=red"/>
``````````````````````````````````````
<s:fielderror cssStyle="color=blue"></s:fielderror>
<s:form action="register.action" theme="simple"> 姓名:<s:textfield name="username" label="username"></s:textfield><br> 密码:<s:password name="password" label="password"></s:password><br> 重置密码:<s:password name="repassword" label="repassword"></s:password><br> 年龄:<s:textfield name="age" label="age"></s:textfield><br> 出生日期:<s:textfield name="birthday" label="birthday"></s:textfield><br> 毕业日期:<s:textfield name="graduate" label="graduate"></s:textfield><br> <s:submit value="提交"></s:submit> </s:form>