JSF:<f:convertDateTime type="date" pattern="mm/DD/yyyy"/>
Posted
技术标签:
【中文标题】JSF:<f:convertDateTime type="date" pattern="mm/DD/yyyy"/>【英文标题】:JSF: <f:convertDateTime type="date" pattern="mm/DD/yyyy"/> 【发布时间】:2013-02-20 19:06:19 【问题描述】:我有一个使用 convertDateTime 的 JSF 日期组件,它接受“12/12/2013ab”
backing bean 返回“12/12/2013”作为日期
我可以知道如何防止用户输入“12/12/2013ab”。它将提示 12/1a/2013 的错误。
【问题讨论】:
请显示您正在使用的组件。普通 JSF 不提供日期组件。 你在使用任何日历组件吗? 如果应用程序在开始和结束时自动修剪坏字符,会不会有问题?否则我建议实现一个 JSF 验证器。这里还有另一个像你一样的问题:***.com/questions/3194817/… 【参考方案1】:提供一个自定义日期转换器,它还检查输入长度。
@FacesConverter("myDateTimeConverter")
public class MyDateTimeConverter extends DateTimeConverter
public MyDateTimeConverter()
setPattern("MM/dd/yyyy");
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
if (value != null && value.length() != getPattern().length())
throw new ConverterException("Invalid format");
return super.getAsObject(context, component, value);
(请注意pattern 是MM/dd/yyyy
而不是mm/DD/yyyy
)
然后,而不是
<h:inputText value="#bean.date">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:inputText>
使用
<h:inputText value="#bean.date" converter="myDateTimeConverter" />
【讨论】:
以上是关于JSF:<f:convertDateTime type="date" pattern="mm/DD/yyyy"/>的主要内容,如果未能解决你的问题,请参考以下文章
如果 bean 提供时区,f:convertDateTime 将显示错误的日期
f:convertDateTime 显示错误的日期 [重复]