【发布时间】:2012-04-16 09:56:29
【问题描述】:
我有以下 JSF:
<rich:calendar id="startDate"
value="#{myBean.startDate}" defaultTime="00:00"
disabled="#{myBean.disabled}"
popup="true" required="true"
showWeeksBar="false"
dayDisableFunction="isDayEnabled"
dayClassFunction="getDisabledStyle"
requiredMessage="#{msg.errorStartDateNull}"
datePattern="#{myBean.datePattern}"
showApplyButton="false" cellWidth="24px" cellHeight="22px" todayControlMode="hidden"
style="width:1000px"
timeZone="#{myBean.defaultTimeZone}"
styleClass="noToday" showFooter="false" converter="calendarConverter" immediate="true">
这是我的转换器:
@FacesConverter("calendarConverter")
public class CalendarConverter extends DateTimeConverter
{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
Date calDate = (Date) component.getAttributes().get("currentDate");
if (calDate instanceof Date) {
return super.getAsObject(context, component, value);
}
else {
throw new ConverterException(new FacesMessage("Invalid Date Format"));
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
return super.getAsString(context, component, value);
}
}
我需要检查日历上的 currentDate 属性,但是当我将 immediate="true" 属性应用于我的日历组件时它为空。如果我删除了即时属性,我的转换器永远不会被调用(JSF 似乎正在使用它自己的转换)并将它自己的错误消息发送到屏幕:
java.lang.NumberFormatException: For input string: "2012asd"
当 currentDate 属性不是日期时,如何返回自定义错误消息? (已被篡改日期firefox插件篡改)
更新
我为转换器提供了一个构造函数,以查看它在提交表单时是否进入(删除了 immediate=true)。它正在进入构造函数,但仍然没有调用我的 getAsObject 方法。
public CalendarConverter()
{
super();
}
谢谢
【问题讨论】:
-
您是否尝试通过将
放入您的 rich:calendar 来替换转换器属性? -
我有丹尼尔没有运气,它仍然显示 java.lang.NumberFormatException: For input string: "2012asd" 并忽略我的转换器
-
你看过这个community.jboss.org/message/57921他们谈到了richfaces的特定版本......
-
恐怕还是没用。我使用的是 Richfaces 4.0,所以无论如何都应该修复它。
-
你试过通过javascript验证吗?