How to solve “Conversion Error setting value '2013-10-26' for 'null Converter'” in h:inputText with Date value?

天涯浪子 提交于 2019-12-01 21:54:25
Luiggi Mendoza

There are two problems in your current approach:

  1. You should use java.util.Date instead java.sql.Date. JSF and other frameworks work with this type. Also, java.sql.Date extends java.util.Date but its purpose is basically for JDBC usage. More info about this: Date vs TimeStamp vs calendar?

  2. <h:inputText> expects a String as value, and when sending the data to the managed bean, it also expects the class field is from String type as well. In cases like this, you need to use a converter to tell JSF that this String in fact represents a Date. For this, you may use <f:convertDateTime> tag component.

    <h:inputText id="date" required="true" requiredMessage="Campo Obligatorio"
        value="#{aaaNewDetalles.criterioAaa.plazo}">
        <f:convertDateTime pattern="yyyy-MM-dd" />
    </h:inputText>
    

As a recommendation, you may use a calendar component from third party libraries like PrimeFaces or RichFaces whose provide <p:calendar> and <rich:calendar> component respectively.

You should import the appropriate Date package :

import java.util.Date;

In Managed Bean You should use java.util.Date. and specify the converter. e.g.

<h:inputText id="date" required="true" requiredMessage="Hire Date"
value="#{empBean.empDetail.hireDate}">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>

you can specify the pattern for DateTimeConverter also you can use dateStyle,timeStyle, type.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!