【问题标题】:Custom Calendar Converter not picking up calendar attributes自定义日历转换器未获取日历属性
【发布时间】: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验证吗?

标签: java jsf richfaces


【解决方案1】:

您可以通过在 web.xml 文件中添加 &lt;error-page&gt; 来处理异常。

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errorpage.html</location>
</error-page>

这将显示任何异常实例的errorpage.html。

也可以从此链接获得更多信息。 http://docs.oracle.com/cd/B32110_01/web.1013/b25947/web_val.htm

希望对你有帮助

【讨论】:

  • 我有一个小小的建议是,不要通过调用 super.getAsObject() 直接从方法返回,而是验证日期的格式。此外,您可以在 try catch 中包含所有代码,如果没有例外,然后返回日期,否则抛出你自己的错误信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多