Using JSP Scriplet along with JSON Model attributes

434 views Asked by At

I am getting an attribute/parameter from JSON response as EPOCH time variable. I want to convert into dd/MM/yyyy hh:mm:ss format and display in the table

<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>

But it's unable to compile the JSP. I couldn't find how to use combination of scriplet and model attribute value from JSON

Update Tried this - not working

<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>

Tried this - not working

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

Tried this - not working

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

There are 2 answers

4
Netorica On BEST ANSWER

you can use JSTL tags

<%@ 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

5
Michele Mariotti On
<%
    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())));
%>

wth! please, use this code to print classes and post the result so i can figure out what classes you are using:

<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>