为什么这个 dataTable sortBy 函数不起作用?

发布于 2024-10-28 04:25:37 字数 4161 浏览 1 评论 0原文

我继续尝试修复此 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

enter image description here

The console output when the page is refreshed
(SEVERE: line 1:61 no viable alternative at character ';'):

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

如果没有 2024-11-04 04:25:37

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

}

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

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

}
扛刀软妹 2024-11-04 04:25:37

这个答案有点晚了,但我希望它对其他人有帮助。
我在 Java7、JSF2 和 Primefaces 4 上遇到了同样的问题,我的修复方法是初始化 ArrayList<>创建 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' 属性。

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:

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

}

You don't need the 'filteredBy' and 'filterMatchMode' attributes anymore.

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