0

I'm trying to convert some timestamp values from a collection to a human readable date format. Within a jsp, I've coded this loop:

  <c:forEach items="${tokenCollection}" var="tokenCollection" varStatus="status">
      <jsp:setProperty name="dateValue" property="time" value="${tokenCollection.value.timestamp}" />
      <fmt:formatDate value="${dateValue}" pattern="MM/dd/yyyy HH:mm" /> 
      <!-- Correctly prints values as "40aa4ab4-f6c1-458a-9v3d-5d13b872d9c2" -->
      <c:out value="${tokenCollection.value.value}"/>
      <!-- Correctly prints values as "1378722681816" -->
      <c:out value="${tokenCollection.value.timestamp}"/>
  </c:forEach>

Unfortunately, all I get by running it is:

org.apache.jasper.JasperException: An exception occurred processing JSP page /session_management.jsp at line 36

33:       <p>Dati di sessione - utente "<c:out value="${username}" />"</p>
34:       
35:       <c:forEach items="${tokenCollection}" var="tokenCollection" varStatus="status">
36:           <jsp:setProperty name="dateValue" property="time" value="${tokenCollection.value.timestamp}" />
37:           <fmt:formatDate value="${dateValue}" pattern="MM/dd/yyyy HH:mm" />                      
38:           <c:out value="${tokenCollection.value.value}"/>
39:           <c:out value="${tokenCollection.value.timestamp}"/>

org.apache.jasper.JasperException: org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "${tokenCollection.value.timestamp}"

As stated in the comments, values are retreived correctly, so ${tokenCollection.value.timestamp} is by all means a timestamp (like 1378722681816). Still, I can't figure out how to fix this issue, or what exactly I'm being wront at. Any hints?

Seether
  • 1,524
  • 1
  • 14
  • 28

1 Answers1

0

jsp:setProperty is for setting a property on a bean; the bean needs to be declared first using jsp:useBean. This will call the setTime method on the com.something.className class; it won't create a dateValue variable.

<jsp:useBean id="dateValue" class="com.something.className" scope="page" />
<jsp:setProperty name="dateValue" property="time" value="${tokenCollection.value.timestamp}" />

You can use your value directly:

<fmt:formatDate value="${tokenCollection.value.timestamp}" pattern="MM/dd/yyyy HH:mm" /> 

Or if you really need it in different variable. use c:set:

<c:set var="dateValue" value="${tokenCollection.value.timestamp}" />
<fmt:formatDate value="${dateValue}" pattern="MM/dd/yyyy HH:mm" /> 
kielni
  • 4,779
  • 24
  • 21
  • Hi, thanks for your help. By directly using I get: Unable to convert string "${tokenCollection.value.timestamp}" to class "java.util.Date" for attribute "value": Property Editor not registered with the PropertyEditorManager – Seether Sep 09 '13 at 14:36
  • The error message means fmt:formatDate expects a date object (java.util.Date), but you're giving it a string. You'll need to convert your string to a date first using fmt:parseDate. See http://stackoverflow.com/questions/75489/formatting-a-long-timestamp-into-a-date-with-jstl Maybe that's where your jsp:setProperty came from? – kielni Sep 10 '13 at 00:01
  • `` `` `` – kielni Sep 10 '13 at 00:08
  • Thanks, I had already read that other question but unfortunately hadn't worked for me. Unfortunately your solution doesn't seem to be working either, as I get a NullPointerException at the setProperty line – Seether Sep 10 '13 at 08:04
  • Can you see any more info about the exception? Can you print out the parameter you're passing to the setProperty? I can't seem to get the code to format properly, but this worked for me: `<%@ taglib prefix="fmt" uri="http://jakarta.apache.org/taglibs/fmt" %> ` – kielni Sep 10 '13 at 15:16
  • Hi @kielni, apart from not being able to use your taglibs (doesn't seem to be there anymore), I tried to put your code in but unfortunately didn't work. Here's full Tomcat stack trace: https://dl.dropboxusercontent.com/u/6299368/Apache%20Tomcat_7.0.42%20-%20Error%20report.htm – Seether Sep 10 '13 at 19:07
  • there shouldn't be a space in the dateValue: `` – kielni Sep 10 '13 at 19:28