dataTable sortBy 函数不起作用(primefaces)

发布于 2024-10-27 21:25:24 字数 5129 浏览 1 评论 0原文

我对 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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2024-11-03 21:25:24

Java EE 教程我读到了以下内容:

返回类型

SELECT 子句的返回类型
由结果类型定义
选择其中包含的表达式
它。如果使用多个表达式,
查询的结果是
Object[] 以及 中的元素
数组对应的顺序
SELECT 子句中的表达式,以及
in type 到每个的结果类型
表达。

现在您的查询如下所示:

SELECT g.filename, g.description,  g.uploadDate FROM Garbage g;

从您的实体类 Garbage 中,我读到 filenamedescriptionuploadDate 是字符串。然后,您的查询返回一个 Object[] 列表,每个数组元素包含一个 String 而不是 Garbage 对象。

如果您的数组包含字符串而不是垃圾,则无法在 Facelet 中调用 garbage[0].filename

尝试按以下方式更改您的查询:

SELECT g FROM Garbage g;

然后您会得到一个List。在 SearchEJB 中将方法签名更改为以下内容:

public List<Garbage> findAllGarbage()

相应地更改 ResultsController 托管 bean 中的方法(现在您始终需要一个 List而不是 List)。

最后修改您的p:dataTable(显示第一列):

<p:column sortBy="#{garbage.filename}">  
  <f:facet name="header">  
    <h:outputText value="Filename" />  
  </f:facet>  
  <h:outputText value="#{garbage.filename}" />
</p:column> 

更新:

如果您想保留查询,您可以转换Object[]Garbage 对象列表中,并将缺少的字段留空。像这样的东西(向你的垃圾类添加一个构造函数):

List<Garbage> gList = new ArrayList();

for (Object[] o: query.getResultList()) {
  gList.add(new Garbage(o[0], o[1], o[2]);
}

更新2:

在你的更新中你再次错过了,你的数组包含字符串。并且字符串按照您的选择表达式的顺序进行排序:元素 0:文件名,元素 1:描述,元素 2:上传日期。

按以下方式更改循环:

for (Object o: query.getResultList()) {         
      Garbage tmpG = new Garbage();
      tmpG.setFilename(o[0]);          
      tmpG.setDescription(o[1]);
      tmpG.setUploadDate(o[2]);

      gList.add(tmpG);
    }

From the Java EE tutorial I read the following:

Return Types

The return type of the SELECT clause
is defined by the result types of the
select expressions contained within
it. If multiple expressions are used,
the result of the query is an
Object[], and the elements in the
array correspond to the order of the
expressions in the SELECT clause, and
in type to the result types of each
expression.

Now your query looks like:

SELECT g.filename, g.description,  g.uploadDate FROM Garbage g;

From your entity class Garbage I read that filename, description, uploadDate are Strings. Then your query returns a list of Object[] and each array element contains a String and not a Garbage 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:

SELECT g FROM Garbage g;

Then you get a List<Garbage>. In the SearchEJB change the method signature to the following:

public List<Garbage> findAllGarbage()

Change the methods in your ResultsController managed bean accordingly (now you always need a List<Garbage> and not a List<Garbage[]>).

Finally modify your p:dataTable (shown for the first column):

<p:column sortBy="#{garbage.filename}">  
  <f:facet name="header">  
    <h:outputText value="Filename" />  
  </f:facet>  
  <h:outputText value="#{garbage.filename}" />
</p:column> 

UPDATE:

If you want to keep your query you could convert the Object[] into a list of Garbage objects and leave the missing fields empty. Something like this (add a constructor to your Garbage class):

List<Garbage> gList = new ArrayList();

for (Object[] o: query.getResultList()) {
  gList.add(new Garbage(o[0], o[1], o[2]);
}

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:

for (Object o: query.getResultList()) {         
      Garbage tmpG = new Garbage();
      tmpG.setFilename(o[0]);          
      tmpG.setDescription(o[1]);
      tmpG.setUploadDate(o[2]);

      gList.add(tmpG);
    }
真心难拥有 2024-11-03 21:25:24

我知道三年前就有人问过这个问题,但我会给你我的解决方案,以防万一有人偶然发现这个问题。

基本上需要做的是在构造函数内为数据调用 findAll() 方法。但由于 EJB 外观是在构造后注入的,因此您无法在构造函数内调用 findAllGarbage() 方法。您可以在注释为的方法中执行此操作
@PostConstruct:

@PostConstruct
public void myInitMethod(){
   garbage = searchEJB.findAllGarbage();
}

另外:在 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:

@PostConstruct
public void myInitMethod(){
   garbage = searchEJB.findAllGarbage();
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文