我如何正确地重新填充 <sj:datepicker timepicker=&quot;true&quot;>? [英] How do I correctly repopulate a &lt;sj:datepicker timepicker=&quot;true&quot;&gt;?
问题描述
我正在使用 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,状态已接受但仍未修复.
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'}" />
当不使用转换器时,您需要将日期时间的格式发送到 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,它们将处理日期和字符串之间的解析和格式:
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:
使用一个 String setter 和一个 Date getter,那么它就会完美地工作:
Use a String setter, and a Date getter, then it will work perfectly:
<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;
}
(或编写转换器).
旁注: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=&quot;true&quot;>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!