Board index JavaServer Faces General ListDataModel cannot be cast to LazyDataModel

ListDataModel cannot be cast to LazyDataModel

Components, Ajax Framework, Utilities and More.


Posts: 69
Hello,

I tried to implement sorting and filtering for my datatable, but I got this error stop me from going further.

Here is the full trace:

java.lang.ClassCastException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
   at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:641)
   at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:407)
   at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:164)
   at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:80)
   at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620)
   at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
   at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
   at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
   at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
   at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
   at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
   at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
   at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
   at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
   at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
   at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
   at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
   at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
   at java.lang.Thread.run(Thread.java:619)


Here is the java code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.customerapp.web;

import com.customerapp.ejb.CustomerSessionBean;
import com.customerapp.entity.Customer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author sam
 */
@ManagedBean(name="customer")
@SessionScoped
public class CustomerMBean
{

    @EJB
    private CustomerSessionBean customerSessionBean;
    private Customer customer;
    private final static Logger logger = Logger.getLogger(CustomerMBean.class.getName());

    private LazyDataModel<Customer> lazyModel;

    public CustomerMBean()
    {
        lazyModel = new LazyDataModel<Customer>() {

         /**
          * Dummy implementation of loading a certain segment of data.
          * In a real application, this method should load data from a datasource
          */
         @Override
         public List<Customer> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String,String> filters) {
            logger.log(Level.INFO, "Loading the lazy car data between {0} and {1}", new Object[]{first, (first+pageSize)});

                //Sorting and Filtering information are not used for demo purposes just random dummy data is returned

            //customerSessionBean.setFirstResult(first);
                               // customerSessionBean.setMaxResults(pageSize);
                              //  return customerSessionBean.getResultList();
                                    return  null;
         }
      };
    }

   
    public LazyDataModel<Customer> getLazyModel() {
        return lazyModel;
    }
    public void setLazyModel(LazyDataModel<Customer> lazyModel) {
        this.lazyModel = lazyModel;
    }
 

    /**
     * Returns list of customer objects to be displayed in the data table
     * @return
     */
    public List<Customer> getCustomers()
    {
        return customerSessionBean.retrieve();
    }

    /**
     * Returns the selected Customer object
     * @return
     */
    public Customer getDetails()
    {
        //Can either do this for simplicity or fetch the details again from the
        //database using the Customer ID
        return customer;
    }

    /**
     * Action handler - user selects a customer record from the list
     * (data table) to view/edit
     * @param customer
     * @return
     */
    public String showDetails(Customer customer)
    {
        this.customer = customer;
        return "DETAILS";
    }

    /**
     * Action handler - update the changes in the Customer object to the
     * database
     * @return
     */
    public String update()
    {
        System.out.println("###UPDATE###");
        customer = customerSessionBean.update(customer);
        return "SAVED";
    }

    /**
     * Action handler - goes to the Customer listing page
     * @return
     */
    public String list()
    {
        System.out.println("###LIST###");
        return "LIST";
    }
}


PF 2.2RC2, Glassfish v3, Netbeans 6.9.1

Thanks
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

Return to General