我们现在开始在我们现有的 JSF 2.2 项目中使用 JSF 2.3。在我们的自定义转换器上,我们收到警告Converter is a raw type. References to generic type Converter<T> should be parameterized.
我们遇到的问题是当我们尝试使用泛型修复该警告时:
@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>
界面的目的。