如何正确重新填充< sj:datepicker timepicker ="true"> ;? [英] How do I correctly repopulate a <sj:datepicker timepicker="true">?
问题描述
我正在将 Struts2-jQuery Datepicker
与Timepicker
插件.
I'm using the Struts2-jQuery Datepicker
with the Timepicker
Addon.
假设从Action
getMyDate()
getter方法返回了java.util.Date
对象:
Assuming that a java.util.Date
object is returned form an Action
getMyDate()
getter method:
<sj:datepicker displayFormat="dd/mm/yy"
timepickerFormat="HH:mm"
timepicker="true"
name="myDate" />
这总是导致正确填充日期,但是timepicker
始终是00:00
.
This always results in the date being populated correctly, but the timepicker
is always 00:00
.
推荐答案
好吧,我已经找到时间扩展我在12月18日发现并在评论中简要提及的解决方案的时间.
Ok, I've found the time to expand the solution I've figured out on 18 december and briefly mentioned in the comments.
<sj:datepicker timepicker="true">
标记正确解析Date对象,但不能解析其String表示形式. 这在问题1057 中报告 strong>,处于接受状态,但仍未确定.
The <sj:datepicker timepicker="true">
tag correctly parses a Date object, but not its String representation. This is reported in Issue 1057, with status accepted but still not fixed.
此有效:
<sj:datepicker displayFormat="dd/mm/yy"
timepicker="true"
value="%{new java.util.Date()}" />
此无效:
<sj:datepicker displayFormat="dd/mm/yy"
timepicker="true"
value="%{'07/01/2015 14:42'}" />
不使用Converter时,您需要将日期时间的格式发送到String
变量,而不是发送到Date
变量:
When not using a Converter, you need to send the format of the date-time to a String
variable, instead than to a Date
one:
<sj:datepicker displayFormat="dd/mm/yy"
timepicker="true"
name="dateStr" />
private String dateStr;
public void setDateStr(String dateStr){
this.dateStr = dateStr;
}
public String getDateStr(){
return dateStr;
}
如果需要日期(可能需要这样做),则可以使用String Getter和Setter,它们将处理Date和String之间的解析和格式设置:
If you need a Date (you probably do), you can use a String Getter and Setter, that will handle the parsing and the formatting between Date and String:
private static final String FORMAT = "dd/MM/yyyy HH:mm";
private Date date;
public void setDateStr(String dateStr){
date = new SimpleDateFormat(FORMAT,Locale.ITALIAN).parse(dateStr);
}
public String getDateStr(){
return new SimpleDateFormat(FORMAT,Locale.ITALIAN).format(date);
}
但是,由于上述错误,这两种方法都不起作用.解决方案是如此简单,以至于我在对该问题的最后评论中笑了:
But both this methods, due to the bug linked above, won't work. The solution is so easy that made me laugh in my last comment to the question:
使用字符串设置器和日期获取器,则可以完美运行:
<sj:datepicker displayFormat="dd/mm/yy"
timepicker="true"
name="dateStr"
value="date" />
private Date date;
public void setDateStr(String dateStr){
date = new SimpleDateFormat(FORMAT,Locale.ITALIAN).parse(dateStr);
}
public Date getDate(){
return date;
}
(或编写一个Converter).
(or write a Converter).
旁注:不需要timepickerFormat="HH:mm"
,因为HH:mm
(24小时格式)已经是默认格式.仅在需要12小时显示格式时才需要更改它.
Side note: timepickerFormat="HH:mm"
is not needed because HH:mm
(24 hours format) is already the default format. You need to change it only if you want the 12 hours display format.
这篇关于如何正确重新填充< sj:datepicker timepicker ="true"> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!