Search code examples
spring-bootspring-mvcjspjstl

Take fmt:formatDate's pattern from Properties File


fmt:formatDate pattern does not allow anything but a string

This is the LOC i am referring to

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:useBean id="today" class="java.util.Date" />
<fmt:formatDate var="todayString" value="${today}" pattern="dd/MM/yyyy" />

I would like to pick the pattern from the properties file. Normally when I do spring:message inside same jsp file (I am using spring boot/jsp) I can see the message. So they are configured properly. Is there a possible way for me to format my date as well as fetch the pattern from the properties file. I have tried many ways but mostly JSP throws exception.

Change fmt:formatDate pattern dynamically does not answer my question


Solution

  • One way is:

    <fmt:message key="pattern.date" var="datePattern"/>
    <fmt:formatDate var="todayString" value="${today}" pattern="${datePattern}"/>
    

    Or:

    <fmt:formatDate var="todayString"
                    value="${today}"
                    pattern="<fmt:message key='pattern.date'/>"/>