开发者

How to properly create a JavaScript Array of Dates in Spring?

开发者 https://www.devze.com 2023-04-11 08:57 出处:网络
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}

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消