Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

when I press the insert button, I get the error indicated on the title

Conversion Error setting value '2013-10-26' for 'null Converter'

<h:form id="formulario">
   <h:outputLabel for="date">Plazo</h:outputLabel>
   <h:inputText id="date" required="true" requiredMessage="Campo Obligatorio" value="#{aaaNewDetalles.criterioAaa.plazo}"/>
   <h:message for="date" style="color: red;"/>
   <h:commandButton actionListener="#{aaaNewDetalles.add()}" value="Ingresar"/>
</h:form>

the form is managed by this class:

@ManagedBean(name = "aaaNewDetalles")
@ViewScoped
public class aaaNewDetallesBean {
     private CriterioAaaController controller;
     private CriterioAaa criterioAaa;

    @PostConstruct
    public void init(){
          controller= new CriterioAaaController();
          criterioAaa= new CriterioAaa();
    }

    public void add(){
        controller.save(criterioAaa);
    }

    public CriterioAaa getCriterioAaa() {
        return criterioAaa;
    }

    public void setCriterioAaa(CriterioAaa criterioAaa) {
        this.criterioAaa = criterioAaa;
    }
}

The object CriterioAaa:

import java.sql.Date;

@Table(name = "criterio_aaa", schema = "", catalog = "ciclos_calidad")
@Entity
public class CriterioAaa extends Entidad implements Serializable {

    private Date plazo;

    public Date getPlazo() {
        return plazo;
    }

    public void setPlazo(Date plazo) {
        this.plazo = plazo;
    }

}
like image 291
Isma90 Avatar asked Dec 04 '25 14:12

Isma90


1 Answers

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.

like image 90
Luiggi Mendoza Avatar answered Dec 06 '25 06:12

Luiggi Mendoza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!