struts2验证的时候 为什么类型转换错误的时候不提示呢?
我用了struts的标签库。但是我自己重写的validate()方法中的错误提示信息却都显示了。
如果类型转换错误,应该框架自己显示啊?正常的应该提示Invalid field value for field“”?
代码:
public class RegisterAction extends ActionSupport { String username;
String password;
String repassword;
int age;
Date startDate;
Date endDate;
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 getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
} public String execute() throws Exception {
System.out.println("RegisterAction.execute()");
return this.SUCCESS;
} public void validate() {
System.out.println("RegisterAction.validate()"); if(null == username || username.trim().length() <5 || username.trim().length()>12) {
this.addFieldError("username", "用户名的长度应为5-12");
} if(null == password || password.trim().length() <5) {
this.addFieldError("password", "密码的长度应为5-12");
}else if(null == repassword || repassword.trim().length() <5) {
this.addFieldError("repassword", "确认密码的长度应为5-12");
}else if(!password.equals(repassword)) {
this.addFieldError("password", "两次输入的密码不一致!");
}
if(age == 0) {
this.addFieldError("age","年龄不能为空");
}
if(age < 0 ||age >160) {
this.addFieldError("age","您所输入的年龄不对");
}
if(startDate != null && endDate != null){
Calendar start = Calendar.getInstance();
start.setTime(startDate); Calendar end = Calendar.getInstance();
end.setTime(endDate); if(!start.before(end)) {
this.addFieldError("startDate", "毕业日期应该在入学日期之前!");
}
} }
}
==========================================================================
jsp中用的是标签库: <s:form action="register">
<s:textfield name="username" label="username"> </s:textfield>
<s:textfield name="password" label="password"> </s:textfield>
<s:textfield name="repassword" label="password2"> </s:textfield>
<s:textfield name="age" label="age"> </s:textfield>
<s:textfield name="startDate" label="startDate"> </s:textfield>
<s:textfield name="endDate" label="endDate"> </s:textfield>
<s:submit label="submit"> </s:submit>
</s:form> ==================================================================================
配置文件:
<action name="register" class="com.cfd.struts2.RegisterAction">
<result name="success">/success.jsp </result>
<result name="input">/register_taglib.jsp </result>
</action>
=====================================
为什么自己的错误都能显示,但是类型转换的错误就是不出呢? 而且明显已经类型转换错误了,居然还能提交!!!
我用了struts的标签库。但是我自己重写的validate()方法中的错误提示信息却都显示了。
如果类型转换错误,应该框架自己显示啊?正常的应该提示Invalid field value for field“”?
代码:
public class RegisterAction extends ActionSupport { String username;
String password;
String repassword;
int age;
Date startDate;
Date endDate;
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 getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
} public String execute() throws Exception {
System.out.println("RegisterAction.execute()");
return this.SUCCESS;
} public void validate() {
System.out.println("RegisterAction.validate()"); if(null == username || username.trim().length() <5 || username.trim().length()>12) {
this.addFieldError("username", "用户名的长度应为5-12");
} if(null == password || password.trim().length() <5) {
this.addFieldError("password", "密码的长度应为5-12");
}else if(null == repassword || repassword.trim().length() <5) {
this.addFieldError("repassword", "确认密码的长度应为5-12");
}else if(!password.equals(repassword)) {
this.addFieldError("password", "两次输入的密码不一致!");
}
if(age == 0) {
this.addFieldError("age","年龄不能为空");
}
if(age < 0 ||age >160) {
this.addFieldError("age","您所输入的年龄不对");
}
if(startDate != null && endDate != null){
Calendar start = Calendar.getInstance();
start.setTime(startDate); Calendar end = Calendar.getInstance();
end.setTime(endDate); if(!start.before(end)) {
this.addFieldError("startDate", "毕业日期应该在入学日期之前!");
}
} }
}
==========================================================================
jsp中用的是标签库: <s:form action="register">
<s:textfield name="username" label="username"> </s:textfield>
<s:textfield name="password" label="password"> </s:textfield>
<s:textfield name="repassword" label="password2"> </s:textfield>
<s:textfield name="age" label="age"> </s:textfield>
<s:textfield name="startDate" label="startDate"> </s:textfield>
<s:textfield name="endDate" label="endDate"> </s:textfield>
<s:submit label="submit"> </s:submit>
</s:form> ==================================================================================
配置文件:
<action name="register" class="com.cfd.struts2.RegisterAction">
<result name="success">/success.jsp </result>
<result name="input">/register_taglib.jsp </result>
</action>
=====================================
为什么自己的错误都能显示,但是类型转换的错误就是不出呢? 而且明显已经类型转换错误了,居然还能提交!!!
解决方案 »
- j2ee的SDK在xp系统中的配置问题
- servlet向jsp传参的问题
- Struts2.1能用 ${param['user.name']}这样的写法么
- js如何获得nested 标签下的某文本框的值.
- 用java实现mp3的截取功能,比如选取MP3中第2分钟到第3分钟的音乐,并转为acc编码.请问需要什么技术,请大家帮忙
- 一道非常有意思的题,可是我不会
- 在进行HIBERNATE调试的时候,出现问题!请赐教!!!
- 如何获得当前应用中的全部session?
- 请问Tomcat5.0的servlet存放在什么目录下?
- 我现在不能访问到sun,谁能告诉我一个J2ee的链接,谢谢先
- 把jsp改成html里边的标签怎么办啊???、急
- JSF的问题
这个问题你还是到google搜下。