JSF Converter Error
Hi, I have the following:
JSF 1.2 RI
Sun App Server 9 Update 1 Patch 1
Models:
publicinterface Model{
int getId();
String getName();
}
publicclass Model1implements Model{
publicint getId(){
return 1;
}
public String getName(){
return"One";
}
}
publicclass Model2implements Model{
publicint getId(){
return 2;
}
public String getName(){
return"Two";
}
}
Controller:
publicclass TestController{
private Model model;
private List<Model> models;
public TestController(){
models =new ArrayList<Model>();
models.add(model =new Model1());
models.add(new Model2());
}
public Model getModel(){
return model;
}
publicvoid setModel(Model model){
this.model = model;
}
public List<SelectItem> getModels(){
List<SelectItem> list =new ArrayList<SelectItem>();
for (Model model : models)
list.add(new SelectItem(model, model.getName()));
return list;
}
public Model findModel(int id){
for (Model model : models){
if (model.getId() == id)
return model;
}
returnnull;
}
}
Converter:
publicclass ModelConverterimplements Converter{
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string){
TestController controller = (TestController) facesContext.getExternalContext().getSessionMap().get("testController");
return controller.findModel(Integer.parseInt(string));
}
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object){
return String.valueOf(((Model) object).getId());
}
}
View:
<%@ page contentType="text/html" %>
<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Test JSF</title>
</head>
<body>
<f:view>
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
<h1>Test JSF</h1>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Model:"/>
<h:selectOneMenu value="#{testController.model}">
<f:selectItems value="#{testController.models}"/>
</h:selectOneMenu>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
faces-config.xml:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<converter>
<converter-for-class>com.test.model.Model</converter-for-class>
<converter-class>com.test.converter.ModelConverter</converter-class>
</converter>
<managed-bean>
<managed-bean-name>testController</managed-bean-name>
<managed-bean-class>com.test.controller.TestController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
The problem:
When I run the app, JSF throws the following error:
javax.servlet.ServletException: Cannot convert com.test.model.Model2@ba30aa of typeclass com.test.model.Model2 toclass com.test.model.Model1
root cause
java.lang.IllegalArgumentException: Cannot convert com.test.model.Model2@ba30aa of typeclass com.test.model.Model2 toclass com.test.model.Model1
Is there any limitation on converters to prevent using a single Converter for a base class (or interface like Model) and subclasses (like Model1 and Model2)?
I've tried this using JSF 1.1 RI, Apache MyFaces 1.1 and the error is still there.
Thanks!

