Rich:由字符串支持的日历抛出异常 [英] Rich:Calendar backed by String throws Exception

查看:66
本文介绍了Rich:由字符串支持的日历抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下RichFaces(3.3.3):

I have the following 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变量类型为String,因为日历的出现列是根据用户配置文件动态计算的,并且可以是多种控件类型(Label,Input,SelectItem,Rich Calendar)之一.

The backbean Variable Type is String as the the column where the Calendar appears is calculated dynamically based on user profile and can be one of many type of controls (Label,Input,SelectItem,Rich Calendar)....

该日历在页面首次加载时起作用,并且会检索并显示RichCal1的正确值(在本例中为05/03/2012 12:00:00).

The calendar works the first time the page loads and the correct value for RichCal1 is retrived and shown (in this case 05/03/2012 12:00:00).

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

Problem occurs when the page needs to update (excuse the technically incorrect expression, i'll try and explain):

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

There is control on the page, where user can click and customize their current profile (additional rows or remove rows...etc). Once updated to reflect the change page needs to update which is where i see the following exception:

    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的,这是引起问题的原因.

I don't understand how the Date went from appearing as 05/03/2012 12:00:00 to 'Mon Mar 05 12:00:00 EST 2012 which is causing the issue.

有人可以启发我吗?

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

Further debugging i discovered that when user updates/create a profile in another window, upon completion the process Refreshes the parent window. I set breakpoints on the setter/Getter of the Rich:Calendar value and i can see that first Getter is called and it has the correctly formatted date value, then the Setter is called which is set to Mon Mar 05 12:00:00 EST 2012 after which the exception is thrown! Anyone knows why this is happening?

推荐答案

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

I resolved the above issue by writing a Converter as below:

    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修改为:

and modified the rich:calendar to:

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

希望能帮助处于类似职位的人.干杯

Hope that helps someone in similar position. Cheers

这篇关于Rich:由字符串支持的日历抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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