我在视图中使用JSP,为控制器使用Spring MVC 3.0.在我的JSP中,我想显示当前的DateTime,我有以下代码...
<c:set var="dateTimeDisplayFormat" value='<spring:message code="display.dateFormat" />'/>
<c:set var="currentDateTime"
value='<%= new SimpleDateFormat(${dateTimeDisplayFormat}).format(new Date()) %>'
scope="page" />
Run Code Online (Sandbox Code Playgroud)
现在,问题是JSTL无法识别我的SimpleDateFormat实例化的嵌套标记.我希望将格式字符串(从'dateTimeDisplayFormat'变量获得)传递给SimpleDateFormat构造函数.
有人可以建议我如何在上面的c:set语句中编写SimpleDateFormat的嵌套构造函数?
在期待中感谢!
ska*_*man 16
<c:set>可以从标记内容中获取其值,而不是从value属性中获取:
<c:set var="dateTimeDisplayFormat">
<spring:message code="display.dateFormat" />
</c:set>
<c:set var="currentDateTime" scope="page">
<%= new SimpleDateFormat(${dateTimeDisplayFormat}).format(new Date()) %>
</c:set>
Run Code Online (Sandbox Code Playgroud)
更重要的是,你不需要<c:set>在所有的,因为两者<spring:message>并<fmt:formatDate>可以存储在您的变量及其结果:
<spring:message code="display.dateFormat" var="dateTimeDisplayFormat"/>
<fmt:formatDate pattern="${dateTimeDisplayFormat}" var="currentDateTime" scope="page"/>
Run Code Online (Sandbox Code Playgroud)