开发者

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="te开发者_JAVA技巧xt" name="${status.expression}"
 value="${fmt:formateDate pattern='MM/mm/YYYY' status.value}"
/>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜