Primefaces数据表将选定行作为类型arrayList [duplicate]

时间:2022-09-17 11:02:41

This question already has an answer here:

这个问题在这里已有答案:

Here is my table in primefaces datatable:

这是我在primefaces数据表中的表:

(I'm using hibernate to get table rows)

(我正在使用hibernate来获取表行)

Primefaces数据表将选定行作为类型arrayList [duplicate]

code:

<f:event type="preRenderView" listener="#{myCardBean.userCard(userBean.currentUser.id)}"/>
...
<p:dataTable id="books" value="#{myCardBean.booksList}" var="book" selectionMode="single"
             selection="#{myCardBean.selectedOrder}" rowKey="#{book[0]}">

    <p:ajax event="rowSelect" listener="#{myCardBean.onRowSelect}"/>
    <p:ajax event="rowUnselect" listener="#{myCardBean.onRowUnselect}"/>

    <p:column>
        <f:facet name="header">Order ID</f:facet>
        <h:outputText value="#{book[0]}"/>
    </p:column>
    <p:column>
        <f:facet name="header">Book Title</f:facet>
        <h:outputText value="#{book[1]}"/>
    </p:column>
    <p:column>
        <f:facet name="header">Book Cost</f:facet>
        <h:outputText value="#{book[2]}"/>
    </p:column>
    <p:column>
        <f:facet name="header">Author</f:facet>
        <h:outputText value="#{book[3]}"/>
    </p:column>
    <p:column>
        <f:facet name="header">Order Date </f:facet>
        <h:outputText value="#{book[4]}"/>
    </p:column>
    <p:column>
        <f:facet name="header">QTY</f:facet>
        <h:outputText value="#{book[5]}"/>
    </p:column>
    <p:column>
        <f:facet name="header">Total Price</f:facet>
        <h:outputText value="#{book[6]}"/>
    </p:column>
</p:dataTable>

Here is MyCardBean class:

这是MyCardBean类:

@Autowired
private BookDao bookDao;
private Object[] selectedOrder;
private List<Object[]> orderBookInfo;

public List<Book> userCard(String uID) {
    booksList =  bookDao.getOrderAndBookInfo(uID).list(); //returns array list of orderId, book title, book cost,...
    return booksList;
}


public List<Object> getOrderBookInfo() {
    return orderBookInfo;
}

public void setOrderBookInfo(List<Object[]> orderBookInfo) {
    this.orderBookInfo = orderBookInfo;
}

public Object[] getSelectedOrder() {
    return selectedOrder;
}

public void setSelectedOrder(Object[] selectedOrder) {
    this.selectedOrder = selectedOrder;
}

public void onRowSelect(SelectEvent event) {
    System.out.println("row selected, " + event.getObject());
}

public void onRowUnselect(UnselectEvent event) {
    System.out.println("row Unselected");
}

Each row of table is an java.util.ArrayList type.

表的每一行都是java.util.ArrayList类型。

I got no exception in row selection, But it displays like this:

在行选择中我没有例外,但它显示如下:

row selected, [Ljava.lang.Object;@c25754e

(I tried Object type instead of Object[] for selectedOrder, but got the same result)

(我为selectedOrder尝试了Object类型而不是Object [],但得到了相同的结果)

Why it can't displays correctly when i select a row?, like:

为什么当我选择一行时它无法正确显示?,如:

3 , ontellijidea , 20000.0 , Mr aa , 2015-05-03 , 5 , 100000.0

UPDATE

public SQLQuery getOrderAndBookInfo(String userId) {
    SQLQuery sqlQuery = getSession().createSQLQuery("SELECT " +
            " orders.id, book.title, book.cost, book.author, orders.orderDate, orderitem.quantity, orderitem.totalPrice " +
            "FROM book" +
            " INNER JOIN orderitem ON book.id = orderitem.book_id " +
            "  INNER JOIN orders ON orders.id = orderitem.orders_id " +
            "WHERE user_id = :userID ");
    sqlQuery.setString("userID", userId);
    return sqlQuery;
}

Model classes:

Primefaces数据表将选定行作为类型arrayList [duplicate]

1 个解决方案

#1


The event.getObject() is an Object[], not an arraylist that is what the [L in [Ljava.lang.Object;@c25754e means, so it is exactly as what should be and your subject/title is wrong.

event.getObject()是一个Object [],而不是一个arraylist,它是[Ljava.lang.Object; @ c25754e的意思,因此它应该是什么,你的主题/标题是错误的。

Java does,for obious reasons, not know how to print this so it prints what you see. You most likely did not try to print event.getObject()[0].

出于各种原因,Java确实不知道如何打印它,因此它打印出你看到的内容。您很可能没有尝试打印event.getObject()[0]。

And again, this is all very basic java stuff that if you takea goood (very good) look at what is actually happening can be found with some simple searches on the internet. Please, as stated multiple times now learn basic java first and learn some troubleshooting

再次,这是所有非常基本的Java东西,如果你采取goood(非常好)看看实际发生的事情可以在互联网上找到一些简单的搜索。请多说几次,现在先学习基础java,然后学习一些故障排除

#1


The event.getObject() is an Object[], not an arraylist that is what the [L in [Ljava.lang.Object;@c25754e means, so it is exactly as what should be and your subject/title is wrong.

event.getObject()是一个Object [],而不是一个arraylist,它是[Ljava.lang.Object; @ c25754e的意思,因此它应该是什么,你的主题/标题是错误的。

Java does,for obious reasons, not know how to print this so it prints what you see. You most likely did not try to print event.getObject()[0].

出于各种原因,Java确实不知道如何打印它,因此它打印出你看到的内容。您很可能没有尝试打印event.getObject()[0]。

And again, this is all very basic java stuff that if you takea goood (very good) look at what is actually happening can be found with some simple searches on the internet. Please, as stated multiple times now learn basic java first and learn some troubleshooting

再次,这是所有非常基本的Java东西,如果你采取goood(非常好)看看实际发生的事情可以在互联网上找到一些简单的搜索。请多说几次,现在先学习基础java,然后学习一些故障排除