Seam DataModel cannot be cast to ArrayList
I am new to SEAM trying out from a generated app some changes. Like implementing a data SelectOneMenu where I can show data from a table in a form.
I am getting the following exception
java.util.ArrayList cannot be cast to javax.faces.model.DataModel
It may be something very basic but I haven't found any good examples. Maybe someone can point one out to me. I am reading Seam in Action and Seam Framework from YUAN, havent found an example like the one I am trying.
This is my bean
@Name("FuncionesAcciones")
public class FuncionesAcciones {
@Logger
private Log log;
@In
StatusMessages statusMessages;
@In
protected EntityManager entityManager = null;
@DataModelSelection
@Out(required = false)
protected Usuarios selectedUser;
@DataModel
@Out(required = false)
protected List<Usuarios> listaUsers = null;
public String view() {
return "/AccionesEdit.xhtml";
}
@Factory("listaUsers")
public void listarUsuarios() {
List resultList = entityManager.createQuery(
"select idUsuarios from Usuarios")
.getResultList();
listaUsers = (List<Usuarios>) resultList;
// "select idUsuarios,NombreUsuario from Usuarios")
}
public void funcionesAcciones() {
// implement your business logic here
log.info("FuncionesAcciones.funcionesAcciones() action called");
statusMessages.add("funcionesAcciones");
}
// add additional action methods
}
This is the part where I use it in the Facelet
<s:decorate id="usuariosIdUsuariosField" value="#{FuncionesAcciones.selectedUser}" template="layout/edit.xhtml">
<ui:define name="label">Usuario que Identifica Accion</ui:define>
<h:selectOneMenu name="usuario" id="usuariosIdUsuarios" required="true" value="Usuarios.nombreUsuario">
<f:selectItems var="_usuario" value ="#{listaUsers}" label="#{_usuario.NombreUsuario}"/>
<s:convertEnum/>
</h:selectOneMenu>
</s:decorate>
Can someone point me in the right direction. Like I said开发者_如何学Go I am new to SEAM, and somewhat average experience in JAVA/Hibernate. I come from PHP so the learning curve is apparently very hard right now as a beginner.
When you have a UIData compenet such as HtmlDataTable you should use The following options
- An Array or List of beans
- A single bean
- java.sql.ResultSet
- javax.servlet.jsp.jstl.Result
- javax.faces.model.DataModel
But you have an HtmlSelectOneMenu which is not an UIData component. So you can not use it. If you want To render an HtmlSelectOneMenu, you should remove @DataModel annotation which is JSF DataModel wrapper. You can either
@Out(required=false)
private List<SelectItem> listaUsers;
/**
* void method with @Out-jection
*/
@Factory("listaUsers")
public void listarUsuarios() {
List<Usuario> resultList = entityManager.createQuery("from Usuarios").getResultList();
for(Usuario usuario: resultList)
listaUsers.add(new SelectItem(usuario.<VALUE_PROPERTY>, usuario.NombreUsuario));
}
Or
/**
* @Factory method itself returns List<SelectItem>
*/
@Factory("listaUsers")
public List<SelectItem> listarUsuarios() {
List<Usuario> resultList = entityManager.createQuery("from Usuarios").getResultList();
List<SelectItem> listaUsers = new ArrayList<SelectItem>();
for(Usuario usuario: resultList)
listaUsers.add(new SelectItem(usuario.<VALUE_PROPERTY>, usuario.NombreUsuario));
return listaUsers;
}
And use
<h:selectOneMenu name="usuario" id="usuariosIdUsuarios" required="true" value="#{Usuarios.nombreUsuario}">
<f:selectItems value="#{listaUsers}"/>
<s:convertEnum/>
</h:selectOneMenu>
Keep in mind you should use binding expression (Notice #{ and } following managed bean property)
Advice: Prefer To use @Factory with return value instead of @Out-jection. @Out-jection will be deprecated in favor of @Factory method
Im posting the answer for reference in the future.
Ok the code of the seam component ended like this and it worked. The final error was with passing the ID field to string on the ItemList.
Thanks again Arthur for the help your code was mostly spot on.
@Factory("listaUsers")
public List<SelectItem> listarUsuarios() {
List<Usuarios> resultList = entityManager.createQuery(
"from Usuarios")
.getResultList();
List<SelectItem> listaUsers = new ArrayList<SelectItem>();
for(Usuarios usuario: resultList)
listaUsers.add(new SelectItem(Integer.toString(usuario.getIdUsuarios()), usuario.getNombreUsuario()));
return
listaUsers;
}
This is the view part of the code for the Dropbox
<h:selectOneMenu name="usuarioIdent" id="usuariosIdUsuarios" required="true" value="#{accionesHome.instance.usuariosIdUsuarios}">
<f:selectItems value ="#{listaUsers}" />
</h:selectOneMenu>
精彩评论