2012-02-22 51 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