Ryan Andriawan
Ryan Andriawan

Reputation: 21

JSF - dataTable binding causing ERROR (can't convert)

I'm new in JSF. I need to retrieve rowData from dataTable.

Here is my code:

       <h:form>
            <h1><h:outputText value="List"/></h1>
            <p:dataTable value="#{accountBacking.dataModel}" 
                         binding="#{accountBacking.htmlTable}" 
                         var="item">
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Id"/>
                    </f:facet>
                    <h:outputText value="#{item.id}"/>
                </p:column>
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Action"/>
                    </f:facet>
                    <p:commandButton value="Submit" action="#{accountBacking.destination()}/>
                </p:column>
           </p:dataTable>
        </h:form>

Bean :

import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean (name = "accountBacking")
@ViewScoped
public class AccountBacking implements Serializable {

private List<Account> daftarAccount;
private DataModel dataModel;
private HtmlDataTable htmlTable;
private AccountFacade service;
private Account account;

@PostConstruct
public void init()
{

}

public DataModel getDataModel() {
    dataModel = new ListDataModel();
    daftarAccount = service.findAll();
    if(daftarAccount != null && daftarAccount.size()>0)
        dataModel.setWrappedData(daftarAccount);

    return dataModel;
}

public void setDataModel(DataModel dataModel) {
    this.dataModel = dataModel;
}


public HtmlDataTable getHtmlTable() {
    return htmlTable;
}

public void setHtmlTable(HtmlDataTable htmlTable) {
    this.htmlTable = htmlTable;
}

public void destination(){
   acount = (Account)htmlTable.getRowData();
}

However, the binding tag inside h:dataTable cause an error, it says:

Cannot convert org.primefaces.component.datatable.DataTable@4f5b348a of type class org.primefaces.component.datatable.DataTable to class javax.faces.component.html.HtmlDataTable

Why is that? And how to fix it so that I can retrieve rowData from the table?

Thanks in advance!

Upvotes: 2

Views: 1012

Answers (1)

Jaumzera
Jaumzera

Reputation: 2359

This is happening 'cos org.primefaces.component.datatable.DataTable doesn't extend (IS NOT A) class javax.faces.component.html.HtmlDataTable.

This is Primeface's Datable class hierarchy

Class DataTable

java.lang.Object
    javax.faces.component.UIComponent
        javax.faces.component.UIComponentBase
          javax.faces.component.UIData
              org.primefaces.component.api.UIData
                  org.primefaces.component.datatable.DataTable

Change

private HtmlDataTable htmlTable;

to

private DataTable htmlTable; // also change the import statement

and your code will work.

Upvotes: 1

Related Questions