如何将模型属性传递给JSP中的javascript函数,该函数本身返回一个值

时间:2017-07-25 12:24:01

标签: javascript java html jsp spring-mvc

我必须在jsp页面本身格式化我从控制器[Spring]收到的日期。
这是JSP文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<!DOCTYPE html>
<html lang="en">

<head>

<script
src="${pageContext.request.contextPath}/static/js/custom/customer.js">/script>
</head>
<body>

                <table id="success-story">
                    <thead>
                        <tr>
                            <th width="5%">Sl#</th>
                            <th width="50%">Description</th>
                            <th width="10%">Status</th>
                            <th width="30%">Created on</th>
                            <th width="5%">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach items="${customer.rfqs}" var="v" varStatus="count">
                            <tr>
                                <td>${count.count}</td>
                                <td>${v.description}</td>
                                <td>${v.status}</td>
                                <td>$v.createdTs</td>
                                <td align="center">
                                   <a href="#" onclick="view(${v.requestId})"> view</a>
                                   <a href="#" onclick="edit(${v.requestId})">edit</a>
                                </td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>

  </body>
 </html>'

$ {v.createdTS}返回YYYY形式的TimeStamp:MM:DD HH:MM:SS
现在我必须将此时间戳转换为这种格式DD:MM:YYYY
但是只有在填充行时才能完成它,怎么做呢?

我尝试了很多可能的解决方案,但没有一个解决方案,这里是我在JS文件中的功能

customer.js:

function formatDate(date) {
  var monthNames = [
    "Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul",
    "Aug", "Sep", "Oct",
    "Nov", "Dec"
  ];

  var day = date.getDate();
  var monthIndex = date.getMonth();
  var year = date.getFullYear();

  return day + ' ' + monthNames[monthIndex] + ' ' + year;
}

我可以以某种方式使用此功能,或任何其他替代方案对我有用。我只需要转换。
提前谢谢

1 个答案:

答案 0 :(得分:0)

如果createdTs属性是日期对象 - 您可以使用fmt tag

<fmt:formatDate pattern = "yyyy-MM-dd" value = "${v.createdTs}" />

如果是String类型,您可以在服务器端进行格式化。