tags:

views:

187

answers:

3

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 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.

+2  A: 

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

Arthur Ronald F D Garcia
Thanks a lot for your response Arthur your example and explanation makes a lot of sense now, i only have one problem with your code.On this line of the bean:for(Usuario usuario: resultList)I get and error Type mismatch: cannot convert from element type Object to Usuarios
demo
Im reading up that maybe i need a converter for this on my entity class, it seems to be getting too complicated for just a simple combobox is there a UIDATA combobox that is faster and simpler to accomplish what it want?
demo
@demo Sorry, Updated
Arthur Ronald F D Garcia
I fixed all the problem is working now Thanks
demo
@demo Post your solution as a additional answer To Stackoverflow users give you UP voting
Arthur Ronald F D Garcia
+1  A: 

Thanks for the response Arthut i fixed the code and now it compiles ok, sorry for all the questions but it seems im way out of my league on this since i cant make a simple thing like a combobox work,

This is the final version of the bean code:

@Name("FuncionesAcciones")
@Scope(CONVERSATION)
public class FuncionesAcciones {
    @Logger
    private Log log;

    @In
    StatusMessages statusMessages;

    @In
    protected EntityManager entityManager = null;

//  @DataModelSelection
//  @Out(required = false)
//  protected Usuarios selectedUser;




    public String view() {
        return "/AccionesEdit.xhtml";
    }

    @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(usuario.getIdUsuarios(), usuario.getNombreUsuario()));

        return listaUsers;
    }

    public void funcionesAcciones() {
        // implement your business logic here
        log.info("FuncionesAcciones.funcionesAcciones() action called");
        statusMessages.add("funcionesAcciones");
    }

    // add additional action methods

}

now i get the following runtime error occurs while loading the page:

java.lang.NullPointerException
    at com.sun.faces.context.RequestMap.put(ExternalContextImpl.java:1066)
    at org.jboss.seam.ui.component.UISelectItems$ContextualSelectItem.setup(UISelectItems.java:73)
    at org.jboss.seam.ui.component.UISelectItems$ContextualSelectItem.create(UISelectItems.java:90)
    at org.jboss.seam.ui.component.UISelectItems.asSelectItems(UISelectItems.java:195)
    at org.jboss.seam.ui.component.UISelectItems.getValue(UISelectItems.java:166)
    at com.sun.faces.renderkit.RenderKitUtils.getSelectItems(RenderKitUtils.java:323)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getOptionNumber(MenuRenderer.java:675)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:793)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:335)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:836)
    at org.jboss.seam.ui.util.cdk.RendererBase.renderChild(RendererBase.java:190)
    at org.jboss.seam.ui.util.cdk.RendererBase.renderChildren(RendererBase.java:166)
    at org.jboss.seam.ui.renderkit.ValidateAllRendererBase.doEncodeChildren(ValidateAllRendererBase.java:33)
    at org.jboss.seam.ui.util.cdk.RendererBase.encodeChildren(RendererBase.java:92)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
    at org.jboss.seam.ui.util.cdk.RendererBase.renderChild(RendererBase.java:186)
    at org.jboss.seam.ui.util.cdk.RendererBase.renderChildren(RendererBase.java:166)
    at org.jboss.seam.ui.renderkit.DecorateRendererBase.doEncodeChildren(DecorateRendererBase.java:152)
    at org.jboss.seam.ui.util.cdk.RendererBase.encodeChildren(RendererBase.java:92)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
    at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:277)
    at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:258)
    at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:220)
    at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:215)
    at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:120)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:137)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
    at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
    at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
    at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
    at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
    at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
    at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
    at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
    at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
    at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
    at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
    at java.lang.Thread.run(Thread.java:619)
demo
@demo Do not forget JSF is a server-side based component model (I am pretty sure It is a new concept To you as a PHP developer). If possible, read Seam in Action book or read Seam reference documentation. Later, i can see how can i help you
Arthur Ronald F D Garcia
Thanks a lot got it working for now, i learned a lot in the process and i will finish seam in action, appreciate it.
demo
A: 

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>
demo