jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix='s' uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:fielderror></s:fielderror>
<form action="register.action" method="post">
username:<input type="text" name="username" value="${username}"/><br>
password:<input type="password" name="password"/><br>
repassword:<input type="password" name="repassword"/><br>
age:<input type="text" name="age"/><br>
birthday:<input type="text" name="birthday"/><br>
graduation:<input type="text" name="graduation"/><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form> <br>
</body>
</html>
action类代码:
package com.test.action;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
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 getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
@Override
public void validate() {
if (null == username || username.length() < 6 || username.length() > 10) {
this.addFieldError("username", "username is invalid!");
}
if (null == password || password.length() < 6 || password.length() >10) {
this.addFieldError("password", "password is invalid!");
} else if (null == repassword || repassword.length() < 6 || repassword.length() > 10) {
this.addFieldError("repassword", "repassword is invalid!");
} else if (!password.equals(repassword)) {
this.addFieldError("repassword", "password is not equals repassword!");
}
if (age < 1 || age >150) {
this.addFieldError("age", "age is invalid!");
}
if (null == birthday) {
this.addFieldError("birthday", "birthday is invalid!");
} else if (null == graduation) {
this.addFieldError("graduation", "graduation is invalid!");
} else {
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);
Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);
if (!c1.before(c2)){
this.addFieldError("birthday", "birthday is not before graduation!");
}
}
}
}
struts.xml内容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/result.jsp</result>
<result name="failer">/login2.jsp</result>
</action>
<action name="pointconvert" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>
<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
当表单中的age中填写 一个abc字符串时,由于RegisterAction 中定义的age为int类型,遇到了类型转换错误的时候(也就是说不能进行类型转换),struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面,也就是上图第一行中显示 的内容 Invalid field value for field “age”
而我的程序却不能显示第一行的内容,只能显示在action中自己定义的错误内容,也就是第二行的内容。请高手指教,为什么自动类型转换的错误消息不能出来呢?