Spring Roo最佳做法,以当地时间显示日期 - java

Spring Roo的强大之处在于它可以处理困难的工作。

问题:是否有最佳的最佳实践和实现方式来向每个用户展示他在这个星球上的住处,以当地时间显示日期。

问题:
当用户输入日期并转到“列出所有访问”页面时,如果他/她所在的时区与OpenShift服务器所在的时区不同,则他/她将看到不同的日期。例如,我在GMT + 1(西欧)时区。

Spring Roo最佳做法,以当地时间显示日期 - java

我的情况:

Web应用程序是使用Spring Roo 2.0 / gvNIX 2.0开发的
将托管在OpenShift(rhcloud.com)上
就像宠物诊所的样本一样:https://petclinic-gvnix.rhcloud.com/
用户可以生活在世界各地,因此可以在每个时区生活。
目前还不能选择Java 8,因为OpenShift尚未部署TomCat 8,并且,如果可能的话,我想避免创建OpenShift DIY应用程序。

需求:

每个用户都应该在其本地时间中看到日期。

复制:

进入Petclinic示例并登录:https://petclinic-gvnix.rhcloud.com/
选择访问->创建新访问
选择列出所有访问

当您所在的时区与Openshift服务器位于不同的时区时,您将获得如上所示的信息

参考方案

考虑了这个问题之后,我认为我有一个干净的解决方案,可以在不影响当前项目的情况下实现时区支持。

我的出发点是解决方案应向后兼容,并且不应破坏当前的项目。同样,更改应尽可能少。

我对其进行了测试,并且在我的Web应用程序在Openshift上运行(在TimeZone GMT-5:00上托管,而我居住在西欧,GMT +1:00上)时也可以正常工作

请给我您的意见。

我将描述show.jspx视图的解决方案/建议。但是,以同样的方式,它也可以用于其他视图。

更改的实质是我在WEB-INF / tags / form / fields / column.tagx / display.jspx中的fmt:formatDate ..语句中添加了timeZone:

旧:

<fmt:formatDate value="${object[field]}" pattern="${fn:escapeXml(dateTimePattern)}" />

新:

<fmt:timeZone value="${timeZone}">
    <fmt:formatDate value="${object[field]}" pattern="${fn:escapeXml(dateTimePattern)}" timeZone="${timeZone}" />
</fmt:timeZone>

为此,我还添加了声明和未使用时的默认timeZone设置。

<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>

添加/更改这几行时,所有旧代码将继续起作用。
您还必须将这些更改应用于WEB-INF / tags / form / fields / column.tagx文件。

我在使用gvNIX JQuery时,也应该在其中应用相同的更改。

这就是您要做的所有更改!!!!

要在所有show.jspx文件中添加时区支持:
1)为任何日期添加额外的时区选项。

旧:

<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"/>

2)在您的控制器中指定您的timeZone,例如:

uiModel.addAttribute("fileUpload_uploaddate_date_timezone", "Europe/Amsterdam");

秉承Spring Roo的数据格式精神,addDateTimeFormatPatterns(uiModel);我添加了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());

}

Spring MVC拦截器映射问题 - java

我有这段XML:<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/statics/**" /> <bean class="com.company.website.servlet.StaticsHandlerIntercept…

SOAPFaultException部署在Tomcat上时,但在GlassFish中工作正常 - java

朋友们,我一直在尝试很多,阅读了很多论坛,但无法理解为什么出现此问题。我使用契约优先方法创建了一个Jax-WS WebService。创建WSDL和XSD,然后使用wsimport工具生成其余工件,为SEI提供实现。将WebService应用程序部署到Eclipse Helios中的GlassFish(Glassfish适配器和Eclipse中安装的插件)。…

Java Applet的URLConnection与PHP无效 - java

我已经研究了Oracle文档和示例,但仍然无法正常工作。我有一个Java Applet,它只是尝试使用URLConnection和OutputStreamWriter通过POST将文本字段发送到PHP脚本。 Java方面似乎工作正常,没有引发异常,但是PHP在我的页面上未显示任何输出。我是PHP新手,因此请耐心等待。这是相关的Java部分: try { UR…

页面加载而不是提交时发生struts验证 - java

请原谅我;我对Struts有点陌生。我遇到一个问题,即页面加载而不是我实际提交表单时发生了验证。我整天都在论坛上搜寻和搜寻,没有任何运气。我显然做错了一些事情,应该很容易确定,但是我还没有发现问题所在。这是我的struts.xml的片段:<action name="*Test" method="{1}" clas…

DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java

以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…