我一直在尝试修复此dataTable(Primefaces组件)的sortBy函数,但我无法理解为什么它不起作用,当其他功能如分页或过滤器正常工作时。对于这个dataTable,我只需要为其名为“value”的标记属性传递一个数组,并为名为“var”的标记属性传递一个相同类型数组的单个对象。在下面,我将发布我的代码。
这是带有dataTable的JSF页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:p="http://primefaces.prime.com.tr/ui">
<ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="resultsForm">
<h:form enctype="multipart/form-data">
<h:inputText id="search" value="" /><h:commandButton value="search"/>
<p:dataTable var="garbage" value="#{resultsController.allGarbage}" dynamic="true" paginator="true" paginatorPosition="bottom" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column filterBy="#{garbage.filename}" filterMatchMode="startsWith" sortBy="#{garbage.filename}" parser="string">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage.filename}" />
</p:column>
<p:column filterBy="#{garbage.description}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{garbage.description}" />
</p:column>
<p:column sortBy="#{garbage.uploadDate}" parser="string">
<f:facet name="header">
<h:outputText value="Upload date" />
</f:facet>
<h:outputText value="#{garbage.uploadDate}" />
</p:column>
</p:dataTable>
</h:form>
</ui:define>
这里是与该页面交互的托管bean:
@ManagedBean
@RequestScoped
public class ResultsController {
@EJB
private ISearchEJB searchEJB;
private Garbage garbage;
public List<Garbage> getAllGarbage() {
return searchEJB.findAllGarbage();
}
public Garbage getGarbage() {
return garbage;
}
public void setGarbage(Garbage garbage) {
this.garbage = garbage;
}
访问数据库的EJB:
@Stateless(name = "ejbs/SearchEJB")
public class SearchEJB implements ISearchEJB {
@PersistenceContext
private EntityManager em;
public List<Garbage> findAllGarbage() {
Query query = em.createNamedQuery("findAllGarbage");
List<Garbage> gList = new ArrayList<Garbage>();
for (Object o : query.getResultList()) {
Object[] cols = (Object[]) o;
Garbage tmpG = new Garbage();
tmpG.setFilename(cols[0].toString());
tmpG.setDescription(cols[1].toString());
tmpG.setUploadDate(cols[2].toString());
gList.add(tmpG);
}
return gList;
}
}
正在使用具有JPQL命名查询的实体:
@NamedQuery(name = "findAllGarbage", query = "SELECT g.filename, g.description, g.uploadDate FROM Garbage g;")
@Entity
public class Garbage {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String filename;
@Column(nullable = false)
private String fileType;
@Column(nullable = false)
private String uploadDate;
@Column(nullable = false)
private String destroyDate;
@Lob
@Column(nullable = false)
private byte[] file;
@Column(nullable = false)
private String description;
带浏览器输出的打印屏幕
刷新页面时的控制台输出 (严重:第1:61行没有可行的替代角色';'):
答案 0 :(得分:3)
将ResultsController的范围更改为@ViewScoped
。 ResultsController和Garbage对象需要实现java.io.Serializable。
也许这会有所帮助。以下是我开始工作的代码:
<强> index.xhmtl 强>
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<f:view contentType="text/html" >
<h:head>
<title>PrimeFacesTest</title>
</h:head>
<h:body>
<h:form>
<p:dataTable var="garbage" value="#{indexBean.allGarbage}"
dynamic="true" paginator="true" paginatorPosition="bottom" rows="4"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column filterBy="#{garbage.filename}" filterMatchMode="startsWith" sortBy="#{garbage.filename}" >
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage.filename}" />
</p:column>
<p:column filterBy="#{garbage.description}" filterMatchMode="contains" >
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{garbage.description}" />
</p:column>
<p:column sortBy="#{garbage.uploadDate}" parser="string">
<f:facet name="header">
<h:outputText value="Upload date" />
</f:facet>
<h:outputText value="#{garbage.uploadDate}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</f:view>
</html>
<强> IndexBean.java 强>
@ManagedBean
@ViewScoped
public class IndexBean implements Serializable {
private ArrayList allGarbage;
public IndexBean() {
allGarbage = new ArrayList();
allGarbage.add(new Garbage("Abc", "Abc", "28/03/2011 12:13:32"));
allGarbage.add(new Garbage("Bbc;", "bbc", "28/03/2011 12:14:32"));
allGarbage.add(new Garbage("Cbc", "Cbc", "28/03/2011 12:17:32"));
allGarbage.add(new Garbage("1Abc", "1Abc", "28/03/2011 12:13:32"));
allGarbage.add(new Garbage("2Bbc;", "2bbc", "28/03/2011 12:14:32"));
allGarbage.add(new Garbage("3Cbc", "3Cbc", "28/03/2011 12:17:32"));
allGarbage.add(new Garbage("4Abc", "4Abc", "28/03/2011 12:13:32"));
allGarbage.add(new Garbage("5Bbc;", "5bbc", "28/03/2011 12:14:32"));
allGarbage.add(new Garbage("6Cbc", "6Cbc", "28/03/2011 12:17:32"));
}
public ArrayList getAllGarbage() {
return allGarbage;
}
}
<强> Garbage.java 强>
public class Garbage implements Serializable {
private String filename;
private String description;
private String uploadDate;
public Garbage(String filename, String description, String uploadDate) {
this.filename = filename;
this.description = description;
this.uploadDate = uploadDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getUploadDate() {
return uploadDate;
}
public void setUploadDate(String uploadDate) {
this.uploadDate = uploadDate;
}
}
答案 1 :(得分:0)
这个答案有点晚,但我希望它有助于其他人。 我在Java7,JSF2和Primefaces 4上遇到了同样的问题,我的修复是初始化ArrayList&lt;&gt;在创建Bean时。 否则,排序函数无法对数据进行排序,并且只有在之前过滤了某些内容之后才能正常工作。
所以Bean看起来像:
@ManagedBean
@ViewScoped
public class IndexBean implements Serializable {
private ArrayList<Garbage> allGarbage;
@PostConstuct
public init()
{
allGarbage = new ArrayList<Garbage>();
allGarbage.add(new Garbage("Abc", "Abc", "28/03/2011 12:13:32"));
allGarbage.add(new Garbage("Bbc;", "bbc", "28/03/2011 12:14:32"));
allGarbage.add(new Garbage("Cbc", "Cbc", "28/03/2011 12:17:32"));
allGarbage.add(new Garbage("1Abc", "1Abc", "28/03/2011 12:13:32"));
allGarbage.add(new Garbage("2Bbc;", "2bbc", "28/03/2011 12:14:32"));
allGarbage.add(new Garbage("3Cbc", "3Cbc", "28/03/2011 12:17:32"));
allGarbage.add(new Garbage("4Abc", "4Abc", "28/03/2011 12:13:32"));
allGarbage.add(new Garbage("5Bbc;", "5bbc", "28/03/2011 12:14:32"));
allGarbage.add(new Garbage("6Cbc", "6Cbc", "28/03/2011 12:17:32"));
}
public ArrayList<Garbage> getAllGarbage()
{
return allGarbage;
}
public void setAllgarbage(ArrayList<Garbage> garbage)
{
this.allGarbage = garbage;
}
}
您不再需要'filteredBy'和'filterMatchMode'属性。