dataTable sortBy 函数不起作用(primefaces)
我对 primefaces dataTable 组件有疑问。我不知道为什么当我点击它时它不会缩短表中的数据。
<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 sortBy="#{garbage[0].filename}">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage[0]}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{garbage[1]}" />
</p:column>
<p:column sortBy="#{garbage[2].uploadDate}">
<f:facet name="header">
<h:outputText value="Upload date" />
</f:facet>
<h:outputText value="#{garbage[2]}" />
</p:column>
</p:dataTable>
这是托管 bean
@ManagedBean
@RequestScoped
public class ResultsController {
@EJB
private ISearchEJB searchEJB;
private Garbage[] garbage;
public List<Garbage[]> getAllGarbage() {
return searchEJB.findAllGarbage();
}
public Garbage[] getGarbage() {
System.out.println("VALUES!!!!!!!!" + garbage[0].getFilename());
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");
return query.getResultList();
}
}
这是实体(数据表示)
@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;
...//Getters and Setters
如图所示,单击排序按钮时没有任何变化:
这是控制台的内容:
严重:排序错误
UPDATE
public List<Garbage> findAllGarbage() {
Query query = em.createNamedQuery("findAllGarbage");
List<Garbage> gList = new ArrayList();
for (Object o: query.getResultList()) {
Garbage tmpG = new Garbage();
tmpG.setFilename(((Garbage) o).getFilename());
tmpG.setUploadDate(((Garbage) o).getUploadDate());
tmpG.setDescription(((Garbage) o).getDescription());
gList.add(tmpG);
}
return gList;
}
修改后的托管 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;
}
}
修改后的 JSF
<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 sortBy="#{garbage.filename}" parser="string">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage.filename}" />
</p:column>
<p:column>
<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>
I have a problem with the primefaces dataTable component. I dont know why it does not short the data in the table when i click on it.
<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 sortBy="#{garbage[0].filename}">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage[0]}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{garbage[1]}" />
</p:column>
<p:column sortBy="#{garbage[2].uploadDate}">
<f:facet name="header">
<h:outputText value="Upload date" />
</f:facet>
<h:outputText value="#{garbage[2]}" />
</p:column>
</p:dataTable>
This is the managed bean
@ManagedBean
@RequestScoped
public class ResultsController {
@EJB
private ISearchEJB searchEJB;
private Garbage[] garbage;
public List<Garbage[]> getAllGarbage() {
return searchEJB.findAllGarbage();
}
public Garbage[] getGarbage() {
System.out.println("VALUES!!!!!!!!" + garbage[0].getFilename());
return garbage;
}
public void setGarbage(Garbage[] garbage) {
this.garbage = garbage;
}
}
This is the EJB that allows data access
@Stateless(name = "ejbs/SearchEJB")
public class SearchEJB implements ISearchEJB {
@PersistenceContext
private EntityManager em;
public List<Garbage[]> findAllGarbage() {
Query query = em.createNamedQuery("findAllGarbage");
return query.getResultList();
}
}
And this is the entity(Data representation)
@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;
...//Getters and Setters
As shown in the image there is no changes when the sort buttons are clicked:
This is what the console says:
SEVERE: Error in sorting
UPDATE
public List<Garbage> findAllGarbage() {
Query query = em.createNamedQuery("findAllGarbage");
List<Garbage> gList = new ArrayList();
for (Object o: query.getResultList()) {
Garbage tmpG = new Garbage();
tmpG.setFilename(((Garbage) o).getFilename());
tmpG.setUploadDate(((Garbage) o).getUploadDate());
tmpG.setDescription(((Garbage) o).getDescription());
gList.add(tmpG);
}
return gList;
}
The modified managed 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;
}
}
The modified JSF
<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 sortBy="#{garbage.filename}" parser="string">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage.filename}" />
</p:column>
<p:column>
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Java EE 教程我读到了以下内容:
现在您的查询如下所示:
从您的实体类
Garbage
中,我读到filename
、description
、uploadDate
是字符串。然后,您的查询返回一个Object[]
列表,每个数组元素包含一个 String 而不是Garbage
对象。如果您的数组包含字符串而不是垃圾,则无法在 Facelet 中调用
garbage[0].filename
。尝试按以下方式更改您的查询:
然后您会得到一个
List
。在SearchEJB
中将方法签名更改为以下内容:相应地更改
ResultsController
托管 bean 中的方法(现在您始终需要一个List而不是
List
)。最后修改您的
p:dataTable
(显示第一列):更新:
如果您想保留查询,您可以转换
Object[]
到Garbage
对象列表中,并将缺少的字段留空。像这样的东西(向你的垃圾类添加一个构造函数):更新2:
在你的更新中你再次错过了,你的数组包含字符串。并且字符串按照您的选择表达式的顺序进行排序:元素 0:文件名,元素 1:描述,元素 2:上传日期。
按以下方式更改循环:
From the Java EE tutorial I read the following:
Now your query looks like:
From your entity class
Garbage
I read thatfilename
,description
,uploadDate
are Strings. Then your query returns a list ofObject[]
and each array element contains a String and not aGarbage
object.If your array contains Strings and not Garbage, you cannot call
garbage[0].filename
in your facelet.Try to change your Query in the following way:
Then you get a
List<Garbage>
. In theSearchEJB
change the method signature to the following:Change the methods in your
ResultsController
managed bean accordingly (now you always need aList<Garbage>
and not aList<Garbage[]>
).Finally modify your
p:dataTable
(shown for the first column):UPDATE:
If you want to keep your query you could convert the
Object[]
into a list ofGarbage
objects and leave the missing fields empty. Something like this (add a constructor to your Garbage class):UPDATE 2:
In your update you missed again, that your array contains strings. And the strings are ordered as your select expression is ordered: element 0: filename, element 1: description, element 2: uploadDate.
Change your loop in the following way:
我知道三年前就有人问过这个问题,但我会给你我的解决方案,以防万一有人偶然发现这个问题。
基本上需要做的是在构造函数内为数据调用 findAll() 方法。但由于 EJB 外观是在构造后注入的,因此您无法在构造函数内调用 findAllGarbage() 方法。您可以在注释为的方法中执行此操作
@PostConstruct:
另外:在 getter 中调用 findAllGarbage() 方法会重置数据,并且排序函数不会产生任何效果。因此,也许可以从 getter 中删除该方法,并将 findAll 逻辑移到其他地方,例如在将数据添加到表的情况下的按钮操作上。
有关 EJB 注入的更多信息,请参见:ejbFacade 为 null
I'm aware this was asked 3 years ago but I'll give you my solution just in case someone stumbles upon this.
Basically what needs to be done is to call the findAll() method for your data inside a constructor. But since EJB facade is injected post construction, you can't call the findAllGarbage() method inside a constructor. You can do it in a method annotated with
@PostConstruct:
Also: calling the findAllGarbage() method in a getter resets the data and the sort function won't have any effect. So maybe remove that method from the getter and move the findAll logic elsewhere, for example on a button action in case of adding data to the table.
More about EJB injection here: ejbFacade is null