2

I've got a Spring Web MVC application where I need to get a JavaScript Array filled with java.util.Dates that are stored in an ArrayList accessible from the webapp with ${cust.dates}. How do I properly initialize the array in the webapp?

Thank you.

5
  • JavaScript Array filled with java.util.Dates? Commented Oct 6, 2011 at 13:06
  • Well, naturally the java.util.Dates should be converted to JS Dates. Commented Oct 6, 2011 at 13:08
  • How do I use a convert a java.util.Date to a Javascript Date? Do you mean some sort of textual representation that Javascript knows how to read? Commented Oct 6, 2011 at 13:10
  • I read the title before the question and thought, "Just test if the month is March, April or May... what could be simpler?" Commented Oct 6, 2011 at 13:14
  • @nicholas.hauschild Exactly, a usable textual representation. var array = new Array(${cust.dates}); just gives me a char array of the dates in a unusable format. Commented Oct 6, 2011 at 13:16

1 Answer 1

4

Spring executes at server-side, and JavaScript executes at client-side. For the point of view of Spring, JavaScript is just text that must be generated. And this text must represent valid JavaScript source code.

The JavaScript source code that creates an array of dates could thus be generated like this:

var dateArray = [];
<c:forEach var="javaDate" items="${cust.dates}">
    dateArray.push(new Date(${javaDate.time}));
</c:forEach>

This will generate the following JavaScript code:

var dateArray = [];
dateArray.push(new Date(65987985);
dateArray.push(new Date(98654654);
// ...

with the numeric arguments being the number of milliseconds since the epoch, which is the same in Java and JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.