2

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?

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

}

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>
Community
  • 1
  • 1
airowe
  • 794
  • 2
  • 9
  • 29

1 Answers1

3

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

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.


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.

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.


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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC, sorry to have misinterpreted your post. My eyes were bleeding at the time of my question. To your question re: functionality, this is not a confirmation page. Rather, it's a page displaying data in a data table that was received via an API call to a remote database. I am about to try to remove the binding and change the backing bean to a View Scoped. Thanks for all your help so far. Your articles have been a big help. – airowe Aug 14 '12 at 01:16
  • BalusC, ever since I started to get these warnings, I haven't been able to keep scope from page to page regardless of what my beans are scoped. "WARNING: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@a40f0e]. WARNING: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@a40f0e] from index [0] WARNING: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@1bff5c9] from index [1]" Could this be causing my issues? – airowe Aug 14 '12 at 03:25
  • I'm going to ask a new question here and accept your original answer. If you can provide further insight related to this question, please do. Thanks again! – airowe Aug 14 '12 at 03:35