t0mcat
t0mcat

Reputation: 5679

JSTL format tag

Trying to populate and formate a date value inside an INPUT text field. What am I doing wrong here?

<spring:bind path="salesData.weekEndDate">
 <input type="text" name="${status.expression}"
 value="${fmt:formateDate pattern='MM/mm/YYYY' status.value}"
/>

Upvotes: 5

Views: 15041

Answers (1)

BalusC
BalusC

Reputation: 1109875

The JSTL fmt taglib exists of <fmt:xxx> tags, not ${fmt:xxx} functions.

Fix it accordingly:

<input type="text" name="${status.expression}"
    value="<fmt:formatDate pattern="MM/dd/yyyy" value="${status.value}" />" />
/>

(note that days are to be represented as dd, not mm and that years are to be represented as yyyy, not YYYY, see also SimpleDateFormat javadoc for all valid patterns)

If your IDE jerks about the nested tags (which should run perfectly fine however) or you get itch from it, make use of the var attribute so that your HTML/XML ends up well formed.

<fmt:formatDate pattern="MM/dd/yyyy" value="${status.value}" var="statusDate" />
<input type="text" name="${status.expression}" value="${statusDate}" />

If you really like to have a ${fmt:formatDate()} function, you'd have to homegrow it yourself. You can find a kickoff example in this answer.


Update as turns out per comments, the ${status.value} is actually a String in the format yyyy-MM-dd. If fixing it to be a fullworthy Date is not an option, then you would need to parse it into a Date first with help of <fmt:parseDate> before feeding it to <fmt:formatDate>.

<fmt:parseDate pattern="yyyy-MM-dd" value="${status.value}" var="parsedStatusDate" />
<fmt:formatDate pattern="MM/dd/yyyy" value="${parsedStatusDate}" var="formattedStatusDate" />
<input type="text" name="${status.expression}" value="${formattedStatusDate}" />

Upvotes: 13

Related Questions