JSTL格式标签 [英] JSTL format tag

查看:61
本文介绍了JSTL格式标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在INPUT文本字段内填充和设置日期值.我在这里做错了什么?

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

推荐答案

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

相应修复:

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

(请注意,日期应表示为dd,而不是mm,而年份应表示为yyyy,而不是YYYY,另请参见

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

如果您的IDE急于嵌套标记(不过应该运行得很好),或者您对此感到不满,请利用var属性,以使HTML/XML的格式正确.

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

如果您真的想拥有${fmt:formatDate()}功能,则必须自己自行开发.您可以在此答案<中找到启动示例/a>.

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.

更新(根据注释显示),${status.value}实际上是格式为yyyy-MM-ddString.如果不能将其固定为完全值得使用的Date,那么您需要先在<fmt:parseDate>的帮助下将其解析为Date,然后再将其提供给<fmt:formatDate>.

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

这篇关于JSTL格式标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆