请指点struts2校验框架对date类型属性的校验!!!!!

庶人 2010-12-06 04:13:16
以下是我用struts2框架校验日期的范围:
struts.xml文件:
<package name="newer" extends="struts-default">
<action name="text" class="com.newer.action.TextAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
TextAction类代码:
public class TextAction extends ActionSupport {
private Date birth;

public Date getBirth() {
return birth;
}

public void setBirth(Date birth) {
this.birth = birth;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
index.jsp页面:(<%@ taglib uri="/struts-tags" prefix="s"%>也导入了)
<body>
<s:form action="text">
<s:textfield name="birth" label="birth"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
对birth进行类型转换的自定义类:
@Override
public Object convertValue(Map context, Object value, Class toType) {
// TODO Auto-generated method stub

if(toType==Date.class){
String[] birth=(String[])value;
String str=birth[0];
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parseObject(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.convertValue(context, value, toType);
}

注册全局类型转换器:
xwork-conversion.properties:
java.util.Date=com.newer.convert.UserConvert

校验框架的配置文件:(该文件我已经确定跟TextAction在同一目录了)
TextAction-validation.xml:
<validators>
<field name="birth">
<field-validator type="required">
<message>birth not null</message>
</field-validator>
<field-validator type="date">
<param name="max">2010-01-01</param>
<param name="min">2000-01-01</param>
<message>birth should between 2000-01-01 and 2010-01-01</message>
</field-validator>
</field>
</validators>



当我在表单输入的birth值不在2000-01-01到2010-01-01之间时既然也调到了成功页面success.jsp,而且在页面也正式显示了,我知道struts2在日期类型转换这块,strut2本身也提供了对date类型属性的自动转换,但是这个自动转换它有时会执行,但有时不执行,像我之前没有写自定义类型转换器时,struts2帮我转换了,后来莫名其妙它又没有帮我转换了,我在网上也找过相关资料,确实好多网友也这么说过。我主要是想大家帮我看看为什么这个范围校验为什么不能起作用
...全文
158 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧