2017-06-12 4 views
0

Ich erstelle ein Formular mit JSF/Primefaces, damit ein Benutzer Daten eingeben kann. Ich habe eine generische Klasse Input definiert, um den Zugriff auf die Daten zu ermöglichen (da verschiedene Felder unterschiedliche Typen haben). Allerdings, wenn ich Daten von einem Objekt als Input<Double> erklärt versuchen und abrufen bekomme ich die Ausnahme java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Doublejava.lang.String kann nicht in java.lang.Double umgewandelt werden, wenn Generics verwendet werden

Ich bin ein wenig verwirrt, warum dies geschieht als in Input gespeicherten Wert sollte ein Doppel sein? Wenn mir jemand in die richtige Richtung zeigen könnte, was das verursacht, wäre ich sehr dankbar.

FormBean.java

@ManagedBean(name="form", eager=true) 
@SessionScoped 
public class FormBean { 

    private Input<String> name = new Input<>(); 
    private Input<Date> date = new Input<>(); 
    private Input<Double> hours = new Input<>(); 
    private Input<Double> amount = new Input<>(); 

    public void submit(){ 
     System.out.println(name.getValue()); 
     System.out.println(parseDate(date.getValue(),"dd/MM/yy")); 
     System.out.println(formatDouble(hours.getValue())); 
     System.out.println(formatDouble(amount.getValue()));  
    } 

    public static String parseDate(Date date, String format){ 
     if(date == null) return ""; 
     DateFormat df = new SimpleDateFormat(format); 
     return df.format(date); 
    } 

    public static String formatDouble(Double d){ 
     if(d==null) return ""; 
     else return String.format("%.2f", (double)d); 
    } 

    //-------------------GETTERS AND SETTERS---------------------------- 

    public Input<String> getName() { 
     return name; 
    } 
    public Input<Date> getDate() { 
     return date; 
    } 
    public Input<Double> getHours() { 
     return hours; 
    } 
    public Input<Double> getAmount() { 
     return amount; 
    } 
} 

Input.java

public class Input<T> { 

    private T value; 
    private String styleClass = "inputdefault"; 

    public T getValue() { 
     return value; 
    } 
    public void setValue(T value) { 
     this.value = value; 
    } 
    public String getStyleClass() { 
     return styleClass; 
    } 
    public void setStyleClass(String styleClass) { 
     this.styleClass = styleClass; 
    } 
} 

form.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 

    <h:head> 
     <ui:define name="title">Form</ui:define> 
    </h:head> 

    <h:body>  
      <h:form id="form"> 
       <p:panelGrid id="formGrid" style="text-align:center;margin:20px;" columns="2" layout="grid"> 
        <h:outputText class="output" value="Name"/> 
        <p:inputText id="name" class="#{form.name.styleClass}" value="#{form.name.value}"/> 

        <h:outputText class="output" value="Date:"/> 
        <p:calendar class="#{form.date.styleClass}" value="#{form.date.value}" pattern="dd/MM/yy"/> 

        <h:outputText class="output" value="Hours:"/> 
        <p:inputNumber id="hours" class="#{form.hours.styleClass}" value="#{form.hours.value}" decimalPlaces="2"/> 

        <h:outputText class="output" value="Amount:"/> 
        <p:inputNumber class="#{form.amount.styleClass}" value="#{form.amount.value}" decimalPlaces="2"/> 
       </p:panelGrid> 

       <p:commandButton value="Submit" update="@form" action="#{form.submit}" class="left"/> 
      </h:form> 
    </h:body> 

</html> 

Der Fehler tritt bei der Linie 13 in den submit() Verfahren in FormBean.java: System.out.println(formatDouble(amount.getValue()));

Wenn Sie weitere Informationen benötigen, lassen Sie es mich wissen. Danke!

EDIT: Added Stacktrace:

Jun 12, 2017 1:10:55 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute 
WARNING: #{form.submit}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
javax.faces.FacesException: #{form.submit}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: javax.faces.el.EvaluationException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    ... 26 more 
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at FormBean.submit(FormBean.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
    ... 27 more 

Jun 12, 2017 1:10:55 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError 
SEVERE: javax.faces.el.EvaluationException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double 
    at FormBean.submit(FormBean.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
    ... 27 more 
+0

zu erhalten In welcher Zeile tritt der Fehler auf? –

+0

Der Fehler tritt bei der Zeile 13 in der Methode submit() in FormBean.java auf: 'System.out.println (formatDouble (amount.getValue()));' –

+0

welche Ausgabe erhalten Sie, wenn Sie die doppelte Besetzung entfernen 'else return String.format ("%. 2f ", (double) d);'? –

Antwort

0

ich glaube, das Problem ist nicht, dass peaces Code gefunden in werden kann. An dem Punkt, an dem Sie die Ausnahme erwarten, greift die Bean das erste Mal auf das Bean-Attribut zu. JSF versucht, Input.setValue (Double double) aufzurufen, aber der Wert, der aus Ihrem jsf-Steuerelement stammt, ist eine Zeichenfolge. Also ich denke hier wird die Ausnahme geworfen.

Um sicher zu sein PLZ PLZ die Stacktrace der Ausnahme.

Die Lösung für dieses Problem wäre, einen Wertkonverter für Ihre Steuerung zu konfigurieren.

http://download.oracle.com/otn_hosted_doc/jdeveloper/j2ee101302/jsf_apps/eventvalidate/sf_avc_converters.html

Eine andere Lösung den Rohwert als String in Ihrer Eingangsklasse zu speichern wäre und in den Zieltyp konvertieren, wenn ein Aufruf der Methode den umgerechneten Wert (nicht die Getter-Methode)

+0

Ich habe den Stacktrace gepostet und es scheint, dass der Fehler nur in dieser Zeile auftritt, wenn die Methode 'submit()' versucht, 'amount.getValue()' an die 'formatDouble()' -Methode zu übergeben, die ein Double akzeptiert. Ich habe mir die PrimeFaces-Dokumentation angeschaut und '' gibt ein Double zurück. –

Verwandte Themen