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");
}
}
}
<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");
}
}
}
解决方案 »
- 集成环境IntelliJ 12发布项目出软件包 javax.servlet.http不存在
- REST风格的项目部署在tomcat上,静态资源如何访问
- 急!!!CKEditor 为什么显示不出来???
- 请教一下: EJB2和EJB3开发的区别在什么地方?
- 如何设置Jfreechart折线图中线条的宽度
- spring+hibernate做查询时,如果查到数据就保错,查不到就没事,怎么回事
- struts标签下拉列表问题
- 应用程序经常出现管道错误,是什么引起的,有解决方案吗??? 急急急!!!!
- 请朋友介绍一下,哪里有关于JBUILDER调试技术的资料?
- 单个jsp文件放在weblogic哪个目录下?
- Java怎么提高导出大批量数据的效率
- XML中的xsl问题!请高手指教,急急急!!!!
由于RegisterAction 中定义的age为int类型,
遇到了类型转换错误的时候,struts2框架应该自动生成一条错误信息,
并将该错误信息放到addFieldError里面,并且显示:age error
而我的程序不能显示第一行内容,只能显示在action中自己定义的错误内容