4

For my open source project I'm in the process of switching date fields from java.util.Date to Java 8's java.time.LocalDateTime.

In the JSP's I'm using Struts tags to format the java.util.Date from the passed-in bean, however, those won't work with Java 8 time. I use the Struts tag not only to retrieve the time field but also the formatting string to be used to parse it:

<s:text name="generic.date.toStringFormat">
    <s:param value="myBean.timeField" />
</s:text>

"generic.date.toStringFormat" is in the message resource file, and myBean.timeField is from the Action form backing the JSP.

Looking at this post: JSTL LocalDateTime format, it seems there are two alternatives I can use, Sargue's https://github.com/sargue/java-time-jsptags or a custom EL function, for example with the former:

<javatime:format value="myBean.timeField" pattern="generic.date.toStringFormat"/>

Problem is, "myBean.timeField" and "generic.date.toStringFormat" seem to be resolvable only within Struts tags. If <javatime> were a standard HTML tag, I could use <s:property/> to get the values resolved, e.g.,

<span><s:property value="myBean.timeField"/></span>

But <javatime:.../> is a JSP tag and I can't seem to resolve Struts JSP tags within other JSP tags. Question: How can I extract the bean value myBean.timeField and the message resource value generic.date.toStringFormat into variables so I can reference them in the <javatime/> tag above? (If this can be done via the custom EL function option instead, that will work for me also.)

Community
  • 1
  • 1
Glen Mazza
  • 748
  • 1
  • 6
  • 27

2 Answers2

1

You can use EL in the value attribute. Struts2 wrapped a request to search attributes from the valueStack. This is native access to your action variables from the EL.

<javatime:parseLocalDateTime value="${myBean.timeField}" pattern="generic.date.toStringFormat" var="parsedDate" />
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • ${myBean.timeField} works perfectly, thx, the pattern portion (with or without the ${ }'s) is still failing though, I'm going to see if I can use the JSTL fmt tag to extract the resource value into a var so I can do something like pattern={$patternVar} in the jt:pLDT tag. – Glen Mazza Jun 12 '16 at 19:12
1

More information to add to Roman's answer: To obtain the pattern from a resource file, add in the JSTL fmt tag and place it into a variable:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:message key="generic.date.toStringFormat" var="dateFormat"/>

Taken together with Roman's solution above for extracting ${myBean.timeField}:

<javatime:format value="${myBean.timeField}" pattern="${dateFormat}" />

As an aside, for date variables being retrieved from a Struts iterator tag:

<s:date name="#iter.myDateField"/>

This can be replaced by using the Struts set tag:

<s:set var="tempTime" value="#comment.postTime"/>
<javatime:format value="${tempTime}" pattern="${dateFormat}"/>
Glen Mazza
  • 748
  • 1
  • 6
  • 27