我有一个动态键和值的域模型对象,它是从一个映射派生的:
public class MyDomainModelObject extends HashMap<String, SomeClass<?>>
{
private String documentType;
public MyDomainModelObject()
{
super();
}
public String getDocumentType()
{
return documentType;
}
public void setDocumentType(final String documentType)
{
this.documentType = documentType;
}
}
获取和设置存储在映射中的值正在工作。但是成员documentType
的设置导致ClassCastException
。
documentType
在primefaces数据表单元格编辑器中编辑:
<p:dataTable ...>
<p:column ... />
...
</p:column>
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{curObj.getDocumentType()}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{curObj.documentType}">
<f:selectItems value="#{someBean.determineDocumentTypes(curObj)}" var="curDocType" itemLabel="#{curDocType}"
itemValue="#{curDocType}" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
当我改变表内的documentType
时,我得到以下异常:
10:08:09,581 ERROR [stderr] (http-/0.0.0.0:9090-3) java.lang.ClassCastException: java.lang.String cannot be cast to SomeObject
10:08:09,581 ERROR [stderr] (http-/0.0.0.0:9090-3) at MyDomainModel.put(MyDomainModel.java:1)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at javax.el.MapELResolver.setValue(MapELResolver.java:262)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at com.sun.faces.el.DemuxCompositeELResolver._setValue(DemuxCompositeELResolver.java:255)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at com.sun.faces.el.DemuxCompositeELResolver.setValue(DemuxCompositeELResolver.java:281)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at de.odysseus.el.tree.impl.ast.AstProperty.setValue(AstProperty.java:156)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at de.odysseus.el.tree.impl.ast.AstEval.setValue(AstEval.java:87)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at de.odysseus.el.TreeValueExpression.setValue(TreeValueExpression.java:146)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.jboss.weld.el.WeldValueExpression.setValue(WeldValueExpression.java:64)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3) at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at javax.faces.component.UIInput.updateModel(UIInput.java:832)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at javax.faces.component.UIInput.processUpdates(UIInput.java:749)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1290)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.primefaces.component.celleditor.CellEditor.processUpdates(CellEditor.java:89)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.primefaces.component.api.UIData.process(UIData.java:379)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.primefaces.component.api.UIData.processChildren(UIData.java:360)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.primefaces.component.api.UIData.processPhase(UIData.java:322)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.primefaces.component.api.UIData.processUpdates(UIData.java:308)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3) at org.primefaces.component.datatable.DataTable.processUpdates(DataTable.java:722)
10:08:09,584 ERROR [stderr] (http-/0.0.0.0:9090-3) at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:577)
10:08:09,584 ERROR [stderr] (http-/0.0.0.0:9090-3) at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
为什么要用javax.el.MapELResolver
来设置documentType
的值?不执行setDocumentType
方法,只执行getDocumentType
方法。我有点糊涂了
为什么用
javax.el.MapELResolver
来设置documentType
的值?
因为base是java.util.Map
的实例。基本上,EL检查如下:
if (curObj instanceof Map) {
// Use MapELResolver.
}
else (...) {
// ...
}
else {
// Use BeanELResolver.
}
你有两个选择:
用组合代替继承。
public class MyDomainModelObject { private String documentType; private Map<String, Foo> properties; // ... }
坚持继承并委托给它(hacky, raw类型等)。
public String getDocumentType() { return (String) ((Map) this).get("documentType"); } public void setDocumentType(final String documentType) { ((Map) this).put("documentType", documentType); }
注意EL不会直接使用它。它仍然会通过
Map#get()
和MapELResolver
抓取它。这些方法将仅由您的域代码使用。