0

我收到一个奇怪的错误。我创建了一个简单的 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 扩展 0.6.1 上运行

**编辑

1 - 正如我在评论中所说:问题出在复合组件上,如果我将 p:calendar 直接放在它工作的表单上。所以我会把CC的代码片段编辑成整个东西!:)

请注意,我有一个type="java.sql.Date"因为我正在测试而被添加。最初它没有类型属性。我还使用type="java.util.Date进行了测试。

4

1 回答 1

2

有一种解决方法,无需更改休眠模式。我更喜欢这种方式,因为所有更改都在 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;
}

}

于 2013-11-01T08:36:02.850 回答