我们现在开始在现有的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:'