Rich:由String支持的日历抛出异常

时间:2012-03-05 05:12:30

标签: jsf richfaces

我有以下RichFaces(3.3.3):

    <rich:calendar id="richCal1"                                    
        value="#{user.CreateDate}"
        popup="true" mode="client"
        inputSize="20"
        datePattern="dd/M/yyyy HH:mm"
        enableManualInput="true"
        buttonIcon="/images/calendar.gif">
        <f:convertDateTime type="date" pattern="dd/MM/yyyy HH:mm" />
        <a4j:support bypassUpdates="true" event="oninputblur"  ajaxSingle="true" />
        <a4j:support bypassUpdates="true" event="onchanged" ajaxSingle="true" />
    </rich:calendar>

backbean Variable Type是String,因为Calendar出现的列是根据用户配置文件动态计算的,可以是多种控件之一(Label,Input,SelectItem,Rich Calendar)....

日历在第一次加载页面时起作用,并且会重新显示RichCal1的正确值(在本例中为05/03/2012 12:00:00)。

当页面需要更新时出现问题(请原谅技术上不正确的表达,我会尝试解释):

页面上有控件,用户可以在其中单击并自定义其当前配置文件(其他行或删除行...等)。一旦更新以反映更改页面需要更新,我在哪里看到以下异常:

    ERROR: org.ajax4jsf.webapp.BaseXMLFilter - Exception in the filter chain
    javax.servlet.ServletException: myForm:0:richCal1: 'Mon Mar 05 
    12:00:00 EST 2012' could not be understood as a date.

我不明白日期如何从05/03/2012 12:00:00显示到导致问题的'Mon Mar 05 12:00:00 EST 2012

有人可以赐教。

更新

进一步调试我发现当用户在另一个窗口中更新/创建配置文件时,完成后该过程刷新父窗口。我在Rich:Calendar的setter / Getter上设置了断点,我可以看到第一个Getter被调用并且它具有正确格式化的日期值,然后调用Setter,设置为Mon Mar 05 12:00:00 EST 2012之后的异常被抛出!谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

我通过编写Converter解决了上述问题,如下所示:

    public class CalDateStrConveter implements Converter {
        private String pattern = ApplicationConstant.DD_MM_YYYYHHMM;
                    //eg 02/02/2012 12:00  (note Rich:calendar has no support for seconds)

        public Object getAsObject(FacesContext context, UIComponent component, String value)
                throws ConverterException {

            String result = "";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            if(value!= null && value.length() > 0) {
                try {
                    Date date = sdf.parse(value);
                    result = sdf.format(date);
                } catch (Exception e) {
                    Date date = new Date();
                    logger.error(e.getMessage());
                    FacesMessage facesMessage = new FacesMessage("Invalid Date", value + " is an invalid date. Example " + sdf.format(date));
                    FacesContext.getCurrentInstance().addMessage("DATE PARSE ERROR", facesMessage);
                }
            }

            return result;
        }

        public String getAsString(FacesContext context, UIComponent component, Object value)
                throws ConverterException {

            String result = "";
            String valueStr = (String) value;
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            if (valueStr!= null && valueStr.length() > 0) {
                try {
                    Date date = sdf.parse(valueStr);
                    result = sdf.format(date);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    Date date = new Date();
                    FacesMessage facesMessage = new FacesMessage("Invalid Date", value + " is an invalid date. Example " + sdf.format(date));
                    FacesContext.getCurrentInstance().addMessage("DATE PARSE ERROR", facesMessage);
                }
            }
            return result;
        }

    }

faces-config.xml注册:

     <converter>
        <converter-id>CalDateStrConveter </converter-id>
        <converter-class>com.util.userProd.CalDateStrConveter</converter-class>
     </converter>

并将rich:calendar修改为:

    <rich:calendar id="richCal1"                                    
            value="#{user.CreateDate}"
            popup="true" mode="client"
            inputSize="20"
            datePattern="dd/M/yyyy HH:mm"
            enableManualInput="true"
            buttonIcon="/images/calendar.gif">
            <f:converter converterId="CalDateStrConveter "/>
            <a4j:support bypassUpdates="true" event="oninputblur"  ajaxSingle="true" />
            <a4j:support bypassUpdates="true" event="onchanged" ajaxSingle="true" />
    </rich:calendar>

希望能帮助处于类似位置的人。干杯