【问题标题】:Using JSP Scriplet along with JSON Model attributes将 JSP Scriplet 与 JSON 模型属性一起使用
【发布时间】:2013-12-11 08:44:09
【问题描述】:

我从 JSON 响应中获取一个属性/参数作为 EPOCH 时间变量。 我想转换成dd/MM/yyyy hh:mm:ss格式并显示在表格中

<tbody>
    <c:if
        test="${not empty jsonResult && not empty jsonResult.records}">
        <c:forEach items="${jsonResult.records}" var="record">
            <tr>
                <td style="width:15%;"><img src="${record.attributes.P_Image_Path}" class="img-responsive" /></td>
                <td style="width:15%;">${record.attributes.P_Description}</td>
                <td style="width:55%;">${record.attributes.P_Username_Seller}</td>
                <%
                    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
                    System.out.println(sdf.format( new java.util.Date(${record.attributes.P_Close_Time}));
                %>
                <td style="width:15%;">${record.attributes.P_Close_Time}</td>
            </tr>
        </c:forEach>
    </c:if>
</tbody>

但是它无法编译 JSP。我找不到如何使用 JSON 中的 scriplet 和模型属性值的组合

更新 试过这个 - 不工作

<c:set var="now" value="<%=new java.util.Date(${record.attributes.P_Close_Time}%>" />
<td style="width: 15%;"><fmt:formatDate pattern="dd/MM/yyyy hh:mm:ss" value="${now}" /></td>

试过了 - 不工作

<td style="width: 15%;"><fmt:formatDate pattern="dd/MM/yyyy hh:mm:ss" value="<%=new java.util.Date(${record.attributes.P_Close_Time})%>" /></td>

试过了 - 不工作

<td style="width: 15%;"><fmt:formatDate pattern="dd/MM/yyyy hh:mm:ss" value="<%=new java.util.Date(record.attributes.P_Close_Time)>" /></td>

【问题讨论】:

  • 你不能在${...}里面使用&lt;% ... %&gt;
  • @MicheleMariotti,可能是:) 请解决!

标签: java json jsp date scriptlet


【解决方案1】:

你可以使用 JSTL 标签

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>


<fmt:formatDate pattern="dd/MM/yyyy hh:mm:ss" value="${now}" />

http://www.tutorialspoint.com/jsp/jstl_format_formatdate_tag.htm

【讨论】:

  • 我遇到错误无法将 java.lang.String 类型的 1377820800 转换为 java.util.Date 类
  • 首先将record.attributes.P_Close_Time转换为DateTime,然后再将其传递给标签的value属性
  • 你漏掉了一个括号...和其他的东西,比如美元符号&lt;%=new java.util.Date(record.attributes.P_Close_Time)%&gt;
  • 如果你能发布完整的答案会更好。还是不行
【解决方案2】:
<%
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    Record record = (Record) pageContext.getAttribute("record");
    System.out.println(sdf.format(new Date(record.getAttributes().getP_Close_Time())));
%>

哇!请使用此代码打印课程并发布结果,以便我可以弄清楚您正在使用哪些课程:

<tbody>
    <c:if
        test="${not empty jsonResult && not empty jsonResult.records}">
        <c:forEach items="${jsonResult.records}" var="record">
            <tr>
                <td style="width:15%;"><img src="${record.attributes.P_Image_Path}" class="img-responsive" /></td>
                <td style="width:15%;">${record.attributes.P_Description}</td>
                <td style="width:55%;">${record.attributes.P_Username_Seller}</td>
                <%
                    //java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
                    //System.out.println(sdf.format( new java.util.Date(${record.attributes.P_Close_Time}));
                    Object record = pageContext.getAttribute("record");
                    System.out.println("page record: " + (record == null ? null : record.getClass().getName()));
                %>
                <td style="width:15%;">${record.attributes.P_Close_Time}</td>
            </tr>
        </c:forEach>
    </c:if>
</tbody>

【讨论】:

  • 抱歉没听懂您的回答。 Record 对象从何而来?
  • 来自pageContextrequest 隐式jsp 对象(我不记得是第一个还是后者)。 c:forEach 将当前迭代对象存储为 name = var 的属性。
  • 抱歉没有工作。它正在抛出未知类型的记录
  • Record 是你自己的班级...我不知道它的名字是什么...这只是一个例子!
  • 抱歉没有对象。我提到它是 JSON 响应。在 JSON 响应中,您没有对象。
猜你喜欢
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 2021-07-27
  • 2014-08-16
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多