Java Spring Roo在当地时间显示日期的最佳实践
SpringRoo的强大之处在于它能处理困难的事情 问:如果有一个好的最佳实践和实现,如何向每个用户显示他在这个星球上居住的地方,当地时间的日期 问题: 当用户输入日期并进入“列出所有访问”页面时,如果他/她位于与OpenShift服务器不同的时区,他/她将看到完全不同的日期。例如,我在GMT+1(西欧)时区 我的情况:Java Spring Roo在当地时间显示日期的最佳实践,java,spring,datetime,spring-roo,gvnix,Java,Spring,Datetime,Spring Roo,Gvnix,SpringRoo的强大之处在于它能处理困难的事情 问:如果有一个好的最佳实践和实现,如何向每个用户显示他在这个星球上居住的地方,当地时间的日期 问题: 当用户输入日期并进入“列出所有访问”页面时,如果他/她位于与OpenShift服务器不同的时区,他/她将看到完全不同的日期。例如,我在GMT+1(西欧)时区 我的情况: Web应用程序是使用SpringRoo2.0/GVNIX2.0开发的 将托管在OpenShift(rhcloud.com)上 与petclinic样本类似: 用户可以生活在世
当您处于与Openshift服务器不同的时区时,您会得到类似于上面所述的东西。考虑到这个问题,我认为我有一个干净的解决方案,可以在不影响当前项目的情况下实现时区支持 我的出发点是解决方案应该是向后兼容的,并且不应该破坏当前的项目。此外,变化应尽可能小 当我的web应用程序在Openshift上运行时,我测试了它,它也可以工作,在Openshift上,它的主机位于时区GMT-5:00,而我住在西欧,GMT+1:00 请给我你的意见 我将描述show.jspx视图的解决方案/提案。但以同样的方式,它也可以用于其他视图 更改的本质是我在fmt:formatDate中添加了时区。。WEB-INF/tags/form/fields/column.tagx/display.jspx中的语句 旧的: 本着SpringRoo的dataformataddDateTimeFormatPatterns(uiModel)的精神I添加addTimeZone(uiModel) 在此方法中,您可以指定时区的来源。 在我的web应用程序中,我要求用户在重新启动时指定他/她的时区。所有其他解决方法都会在某个地方失败
void addTimeZone(Model uiModel) {
uiModel.addAttribute("fileUpload_uploaddate_date_timezone", UserUtils.geTimeZone());
}
顺便说一句,AspectJ控制器中的addTimeZone默认方法可以如下面的示例所示,让程序员能够插入并修改它
void FileUploadController.addTimeZone(Model uiModel) {
uiModel.addAttribute("fileUpload_uploaddate_date_timezone", TimeZone.getDefault().getID());
}TL;问题是-我如何使用人们的本地时间而不是服务器时间?这篇文章的其余部分都是浮躁的。好的,很好的总结。最佳做法是什么?
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:output omit-xml-declaration="yes" />
<jsp:directive.attribute name="id" type="java.lang.String" required="true" rtexprvalue="true" description="The identifier for this tag (do not change!)" />
<jsp:directive.attribute name="object" type="java.lang.Object" required="true" rtexprvalue="true" description="The form backing object" />
<jsp:directive.attribute name="field" type="java.lang.String" required="true" rtexprvalue="true" description="The field name" />
<jsp:directive.attribute name="label" type="java.lang.String" required="false" rtexprvalue="true" description="The label used for this field, will default to a message bundle if not supplied" />
<jsp:directive.attribute name="date" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate that this field is of type java.util.Date" />
<jsp:directive.attribute name="calendar" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate that this field is of type java.util.Calendar" />
<jsp:directive.attribute name="dateTimePattern" type="java.lang.String" required="false" rtexprvalue="true" description="The date / time pattern to use if the field is a date or calendar type" />
========== Added declaration
<jsp:directive.attribute name="timeZone" type="java.lang.String" required="false" rtexprvalue="true" description="The timezone to use if the field is a date or calendar type" />
========== End
<jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" />
<jsp:directive.attribute name="z" type="java.lang.String" required="false" description="Used for checking if element has been modified (to recalculate simply provide empty string value)" />
<c:if test="${empty render or render}">
<c:if test="${not empty object and empty label}">
<spring:message code="label_${fn:toLowerCase(fn:substringAfter(id,'_'))}" var="label" htmlEscape="false" />
</c:if>
<c:if test="${empty dateTimePattern}">
<c:set value="MM/dd/yyyy" var="dateTimePattern" />
</c:if>
========== Added default setting. Taking the timezone of the server!
<c:if test="${empty timeZone}">
<jsp:scriptlet>
jspContext.setAttribute("timeZone", java.util.TimeZone.getDefault().getID());
</jsp:scriptlet>
</c:if>
========== End
<div id="_${fn:escapeXml(id)}_id">
<label for="_${fn:escapeXml(field)}_id">
<c:out value="${label}" />
:
</label>
<div class="box" id="_${fn:escapeXml(id)}_${fn:escapeXml(field)}_id">
<c:choose>
<c:when test="${date}">
<spring:escapeBody>
========== Changed: added timeZone support for Date
<fmt:timeZone value="${timeZone}">
<fmt:formatDate value="${object[field]}" pattern="${fn:escapeXml(dateTimePattern)}" timeZone="${timeZone}" />
</fmt:timeZone>
========== End
</spring:escapeBody>
</c:when>
<c:when test="${calendar}">
<spring:escapeBody>
========== Changed: added timeZone support for Calendar
<fmt:timeZone value="${timeZone}">
<fmt:formatDate value="${object[field].time}" pattern="${fn:escapeXml(dateTimePattern)}" timeZone="${timeZone}" />
</fmt:timeZone>
========== End
</spring:escapeBody>
</c:when>
<c:otherwise>
<spring:eval expression="object[field]" />
</c:otherwise>
</c:choose>
</div>
</div>
<br />
</c:if>
</jsp:root>
<field:display date="true" dateTimePattern="${fileUpload_uploaddate_date_format}" field="uploadDate" id="s_com_myproject_onlineviewer_domain_FileUpload_uploadDate" object="${fileupload}" z="user-managed"/>
<field:display date="true" dateTimePattern="${fileUpload_uploaddate_date_format}" field="uploadDate" id="s_com_myproject_onlineviewer_domain_FileUpload_uploadDate" object="${fileupload}" timeZone="${fileUpload_uploaddate_date_timezone}" z="user-managed"/>
uiModel.addAttribute("fileUpload_uploaddate_date_timezone", "Europe/Amsterdam");
void addTimeZone(Model uiModel) {
uiModel.addAttribute("fileUpload_uploaddate_date_timezone", UserUtils.geTimeZone());
}
void FileUploadController.addTimeZone(Model uiModel) {
uiModel.addAttribute("fileUpload_uploaddate_date_timezone", TimeZone.getDefault().getID());