为什么这个 dataTable sortBy 函数不起作用?
我继续尝试修复此 dataTable(Primefaces 组件)的 sortBy 函数,但我只是无法理解为什么当分页或过滤器等其他功能正常工作时它不起作用。对于这个数据表,我只需要传递一个名为“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 行在字符“;”处没有可行的替代方案):
I keep on trying to fix the sortBy function of this dataTable(Primefaces component) but i just cant understand why it doesnt work, when other features like pagination or filter work correctly. For this dataTable i just need to pass an array for its tag attribute called "value" and also a single object of the same type of the array for the tag attribute called "var". Down below i will post my code.
This is the JSF page with the dataTable
<!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>
Here the managed bean that interacts with that page:
@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;
}
The EJB that accesses the database:
@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;
}
}
The entity with the JPQL named query being used:
@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;
A print screen with browsers output
The console output when the page is refreshed
(SEVERE: line 1:61 no viable alternative at character ';'):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 ResultsController 的范围更改为
@ViewScoped
。 ResultsController 和 Garbage 对象需要实现 java.io.Serializable。也许这会有所帮助。下面是我开始工作的代码:
index.xhmtl
IndexBean.java
Garbage.java
Change the scope of the ResultsController to
@ViewScoped
. The ResultsController and the Garbage object will need to implement java.io.Serializable.Maybe this will help. Below is my code that I got to work:
index.xhmtl
IndexBean.java
Garbage.java
这个答案有点晚了,但我希望它对其他人有帮助。
我在 Java7、JSF2 和 Primefaces 4 上遇到了同样的问题,我的修复方法是初始化 ArrayList<>创建 Bean 时。
否则,排序函数无法对数据进行排序,并且仅在之前过滤了某些内容的情况下才能像所描述的那样工作。
所以 Bean 看起来像:
您不再需要 'filteredBy' 和 'filterMatchMode' 属性。
This answer is a bit late but i hope it helps others.
I had the same problem on Java7, JSF2 with Primefaces 4 and my fix was to initialize the ArrayList<> upon creation of the Bean.
Otherwise the sort function fails to sort the data and works like described, only if something is filtered before.
So the Bean would look like:
You don't need the 'filteredBy' and 'filterMatchMode' attributes anymore.