想要过滤通过数据表显示的查询的结果集。行选择,单击列标题的行排序和数据表的分页功能正常工作。当我将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;
....
}
Managed 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
...
}
的facelet:
<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>
答案 0 :(得分:2)
经过数小时的调查,我终于意识到我应用过滤器的对象实体是不可序列化的。 解决方案是从序列化类继承对象实体。
@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;
....
}