Hello,
I stuck with strange JSF java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer, when I try to use abstract class with generic. My code:
ObjectClass.java
public abstract class ObjectClass<T> {
public abstract T getFieldValue();
public abstract void setFieldValue(T fieldValue);
}
IntClass.java
public class IntClass extends ObjectClass<Integer> {
private Integer fieldValue;
public Integer getFieldValue() {
return fieldValue;
}
public void setFieldValue(Integer fieldValue) {
this.fieldValue = fieldValue;
}
}
TestBean.java
@ManagedBean(name = "TestBean")
public class TestBean {
private IntClass intClass = new IntClass();
public IntClass getIntClass() {
return intClass;
}
}
test.xhtml
<h:form>
<h:outputLabel value="Integer value:" />
<h:inputText value="#{TestBean.intClass.fieldValue}" />
<h:commandButton type="submit" value="Set" />
</h:form>
When I try to enter numeric value into a field and submit, I get java.lang.ClassCastException error:
javax.faces.component.UpdateModelException: javax.el.ELException: /test.xhtml @16,68 value="#{TestBean.intClass.fieldValue}": java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at javax.faces.component.UIInput.updateModel(UIInput.java:839)
at javax.faces.component.UIInput.processUpdates(UIInput.java:722)
at javax.faces.component.UIForm.processUpdates(UIForm.java:270)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1108)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1108)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1239)
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:312)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.el.ELException: /test.xhtml @16,68 value="#{TestBean.intClass.fieldValue}": java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:132)
at javax.faces.component.UIInput.updateModel(UIInput.java:805)
... 33 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at test.IntClass.setFieldValue(IntClass.java:3)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.setValue(BeanELResolver.java:381)
at javax.el.CompositeELResolver.setValue(CompositeELResolver.java:386)
at com.sun.faces.el.FacesCompositeELResolver.setValue(FacesCompositeELResolver.java:100)
at com.sun.el.parser.AstValue.setValue(AstValue.java:197)
at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:286)
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:124)
... 34 more
When I remove [extends ObjectClass<Integer>] from class IntClass, or when I explicitly add IntegerConverter to inputText component, everything works fine. Can someone tell me, what's the problem here? Thanks.
vide[b][/b][b][/b][b][/b][u][/u][i][/i][b][/b][b][/b][b][/b]
[Message sent by forum member 'vide']
http://forums.java.net/jive/thread.jspa?messageID=398492