java 数据表 sortBy 功能不起作用(素数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5473993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:17:15  来源:igfitidea点击:

dataTable sortBy function does not work(primefaces)

javajsfjakarta-eejsf-2primefaces

提问by sfrj

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.

我的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> 

This is the managed bean

这是托管 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

这是允许数据访问的 EJB

@Stateless(name = "ejbs/SearchEJB")

public class SearchEJB implements ISearchEJB {

公共类 SearchEJB 实现 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

修改后的托管 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

修改后的 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> 

采纳答案by Matt Handy

From the Java EE tutorialI read the following:

Java EE 教程中,我阅读了以下内容:

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.

返回类型

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

Now your query looks like:

现在您的查询如下所示:

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

From your entity class GarbageI read that filename, description, uploadDateare Strings. Then your query returns a list of Object[]and each array element contains a String and not a Garbageobject.

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

If your array contains Strings and not Garbage, you cannot call garbage[0].filenamein your facelet.

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

Try to change your Query in the following way:

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

SELECT g FROM Garbage g;

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

然后你得到一个List<Garbage>. 在将SearchEJB方法签名更改为以下内容:

public List<Garbage> findAllGarbage()

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

相应地更改ResultsController托管 bean 中的方法(现在您总是需要 aList<Garbage>而不是 a List<Garbage[]>)。

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

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

<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 Garbageobjects and leave the missing fields empty. Something like this (add a constructor to your Garbage class):

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

List<Garbage> gList = new ArrayList();

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

UPDATE 2:

更新 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.

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

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);
    }

回答by Denis K

I'm aware this was asked 3 years ago but I'll give you my solution just in case someone stumbles upon this.

我知道这是 3 年前提出的问题,但我会给你我的解决方案,以防万一有人偶然发现。

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:

基本上需要做的是在构造函数中为您的数据调用 findAll() 方法。但是由于 EJB 外观是在构造后注入的,因此您不能在构造函数中调用 findAllGarbage() 方法。您可以在用@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.

另外:在 getter 中调用 findAllGarbage() 方法会重置数据并且排序函数不会产生任何效果。因此,也许可以从 getter 中删除该方法并将 findAll 逻辑移到其他地方,例如在将数据添加到表的情况下的按钮操作上。

More about EJB injection here: ejbFacade is null

更多关于 EJB 注入的信息:ejbFacade is null