java JSP/JSTL 中的嵌套表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3351116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nested expression in JSP/JSTL
提问by PaiS
I am using JSPs for the view, and Spring MVC 3.0 for the controller. In my JSP, I want to show the current DateTime, for which I have the following code...
我使用 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" />
Now, the problem is JSTL fails to recognize my nested tag for SimpleDateFormat instantiation. I wish to pass the format string (As obtained from the 'dateTimeDisplayFormat' variable) to the SimpleDateFormat constructor.
现在,问题是 JSTL 无法识别 SimpleDateFormat 实例化的嵌套标记。我希望将格式字符串(从 '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!
感谢期待!
回答by skaffman
<c:set>can take its value from the tag content, instead of from the valueattribute:
<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>
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"/>

