2012-02-22 56 views
0

我在jsp(struts2应用程序)中使用fromDate和toDate的以下代码。如何使用struts2中的<sx:datetimepicker>验证日期,日期?

<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,则必须显示错误消息。

任何人都可以帮助我解决这个问题。提前致谢。

回答

0

可能最直接的方法是实现Validateable,这可能是最容易通过扩展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