我是Struts2的新手。我正在使用Struts2 JQuery datepicker tag。但我無法驗證它。在我的validation.xml
文件中,我將它作爲date
,但它刪除了時間部分。任何解決方案?我應該使用正則表達式來驗證我的日期時間嗎?Struts 2 datepicker與時間驗證
JSP表單
代碼是有點相同,因爲它是在我earlier question
Action類
public Date getDateAndTimeOfOccurance() {
return dateAndTimeOfOccurance;
}
public void setDateAndTimeOfOccurance(Date dateAndTimeOfOccurance) {
System.out.println(dateAndTimeOfOccurance);
this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
}
public String execute() throws Exception {
.
.
ps.setTimestamp(13, new java.sql.Timestamp(getDateAndTimeOfOccurance().getTime()));
/* i have modified my database because the time values in java.sql.Date have been depreciated */
.
.
}
是否有任何現有的解決方案?我應該使用regex
驗證程序嗎?
UPDATE
對於實驗,我刪除了驗證的一部分,但在行動收到的時間仍是00:00:00
。
更新2 我發現了一種通過在正則表達式的評論中提到的或有驗證字符串作爲一個日期的支柱驗證方式recieving解決這個問題(如使用validate()
方法,我不知道這是一個很好的做法)。
更新的代碼(與更新2)
Action類
@RequiredStringValidator(message = "Please enter the date and time of occurance")
public String getDateAndTimeOfOccurance() {
return dateAndTimeOfOccurance;
}
public void setDateAndTimeOfOccurance(String dateAndTimeOfOccurance) {
this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
}
public void execute(){
....
Date d = null;
try {
d = new SimpleDateFormat("dd-MMM-yyyy hh:mm", Locale.getDefault()).parse(getDateAndTimeOfOccurance());
} catch (java.text.ParseException e) {
e.printStackTrace();
addFieldError(dateAndTimeOfOccurance, "Please enter a valid date");
return INPUT;
}
Timestamp t = new java.sql.Timestamp(d.getTime());
ps.setTimestamp(13, t);
...
}
您是否閱讀過該問題的評論? –
@AleksandrM謝謝。但我無法驗證它。我應該使用正則表達式還是日期? – maxx777
@ maxx777你已經提出了同樣的問題http://stackoverflow.com/q/22836503/573032。如果你可以更新它,這反映了最後的狀態,並以一些可讀的格式來制定問題,順便說一句,你有什麼不喜歡你有答案,你接受? –