将 JPQL 自定义查询结果渲染到 JSF 页面中的问题

发布于 2024-10-28 01:05:40 字数 3639 浏览 1 评论 0原文

我有一个具有以下命名查询的实体:

@NamedQuery(name = "findAllGarbage", query = "SELECT g.filename, g.description, g.uploadDate FROM Garbage g;")

问题是我想将该结果传递给 dataTable 进行渲染,并且我收到 NumberFormatException。我不明白为什么,因为到处都没有数字。

程序的其余部分如下所示:

- 执行查询的 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> tmpGarbage = query.getResultList();
    return tmpGarbage;
}

- 显示 tableData 的 JSF 部分:

<p:dataTable var="garbage" value="#{resultsController.allGarbage}" paginator="true" rows="10"  
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15">         

            <p:column>  
            <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>  
            <f:facet name="header">  
            <h:outputText value="Upload date" />  
            </f:facet>  
            <h:outputText value="#{garbage.uploadDate}" /> 
             </p:column>                
    </p:dataTable> 

- 与 JSF 页面交互的托管 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;
}

错误显示:

警告:StandardWrapperValve[Faces Servlet]:PWC1406:servlet Faces Servlet 的 Servlet.service() 引发异常 java.lang.NumberFormatException:对于输入字符串:“文件名”

我不明白文件名有什么问题。

- - - - - - - - - - - - - - - - - - 更新 - - - - - - - -----------------------

我将 JSF 更改为这个,现在我没有看到错误。我看到该表数据但为空:

<p:dataTable var="garbage" value="#{resultsController.allGarbage}" paginator="true" rows="10"  
         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
         rowsPerPageTemplate="5,10,15">         

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Filename" />  
        </f:facet>  
        <h:outputText value="#{garbage[4]}" />
         </p:column> 

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Description" />  
        </f:facet>  
        <h:outputText value="#{garbage[3]}" />  
         </p:column> 

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Upload date" />  
        </f:facet>  
        <h:outputText value="#{garbage[6]}" /> 
         </p:column>                
</p:dataTable> 

I have an entity with the following named query:

@NamedQuery(name = "findAllGarbage", query = "SELECT g.filename, g.description, g.uploadDate FROM Garbage g;")

The problem is that i want to pass that result to a dataTable to render, and i recieve NumberFormatException. I dont understand why because there are no numbers anywhere.

This is how the rest of the program looks like:

-The EJB that executes the query

@Stateless(name = "ejbs/SearchEJB")
public class SearchEJB implements ISearchEJB {

@PersistenceContext
private EntityManager em;


public List<Garbage> findAllGarbage() {
    Query query = em.createNamedQuery("findAllGarbage");    
    List<Garbage> tmpGarbage = query.getResultList();
    return tmpGarbage;
}

-The part of the JSF that displays the tableData:

<p:dataTable var="garbage" value="#{resultsController.allGarbage}" paginator="true" rows="10"  
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15">         

            <p:column>  
            <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>  
            <f:facet name="header">  
            <h:outputText value="Upload date" />  
            </f:facet>  
            <h:outputText value="#{garbage.uploadDate}" /> 
             </p:column>                
    </p:dataTable> 

-The managed bean that interacts with the JSF 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 error says:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NumberFormatException: For input string: "filename"

I dont understand what is wrong with the file name.

------------------------------------UPDATE------------------------------------

I changed the JSF to this, and now i dont see the error. I see tha table data but empty:

<p:dataTable var="garbage" value="#{resultsController.allGarbage}" paginator="true" rows="10"  
         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
         rowsPerPageTemplate="5,10,15">         

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Filename" />  
        </f:facet>  
        <h:outputText value="#{garbage[4]}" />
         </p:column> 

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Description" />  
        </f:facet>  
        <h:outputText value="#{garbage[3]}" />  
         </p:column> 

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Upload date" />  
        </f:facet>  
        <h:outputText value="#{garbage[6]}" /> 
         </p:column>                
</p:dataTable> 

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

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

发布评论

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

评论(1

慈悲佛祖 2024-11-04 01:05:40

该问题是由以下事实引起的:诸如

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

返回一个 Object[] ,其中文件名、描述和 uploadDate 作为其元素。

如果您想将它们作为对象属性进行访问(就像在 JSF 中所做的那样),则需要查询完整的对象:

SELECT g FROM Garbage g

The problem is caused by the fact that a query such as

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

returns an Object[] with filename, description and uploadDate as its elements.

If you want to access them as object properties (as you do in JSF), you need to query for the full object instead:

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