简体   繁体   English

JSP / JSTL中的嵌套表达式

[英]Nested expression in JSP/JSTL

I am using JSPs for the view, and Spring MVC 3.0 for the controller. 我在视图中使用JSP,为控制器使用Spring MVC 3.0。 In my JSP, I want to show the current DateTime, for which I have the following code... 在我的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" />

Now, the problem is JSTL fails to recognize my nested tag for SimpleDateFormat instantiation. 现在,问题是JSTL无法识别我的SimpleDateFormat实例化的嵌套标记。 I wish to pass the format string (As obtained from the 'dateTimeDisplayFormat' variable) to the SimpleDateFormat constructor. 我希望将格式字符串(从'dateTimeDisplayFormat'变量获得)传递给SimpleDateFormat构造函数。

Can someone please advice how do I write the nested constructor for SimpleDateFormat in the c:set statement above? 有人可以建议我如何在上面的c:set语句中编写SimpleDateFormat的嵌套构造函数?

Thanks in anticipation! 在期待中感谢!

<c:set> can take its value from the tag content, instead of from the value attribute: <c:set>可以从标记内容中取值,而不是从value属性中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>    

Better yet, you shouldn't need <c:set> at all, since both <spring:message> and <fmt:formatDate> can store their results in variables for you: 更好的是,您根本不需要<c:set> ,因为<spring:message><fmt:formatDate>都可以将结果存储在变量中:

<spring:message code="display.dateFormat" var="dateTimeDisplayFormat"/>
<fmt:formatDate pattern="${dateTimeDisplayFormat}" var="currentDateTime" scope="page"/>

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

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