简体   繁体   English

设置的“模式”属性 <fmt:formatDate> 用Javascript功能标记

[英]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. 我是Web编程的新手,所以这可能是一个愚蠢的问题。 Here's a snippet from a JSP page: 这是一个JSP页面的片段:

<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: 我想将fmt:formatDate标记的pattern属性设置为可以从JavaScript中的cookie检索的字符串,如下所示:

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. <fmt:formatDate>标记在HTML内容甚至到达您的浏览器之前<fmt:formatDate>处理并执行。

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. 如果在客户端具有在服务器上格式化数据之前需要的信息,则必须通过AJAX或类似方法进行操作。 Essentially you will have to send that date format from the cookie into your controller. 本质上,您将必须将日期格式从cookie发送到控制器。 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. 然后,您的控制器可以根据需要设置日期格式,或通过模型将日期传递到视图中,在这种情况下,您可以使用EL的<fmt:formatDate>标签访问日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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