在JSF中从Datatable获取选定的行数据 [英] Getting Selected Row Data from Datatable in JSF

查看:103
本文介绍了在JSF中从Datatable获取选定的行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图在这个问题中实现Anthony / BalusC的方法:如何在JSF datatable中获取选定的行索引,但无效。我也通过了@ BalusC的使用数据表一文(这是美妙的一如既往),但这是为JSF1.2和 2.0文章并没有真正地获取选定的行数据。当用户点击添加到我的收藏按钮时,所选行不会传递给支持bean,导航案例未被遵循,当前页面被刷新。

So, I have tried to implement the methods from Anthony/BalusC in this question: How to get selected row index in JSF datatable? but to no avail. I also went through @BalusC's "Using datatables" article (which is wonderful as always) but that was written for JSF1.2 and the 2.0 article doesn't really address getting the selected row data. When a user clicks the "Add to my favorites" button, the selected row is not getting passed to the backing bean, the navigation case is not being followed, and the current page is refreshed.

任何想法我在这里做错什么?

Any ideas what I'm doing wrong here?

这是我的支持bean:

Here is my backing bean:

    @ManagedBean(name = "selectedBean")
@RequestScoped
public class SelectedBeerBean 
{
    private List<Beer> favoriteBeers;
    private Beer selectedBeer;
    private HtmlDataTable datatableBeers;

    public HtmlDataTable getDatatableBeers() {
        return datatableBeers;
    }

    public void setDatatableBeers(HtmlDataTable datatableBeers) {
        this.datatableBeers = datatableBeers;
    }

    public String addBeer()
    {
        selectedBeer = (Beer) datatableBeers.getRowData();

        return "selectedBeer";
    }

    public List<Beer> getFavoriteBeers() {
        return favoriteBeers;
    }

    public void setFavoriteBeers(List<Beer> favoriteBeers) {
        this.favoriteBeers = favoriteBeers;
    }

    public Beer getSelectedBeer() {
        return selectedBeer;
    }

    public void setSelectedBeer(Beer selectedBeer) {
        this.selectedBeer = selectedBeer;
    }

}

这是我的xhtml页面: / p>

Here is my xhtml page:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Welcome to Draft Savvy, #{draftSavvyController.name}</title>        
    </h:head>
    <h:form>
    <h:body style="background-image: url(Background-Wood.png); ">
        <h3>You searched for "#{draftSavvyController.searchTerm}"</h3>
        <h4>Here are your beers</h4>

        <h:dataTable binding="#{selectedBean.datatableBeers}" value="#{draftSavvyController.beerList}" var="beer" border="1">
           <h:column>
               <f:facet name="header">
                   <h:outputText value="Logo"/>
               </f:facet>
                   <h:graphicImage url="#{beer.icon}"/>            
           </h:column>
           <h:column>
               <f:facet name="header">
                   <h:outputText value="Beer Name"/>
               </f:facet>
                   <h:outputText value="#{beer.name}"/>
           </h:column>        
           <h:column>
               <f:facet name="header">
                   <h:outputText value="Description"/>
               </f:facet>
                   <h:outputText value="#{beer.description}"/>
           </h:column>
           <h:column>
               <f:facet name="header">
                   <h:outputText value="Beer ID"/>
               </f:facet>
                   <h:outputLabel value="#{beer.id}" />
           </h:column>
           <h:column>
               <f:facet name="header">
                   <h:outputText value="Add To My Favorites"/>
               </f:facet>
               <h:commandButton value="Add Beer" action="#{selectedBean.addBeer}">
                   <f:setPropertyActionListener target="#{selectedBean.selectedBeer}" value="#{beer}" />
               </h:commandButton>            
           </h:column>
         </h:dataTable>

    </h:body>
    </h:form>
</html>


推荐答案


2.0文章并没有真正解决选择的行数据

the 2.0 article doesn't really address getting the selected row data

这样做。也许你看起来不够紧密,但它显示了两种获取所选行数据的方式。看看支持bean的 edit() delete()方法。 第一路通过 DataModel#getRowData()第二种方式是通过使用新的EL 2.2功能将其直接传递到操作方法中。

It does. Perhaps you're not looking closely enough, but it shows two ways of getting the selected row data. Look at the edit() and delete() methods of the backing bean. The first way does it by DataModel#getRowData()and the second way does it by just passing it straight into action method using the new EL 2.2 feature.


当用户点击添加到我的最爱按钮时,所选行不会传递给支持bean,导航案例没有被遵循,当前页面被刷新。

< h:dataTable> 的$ c>与初始请求(其中具有该表的页面)已被显示时的不完全相同。如果bean被请求作用域和/或< h:dataTable> 取决于请求参数。将bean放在视图范围中,和/或确保您准备在bean的(post)构造函数中准确完全相同的可以修复它。使用视图范围时,您应该 < h:dataTable> 绑定 c> to the bean。

That will happen when the value of the <h:dataTable> isn't exactly the same as it was during the initial request wherein the page with the table is been displayed. That will in turn happen if the bean is request scoped and/or the value of the <h:dataTable> depends on a request parameter. Placing the bean in the view scope and/or making sure that you prepare exactly the same value in the (post)constructor of the bean should fix it. When using the view scope, you should remove the binding of the <h:dataTable> to the bean.

在你的具体情况下,导航到一个不同的视图,可能还有另一个,更好, 办法。具体的功能要求并不清楚。这是一个确认页吗?而是使用GET。还是只是一个成功的行动登陆页面?而是使用POST-Redirect-GET。另请参见 JSF 2.0中的通讯 - 处理GET请求参数

In your particular case, with navigation to a different view involved, there's perhaps another, better, way. The concrete functional requirement is not exactly clear. Is it kind of a confirmation page? Rather use GET then. Or is it just a landing page after a successful action? Rather use POST-Redirect-GET. For another hints, see also Communication in JSF 2.0 - Processing GET request parameters.

这篇关于在JSF中从Datatable获取选定的行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆