【问题标题】:DataModel must implement org.primefaces.model.SelectableDataModel exception using primefaces filterDataModel 必须使用 primefaces 过滤器实现 org.primefaces.model.SelectableDataModel 异常
【发布时间】:2016-08-23 10:30:27
【问题描述】:

想要过滤通过数据表显示的查询的结果集。行选择,单击列标题的行排序和数据表的分页功能工作正常。当我将 primefaces 过滤功能添加到数据表时,我会遇到

javax.faces.FacesException:DataModel 必须实现 org.primefaces.model.SelectableDataModel 启用选择时。

对象实体:

@Entity
@Table(name="Customer", 
       uniqueConstraints={@UniqueConstraint(columnNames={"ID"})})

public class Customer {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID", nullable=false, unique=true, length=11)
    private Integer id;

    @Column(name="LASTNAME", length=40, nullable=false)
    private String lastName;

    @Column(name="FIRSTNAME", length=30, nullable=true)
    private String firstName;
....
}

托管 Bean:

@ManagedBean(name = "customerController")
@ViewScoped

public class CustomerController implements Serializable {

    private static final long serialVersionUID = 1L;
    private Customer selectedCustomer = new Customer();
    private List<Customer> customers = new ArrayList<Customer>();
    private String  message;

    public CustomerController() {
    }

    @PostConstruct
    void init() {        
        CustomerDAO custDAO = new CustomerDAO();
        customers = custDAO.getAllCustomers(); 

        // select first row
        if (customers != null) selectedCustomer=customers.get(0);
    }

    public void onRowSelect(SelectEvent event) {  
        message = "";
    }  

    public void onRowUnselect(UnselectEvent event) {  
        message = "";
    } 

    // getters and setters
    ...
}

小脸:

<ui:define name="contentPart1" >            
    <h:form id="contentPart1Form">
        <p:dataTable id="singleSelection" var="customer" value="#{customerController.customers}" rowKey="#{customer.id}" 
            selection="#{customerController.selectedCustomer}" selectionMode="single" paginator="true" rows="10">
            <p:ajax event="rowSelect" listener="#{customerController.onRowSelect}" />

            <p:column headerText="#{msg['customerCRUD.labelIdentifier']}" style="width:15%;">
                <h:outputText value="#{customer.id}" readonly="#{facesContext.currentPhaseId.ordinal eq 6}"/>
            </p:column>
            <p:column headerText="#{msg['customerCRUD.labelFirstName']}" sortBy="#{customer.firstName}" style="width:30%;">
                <h:outputText value="#{customer.firstName}" />
            </p:column>
            <p:column headerText="#{msg['customerCRUD.labelLastName']}"  filterBy="#{customer.lastName}" filterMatchMode="contains" 
                sortBy="#{customer.lastName}">
                <h:outputText value="#{customer.lastName}" />
            </p:column>

            <f:facet name="footer">
                <h:outputText value=" "/>
            </f:facet>                  
        </p:dataTable>              
    </h:form>
</ui:define>

【问题讨论】:

    标签: jsf primefaces filter datatable


    【解决方案1】:

    经过数小时的调查,我终于意识到我应用过滤器的对象实体不可序列化。 解决办法是从序列化类继承对象实体

    @Entity
    @Table(name="Customer", 
           uniqueConstraints={@UniqueConstraint(columnNames={"ID"})})
    
    public class Customer implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;{
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="ID", nullable=false, unique=true, length=11)
        private Integer id;
    
        @Column(name="LASTNAME", length=40, nullable=false)
        private String lastName;
    
        @Column(name="FIRSTNAME", length=30, nullable=true)
        private String firstName;
    ....
    }
    

    【讨论】:

    • Serializable 是一个接口,所以你不继承而是实现
    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2014-12-12
    • 1970-01-01
    相关资源
    最近更新 更多