8 Replies Latest reply on Dec 28, 2008 11:11 PM by Nikolai Gagov

    How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>

    Sart Valcerou Newbie

      Hello!
      I'm trying to insert data into dataTable from class, but can't find any solution for a week. I know it's really easy to get values from entity but what if i just got a predefined List of values. What should i do?


      Thanks in advance.

        • 1. Re: How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>
          Mikael Andersson Master

          Hi


          Not really clear what you are asking.


          But to make a List available for usage by a component it has to be reachable via EL.
          So create a backing bean which exposes the list and just reference it like you would usually.


          @Name("myBackingBean")
          public class MyBackingBean{
          
          private List<String> list;
          
          @Create
          public void initData(){
          list = //populate it
          }
          
          public List<String> getList(){
          return list;
          }
          
          }
          



          <rich:dataTable value="#{myBackingBean.list}" ...
          
          



          - Micke

          • 2. Re: How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>
            Sart Valcerou Newbie

            Thank you for your reply.


            My Session bean



            @Name("namesAction")
            public @Stateless class NamesActionBean implements NamesAction {
                 
                 private List<String> names;
                 
                 public String namesAct(){
                      
                      names.add("John");       
                      names.add("Sam");       
                      names.add("Andy");
                      return null;
                 }
            
                 public List<String> getNames() {
                      return names;
                 }
            
                 public void setNames(List<String> names) {
                      this.names = names;
                 }
            
            }     



            my page



            <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:s="http://jboss.com/products/seam/taglib"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:rich="http://richfaces.org/rich"
                template="layout/template.xhtml">
            
            <ui:define name="body">
            
               
            
                <rich:panel>
                    <f:facet name="header">asd</f:facet>
                <div class="results" id="nameList">
            
                <rich:dataTable id="nameList"
                            var="_name"
                          value="#{namesAction.names}">
                    <h:column>
                        <f:facet name="header">
                            <ui:include src="layout/sort.xhtml">   
                                <ui:param name="propertyLabel" value="asd"/>
                            </ui:include>
                        </f:facet>
                        <h:outputText value="#{_name.names}"/>
                    </h:column>
                    </rich:dataTable>
            </div>
            </rich:panel>
            
            </ui:define>
            
            </ui:composition>
            



            exception



            18:43:24,390 ERROR [STDERR] 20.12.2008 18:43:24 com.sun.facelets.FaceletViewHandler handleRenderException
            SEVERE: Error Rendering View[/names.xhtml]
            javax.faces.FacesException: javax.el.PropertyNotFoundException: /names.xhtml @21,44 value="#{namesAction.names}": Property 'names' not found on type org.javassist.tmp.java.lang.Object_$$_javassist_7
                 at javax.faces.component.UIData.getValue(UIData.java:612)
                 at org.ajax4jsf.component.UIDataAdaptor.getValue(UIDataAdaptor.java:1640)
                 at org.ajax4jsf.component.SequenceDataAdaptor.getDataModel(SequenceDataAdaptor.java:48)
                 at org.ajax4jsf.component.SequenceDataAdaptor.createDataModel(SequenceDataAdaptor.java:42)
                 at org.richfaces.component.UIDataTable.createDataModel(UIDataTable.java:122)
                 at org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:621)
                 at org.ajax4jsf.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:339)
                 at org.richfaces.renderkit.AbstractTableRenderer.encodeTableStructure(AbstractTableRenderer.java:121)
                 at org.richfaces.renderkit.html.DataTableRenderer.doEncodeBegin(DataTableRenderer.java:206)
                 at org.richfaces.renderkit.html.DataTableRenderer.doEncodeBegin(DataTableRenderer.java:194)
                 at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:101)
                 at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:813)
                 at javax.faces.component.UIData.encodeBegin(UIData.java:962)
                 at org.ajax4jsf.component.UIDataAdaptor.encodeBegin(UIDataAdaptor.java:1220)
                 at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:280)
                 at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
                 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:121)
                 at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
                 at javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
                 at javax.faces.component.UIComponent.encodeAll(UIComponent.java:942)
                 at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
                 at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
                 at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
                 at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
                 at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
                 at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
                 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:38)
                 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:177)
                 at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
                 at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
                 at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
                 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:58)
                 at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                 at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:54)
                 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:182)
                 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(Unknown Source)
            Caused by: javax.el.PropertyNotFoundException: /names.xhtml @21,44 value="#{namesAction.names}": Property 'names' not found on type org.javassist.tmp.java.lang.Object_$$_javassist_7
                 at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:73)
                 at javax.faces.component.UIData.getValue(UIData.java:609)
                 ... 69 more



            What i do wrong?



            • 4. Re: How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>
              Sart Valcerou Newbie

              I just add getNames() to my interface and now i'm not getting any exceptions, but my dataTable is still empty. I think i made a mistake here


              
                  <rich:dataTable id="nameList"
                              var="_name"
                            value="#{namesAction.names}">
                      <h:column>
                          <f:facet name="header">
                              <ui:include src="layout/sort.xhtml">   
                                  <ui:param name="propertyLabel" value="asd"/>
                              </ui:include>
                          </f:facet>
                          <h:outputText value="#{_name.names}"/>
                      </h:column>
                      </rich:dataTable>
              
              



              but still can't find it. Any help appreciated.

              • 5. Re: How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>
                Karthikeyan Chockalingam Newbie

                I guess you are returning null.. Make it



                public List<String> getNames() {
                                names=new ArrayList<String>();
                                names.add("John");       
                          names.add("Sam");       
                          names.add("Andy");
                          return names;
                }



                • 6. Re: How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>
                  Edwin van der Elst Newbie

                  Your 'names' list is used in datatable value=#{namesAction.names}.
                  Each row is a single String, referenced by name (your 'var' attribute).
                  String doesn't have a 'getNames()' method (the expression
                  name.names is used in the outputText).


                  Remember, the expressions in the column tag are based on a single row of your datamodel.


                  Also, I'm not sure if you can actually use a list of String objects as a Datamodel. I only use lists of POJO's for this.

                  • 8. Re: How to retrieve List from POJO class into <h:dataTable> or <rich:dataTable>
                    Nikolai Gagov Newbie
                    You can also access 'names' collection directly without to specify the action inside the
                    facelet:

                    <rich:dataTable id="nameList"
                                    var="_name"
                                  value="#{names}">

                    and inside the action:
                    @Out
                    private List<String> names;

                    public List<String> getNames() {
                       return names;
                    }

                    public void setNames(List<String> names) {
                       this.names = names;
                    }

                    @Factory("names")
                    public List<String> initNames() {
                       names=new ArrayList<String>();
                       names.add("John");      
                       names.add("Sam");      
                       names.add("Andy");
                       return names;
                    }
                    You must add the factory method into the interface too