简体   繁体   English

在jsf datatable中选择一行

[英]Select a row in jsf datatable

Here is my table: 这是我的表:

<h:dataTable var="book" value="#{ordersBean.userOrders}"
             binding="#{ordersBean.htmlDataTable}">
    <h:column>
        <f:facet name="header">order ID</f:facet>
        <h:outputText value="#{book[0]}"/>
    </h:column>
    <h:column>
        <f:facet name="header">Book Title</f:facet>
        <h:outputText value="#{book[1]}"/>
    </h:column>
    <h:column>
        <f:facet name="header">Cost</f:facet>
        <h:outputText value="#{book[2]}"/>
    </h:column>
    <h:column>
        <f:facet name="header">Remove Order</f:facet>
        <h:commandButton action="#{ordersBean.deleteOrder}" image="resources/images/del.gif"/>
    </h:column>
</h:dataTable>

I need to get Order ID of which row that the user clicks on it's Remove Order icon and then remove that order from DAO . 我需要获取用户点击它的Remove Order图标的行的Order ID ,然后从DAO删除该订单。

Here is the deleteOrder() : 这是deleteOrder()

@ManagedBean
@SessionScoped
public class OrdersBean {

private List<Book> userOrders = new ArrayList<Book>();
private HtmlDataTable htmlDataTable;

@Transactional
public void deleteOrder() {
    Book selectedBook = (Book) htmlDataTable.getRowData(); // class cast exception (168)
}

//For populating table
@Transactional
public List<Book> userAllOrders() {
    userOrders = bookDao.getTitleCostQty(String.valueOf(currentUser.getId())).list();
    return userOrders;
}

public HtmlDataTable getHtmlDataTable() {
    return htmlDataTable;
}

public void setHtmlDataTable(HtmlDataTable htmlDataTable) {
    this.htmlDataTable = htmlDataTable;
}
//getter/setters

But i get this error: 但我得到这个错误:

javax.servlet.ServletException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.obs.model.Book
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

javax.faces.el.EvaluationException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.obs.model.Book
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.obs.model.Book
    com.obs.bean.OrdersBean.deleteOrder(OrdersBean.java:168)

Do you use JSF 2.2? 你使用JSF 2.2吗? Maybe it's valid from 2.0 ... 也许它从2.0开始有效......

So you can pass the actual book within your commandButton: 所以你可以在commandButton中传递实际的书:

<h:commandButton actionListener="#{ordersBean.deleteOrder(book)}" ... />

and receive the selected book as parameter inside your bean: 并在bean中接收所选书籍作为参数:

public void deleteOrder(Book selectedBook) {
  // ...
}

value="#{ordersBean.userOrders}" for h:dataTable should be of DataModel type including WrappedData of your model type to avoid cast exception. 对于h:dataTable value="#{ordersBean.userOrders}" h:dataTable应该是DataModel类型,包括模型类型的WrappedData以避免强制转换异常。 Do as following: 请执行以下操作:

private DataModel userOrders = new ListDataModel();

public DataModel getUserOrders() {
    userOrders =  new ListDataModel();
    /* populate your list of Book in another list and setWrappedData for userOrders. */
    List<Book> bookList = userAllOrders();
    if (bookList != null && bookList.size() > 0){
        userOrders.setWrappedData(bookList);
    }   
    return bookList;
}

public void setUserOrders(DataModel userOrders) {
    this.userOrders = userOrders;
}

Now, here it will not through any cast exception: 现在,这里不会通过任何强制转换异常:

Book selectedBook = (Book) htmlDataTable.getRowData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM