我在Jsp(struts2应用程序)中为fromDate和toDate使用以下代码。
<s:label value="valid From* "/>
<sx:datetimepicker required="true" name="validFrom" displayFormat="dd/MM/yyyy" />
<s:label value="Valid To * :" />
<sx:datetimepicker required="true" name="validTo" displayFormat="dd/MM/yyyy" />
它正在以指定格式生成日期。我想检查所选的日期值,如果Todate小于Fromdate,必须显示错误消息。
有人能帮我解决这个问题吗。提前感谢。
可能最直接的方法是实现Validatable,这可能是通过扩展ActionSupport最容易实现的。注意,我在下面将"validFrom"改为"from",将"validTo"改为简单的"to"。
然后在你的行动中你会写。。。
//Not tested
public void Validate(){
//it is obvious that if one of these conditions is true the other will be as well
//this is just to show the typical case of building up multiple field errors
if (to.before(from)){
addFieldError("to", getText("To date must be later than from date."));
}
if (from.after(to)){
addFieldError("from", getText("From date must be before to date."));
}
}
由于我喜欢选择简单的主题,你会发现你可以用http://struts.apache.org/2.2.1.1/docs/fielderror.html以防您希望更多地控制错误消息的放置位置。
有关验证的更多信息,请参阅:http://struts.apache.org/2.x/docs/validation.html