简体   繁体   中英

Setting the “pattern” attribute of <fmt:formatDate> tag with Javascript function

I'm brand new to web programming, so this is probably a dumb question. Here's a snippet from a JSP page:

<c:choose>
    <c:when test="${ empty model.toThemAssoc }">
        Not setup to send
    </c:when>
    <c:otherwise>
        Connected since <fmt:formatDate value="${ model.toThemAssoc.dateEntered }"/>
    </c:otherwise>
</c:choose>

I want to set the pattern attribute of the fmt:formatDate tag to a string I can retrieve from a cookie in JavaScript like this:

function getDateFormat {
    return $.cookies.get('dateFormat');
}

Although the below code doesn't work, it represents what I'm looking for:

<fmt:formatDate pattern="getDateFormat()" value="${ model.toThemAssoc.dateEntered }"/>

Any suggestions? Thanks in advance.

This will not work the way you expect. The <fmt:formatDate> tag is processed and executed before the HTML content even gets to your browser.

If you have information on the client-side that is required to format data from the server-side before it gets to the client-side, you will have to do it via AJAX or something similar. Essentially you will have to send that date format from the cookie into your controller. Your controller can then format the date as it sees fit, or pass it into the view via the model, in which case you can access it using EL for the <fmt:formatDate> tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM