Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL format tag

Tags:

jsp

jstl

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

like image 241
t0mcat Avatar asked Jun 03 '11 18:06

t0mcat


1 Answers

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}" />
like image 61
BalusC Avatar answered Oct 18 '22 05:10

BalusC