![](/img/trans.png)
[英]DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled, but I have already defined rowKey
[英]DataModel must implement org.primefaces.model.SelectableDataModel exception using primefaces filter
要过滤通过数据表显示的查询的结果集。 行选择,单击列标题的行排序以及数据表的分页功能都可以正常工作。 当我将素数过滤功能添加到数据表中时,我遇到了
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>
经过数小时的研究,我终于意识到要对其应用过滤器的对象实体不可序列化。 解决方法是从Serialization类继承对象实体 。
@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;
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.