如何解决“转换错误设置值”2013-10-26'for'null Converter'“在h:inputText与Date值? [英] 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
转换错误设置值'2013-10-26 'for'null转换器'
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>
表单由此类管理:
@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;
}
}
对象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;
}
}
推荐答案
您现在的方法有两个问题:
There are two problems in your current approach:
-
您应该使用
java.util。日期
而是java.sql.Date
。 JSF和其他框架可以使用这种类型。另外,java.sql.Date
extendsjava.util.Date
,但其目的基本上是用于JDBC使用。更多信息:日期vs时间戳vs日历?
You should use
java.util.Date
insteadjava.sql.Date
. JSF and other frameworks work with this type. Also,java.sql.Date
extendsjava.util.Date
but its purpose is basically for JDBC usage. More info about this: Date vs TimeStamp vs calendar?
< h:inputText>
期望一个 String
作为值,并将数据发送到托管bean ,它也期望类字段也来自 String
类型。在这种情况下,您需要使用转换器来告诉JSF,这个 String
实际上代表一个 Date
。为此,您可以使用< f:convertDateTime>
标签组件。
<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>
作为推荐,您可以使用日历来自第三方图书馆的组件,如PrimeFaces或RichFaces,其提供 < p:日历>
和 < rich:calendar>
组件。
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.
这篇关于如何解决“转换错误设置值”2013-10-26'for'null Converter'“在h:inputText与Date值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!