我收到一个奇怪的错误。 我创建了一个简单的JSF复合组件,即:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<cc:interface>
<cc:attribute name="fieldId" required="true" />
<cc:attribute name="targetValue" type="java.sql.Date" required="true" />
<cc:attribute name="required" default="false" />
<cc:attribute name="disabled" default="false" />
<cc:attribute name="styleClass" />
</cc:interface>
<cc:implementation>
<p:calendar id="#{cc.attrs.fieldId}" disabled="#{cc.attrs.disabled}" required="#{cc.attrs.required}" locale="pt" navigator="true" pattern="dd/MM/yyyy" showOn="button" value="#{cc.attrs.targetValue}" styleClass="#{cc.attrs.styleClass}">
<f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy"/>
<f:ajax event="blur" execute="@this" render="@this" />
</p:calendar>
</cc:implementation>
</html>
我在表单上使用它,通常是:
<p:outputLabel for="inputInicial:dataInicial" value="#{msg['entity.dataInicial']}" />
<po:inputData id="inputInicial" targetValue="#{acaoController.entity.dataInicial}" fieldId="dataInicial"/>
<p:outputLabel for="inputFinal:dataFinal" value="#{msg['entity.dataFinal']}" />
<po:inputData id="inputFinal" targetValue="#{acaoController.entity.dataFinal}" fieldId="dataFinal" />
而且,当我点击添加新实体时它工作正常...我点击保存,它会按预期保存。
当我点击编辑现有实体,然后点击“保存”按钮时......会出现以下错误:
SEVERE: Servlet.service() for servlet [facesServlet] in context with path [/ProjetoOlimpio] threw exception [javax.el.ELException: /resources/olimpio/inputData.xhtml @18,238 value="#{cc.attrs.targetValue}": Cannot convert 30/12/12 21:00 of type class java.util.Date to class java.sql.Date] with root cause
javax.el.ELException: /resources/olimpio/inputData.xhtml @18,238 value="#{cc.attrs.targetValue}": Cannot convert 30/12/12 21:00 of type class java.util.Date to class java.sql.Date
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
at javax.faces.component.UIInput.updateModel(UIInput.java:818)
at javax.faces.component.UIInput.processUpdates(UIInput.java:735)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at org.primefaces.component.layout.Layout.processUpdates(Layout.java:252)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1231)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我的模型当然使用java.util.Date类。
import java.util.Date;
[...]
@Column(name = "data_inicial")
@Temporal(TemporalType.DATE)
private Date dataInicial;
@Column(name = "data_final")
@Temporal(TemporalType.DATE)
private Date dataFinal;
我正在使用Spring Data持续存在! 很难理解......
为什么它可以保存(不是更新)? 为什么在单击编辑实体后它在视图上正确显示所选日期?
这里有一些关于我的环境的信息: 信息:启动Servlet引擎:Apache Tomcat / 7.0.12 信息:初始化Mojarra 2.1.10 信息:在PrimeFaces 3.4.2上运行 信息:在PrimeFaces Extensions 0.6.1上运行
**编辑
1 - 正如我在评论中所说:问题在于复合组件,如果我将p:calendar直接放在它工作的表单上。所以我会编辑CC的代码片段到整个事情! :)
请注意,我有 type =“java.sql.Date”这是因为我正在测试而添加的。最初它没有类型属性。我还测试了 type =“java.util.Date 。
答案 0 :(得分:2)
没有更改hibernate模型的解决方法。 我更喜欢这种方式,因为所有更改都在jsf层中。
您可以在复合组件中使用绑定。 下一个代码是rich:calendar(使用java.util.Date)
的示例
...
<cc:interface componentType="CalendarComponent">
...
</cc:interface>
<cc:implementation>
...
<rich:calendar value="#{cc.attrs.value}" binding="#{cc.attrs.calendar}" />
...
</cc:implementation>
...
and in CalendarComponent:
import java.util.Date;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import org.richfaces.component.UICalendar;
@FacesComponent(value = "CalendarComponent")
public class CalendarComponent extends UINamingContainer {
@Override
public void processUpdates(FacesContext context) {
Object o = calendar.getValue();
if (o instanceof Date) {
Date d = (Date) o;
//this ensures type changing
calendar.setValue(new java.sql.Date(d.getTime()));
}
super.processUpdates(context);
}
private UICalendar calendar;
public UICalendar getCalendar() {
return calendar;
}
public void setCalendar(UICalendar calendar) {
this.calendar = calendar;
}
import java.util.Date;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import org.richfaces.component.UICalendar;
@FacesComponent(value = "CalendarComponent")
public class CalendarComponent extends UINamingContainer {
@Override
public void processUpdates(FacesContext context) {
Object o = calendar.getValue();
if (o instanceof Date) {
Date d = (Date) o;
//this ensures type changing
calendar.setValue(new java.sql.Date(d.getTime()));
}
super.processUpdates(context);
}
private UICalendar calendar;
public UICalendar getCalendar() {
return calendar;
}
public void setCalendar(UICalendar calendar) {
this.calendar = calendar;
}