我們現在開始在現有的JSF 2.2項目中使用JSF 2.3。在我們定製的轉換,我們得到了警告Converter is a raw type. References to generic type Converter<T> should be parameterized.
問題,我們遇到的,當我們試圖使用泛型來解決這一警告:JSF 2.3使用泛型的自定義轉換器
@FacesConverter(value = "myConverter", managed = true)
public class MyConverter implements Converter<MyCustomObject>{
@Override
public MyCustomObject getAsObject(FacesContext context, UIComponent component, String submittedValue){}
@Override
public String getAsString(FacesContext context, UIComponent component, MyCustomObject modelValue) {}
}
,當轉換器用於例如
<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:selectOneMenu id="#{componentId}" value="#{componentValue}">
<f:converter converterId="myConverter" />
<f:selectItem itemLabel="label"
itemValue="" />
<f:selectItems value="listOfValues"
var="singleValue"
itemValue="singleValue.value"
itemLabel="singleValue.label" />
</h:selectOneMenu>
然後ClassCastException
用消息java.lang.String cannot be cast to MyCustomObject
被拋出。 stacktrace中還有一行可能可以幫助com.sun.faces.cdi.CdiConverter.getAsString(CdiConverter.java:109)
。
但是,當轉換器仿製藥的定義從MyCustomObject
變更爲Object
:
@FacesConverter(value = "myConverter", managed = true)
public class MyConverter implements Converter<Object>{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue){}
@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {}
}
然後一切正常,但顯然擊敗Converter<T>
接口的目的。
任何想法?也許@ arjan-tijms? –
Btw:'