JSP / JSTL中的嵌套表达式 [英] Nested expression in JSP/JSTL

查看:222
本文介绍了JSP / JSTL中的嵌套表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSP作为视图,而Spring MVC 3.0则用于控制器。
在我的JSP中,我想显示当前的DateTime,我有以下代码......

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...

<c:set var="dateTimeDisplayFormat" value='<spring:message code="display.dateFormat" />'/>

<c:set var="currentDateTime" 
    value='<%= new SimpleDateFormat(${dateTimeDisplayFormat}).format(new Date()) %>' 
    scope="page" />

现在,问题是JSTL无法识别SimpleDateFormat实例化的嵌套标记。我希望将格式字符串(从'dateTimeDisplayFormat'变量获得)传递给SimpleDateFormat构造函数。

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.

有人可以建议我如何编写SimpleDateFormat的嵌套构造函数在上面的c:set语句中?

Can someone please advice how do I write the nested constructor for SimpleDateFormat in the c:set statement above?

感谢您的期待!

推荐答案

< c:set> 可以从标记内容中取值,而不是从属性中取值:

<c:set> can take its value from the tag content, instead of from the value attribute:

<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>    

更好的是,您不需要< c:set> ,因为< spring:message> < fmt:formatDate> 可以将结果存储在变量中:

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:

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

这篇关于JSP / JSTL中的嵌套表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆