Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested expression in JSP/JSTL

Tags:

java

jsp

jstl

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

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.

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

Thanks in anticipation!

like image 902
PaiS Avatar asked Jul 28 '10 08:07

PaiS


1 Answers

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

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"/>
like image 169
skaffman Avatar answered Oct 31 '22 20:10

skaffman