“验证错误:值无效”来自f:datetimeConverter的错误 [英] "Validation Error: Value is not valid" error from f:datetimeConverter

查看:279
本文介绍了“验证错误:值无效”来自f:datetimeConverter的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码创建了一个两个单选按钮。每个选项都包含成功转换为格式为yyyy-MM-dd的标签的日期值。一旦我进行选择并单击下一个按钮,我得到以下错误j_idt12:comDateChoice:验证错误:值无效。看起来很简单,但有些错误。你能发现吗?

The following code creates a two radio buttons. Each option contains a date value that is successfully converted to a label of the format "yyyy-MM-dd". Once I make a selection and click the next button I get the following error "j_idt12:comDateChoice: Validation Error: Value is not valid". It seems simple enough but somethings wrong. Can any of you spot it?

我在glassfish中使用JSF 2.0。

I'm using JSF 2.0 in glassfish.

Backing bean

Backing bean

public List<SelectItem> getComDateList() {
    List<SelectItem> items = new ArrayList<SelectItem>();
    Calendar cal = GregorianCalendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.add(Calendar.MONTH, 1);
    Date nextFirst = cal.getTime();
    cal.add(Calendar.MONTH, 1);
    Date followingFirst = cal.getTime();
    items.add(new SelectItem(nextFirst, new SimpleDateFormat("yyyy-MM-dd").format(nextFirst)));
    items.add(new SelectItem(followingFirst, new SimpleDateFormat("yyyy-MM-dd").format(followingFirst)));
    return items;
}

JSF代码

<h:panelGrid columns="2">
                    <h:outputLabel value="#{msg.FinanceCommencementDate}" for="comDateChoice"/>
                    <h:selectOneRadio id="comDateChoice" value="#{signUpBean.current.commencementDate}" layout="pageDirection">
                        <f:convertDateTime type="date" dateStyle="short"/>
                        <f:selectItems  value="#{signUpBean.comDateList}"/>
                    </h:selectOneRadio>
                </h:panelGrid>


推荐答案

如果所选项目值不为'通过 Object#equals()检查任何可用的选择项目值。如果getter在表单提交请求的应用请求值阶段返回了不同于在初始请求期间显示该表单的消息,则可能会发生这种情况。

This error will occur if the selected item value didn't pass the Object#equals() check on any of the available select item values. This can happen if the getter returned a different list during the apply request values phase of the form submit request than it did during the initial request to display the form.

因为你在getter中重建列表,而不是在视图范围bean的构造函数中构造一次, Date 对象将在每次调用时获得不同的时间戳,这将是一些与最初的 Date 对象相比,在未来的分钟/秒。因此, equals()将失败。

Because you're reconstructing the list in the getter instead of constructing once in the constructor of a view scoped bean, the Date objects will get a different timestamp on every call, it will be some minutes/seconds in the future as compared to the initial Date objects. Hence the equals() will fail.

将此逻辑移动到bean的构造函数中,并重写getter它执行应该做什么:仅返回数据。不要在吸气剂中加载逻辑。您还应该将bean放在视图范围内,以便在提交表单时构造函数不会重新运行。

Move this logic into the constructor of the bean and rewrite the getter so that it does what it is supposed to do: return only the data. Do not do loading logic in a getter. You should also put the bean in the view scope so that the constructor doesn't re-run when you submit the form.

@ManagedBean
@ViewScoped
public class SignUpBean {

    private List<SelectItem> comDateList;

    public SignUpBean() {
        comDateList = new ArrayList<SelectItem>();
        // Fill it here.
    }

    public List<SelectItem> getComDateList() {
        return comDateList; // In getters, do nothing else than returning data!
    }

}






更新:转换器也是问题的潜在来源。您基本上已经指示它脱离渲染HTML页面的时间。所以它使用转换回 Date 的默认时间。使用


Update: the converter is also a potential source of the problem. You've basically instructed it to strip off the time when rendering the HTML page. So it uses the default time when converting back to Date. Either use

<f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss.SSS Z" />

或重置时间和时区在日历事先:

or reset the time and timezone on the Calendar beforehand:

cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.setTimeZone(TimeZone.getTimeZone("GMT"));

这样可以使用一个< f:convertDateTime type =日期/>

这篇关于“验证错误:值无效”来自f:datetimeConverter的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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