PrimeFaces DataTable没有显示任何内容,因为根本没有调用支持bean的方法

时间:2018-03-31 11:09:11

标签: primefaces jsf-2 jboss

我遇到类似于此处描述的问题:

primefaces datatable not displaying anything

PrimeFaces DataTable "No records found" when there are records

我的PrimeFaces数据表没有显示任何结果,尽管数据库中有记录。而且:当我从浏览器请求页面时,似乎调用NO JAVA方法 - 至少支持bean的init()方法似乎根本不被调用

问题出在哪里?我将不胜感激任何帮助!

谢谢!

我的模板:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <meta charset="UTF-8"/> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>Bibliothek - Customers </title>  
</h:head>
<h:body>
    <h:form id="customersform">
        <p:growl id="growl" life="2000" />

        <p:panel header="All customers in the application" />

        <p:dataTable id="customersTable" var="customer"
            value="#{kundenBean.dataModel}" paginator="true" rows="10"
            paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
            resizableColumns="true" rowsPerPageTemplate="5,10,15"
            selection="#{kundenBean.selektierterKunde}" selectionMode="single"
            rowKey="#{customer.id}" lazy="true">

            <p:column headerText="Customer's Name" style="width:332px;text-align:center"
                sortBy="#{customer.vorname}" filterBy="#{customer.vorname}">
                <h:outputText value="#{customer.vorname}" />
            </p:column>
            <p:column headerText="Customer's Address" sortBy="#{customer.adresse}"
                filterBy="#{customer.adresse}">
                <h:outputText value="#{customer.adresse}" />
            </p:column>
        </p:dataTable>

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

支持Bean:

@Named
@ViewScoped
public class KundenBean {

    private Logger logger = Logger.getLogger(KundenBean.class.getName());
    public static final String SELECTED_CUSTOMER = "selectedCustomer";

    @Inject
    private KundeDBService kundenDBService;

    private LazyKundeDataModel dataModel;

    private Kunde selektierterKunde;
    private int selectedCustId;


    @PostConstruct
    private void init() {
        this.dataModel = new LazyKundeDataModel(kundenDBService);
    }


    public LazyKundeDataModel getDataModel() {
        return dataModel;
    }

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

    public Kunde getSelektierterKunde() {
        return selektierterKunde;
    }

    public int getSelectedCustId() {
        return selectedCustId;
    }

    public void setSelektierterKunde(Kunde selektierterKunde) {
        this.selektierterKunde = selektierterKunde;
    }

    public void onRowSelect(SelectEvent event) {
        selectedCustId = ((Kunde) event.getObject()).getId();
    }

    public void onRowDblClick(final SelectEvent event) {
        selectedCustId = ((Kunde) event.getObject()).getId();

        try {
            ExternalContext extCtx = FacesContext.getCurrentInstance()
                    .getExternalContext();

            extCtx.getFlash().put(SELECTED_CUSTOMER,((Kunde) event.getObject()));

            FacesContext.getCurrentInstance().getExternalContext().redirect("rents.xhtml?custId=" +selectedCustId);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

    }
}

数据模型:

public class LazyKundeDataModel extends LazyDataModel<Kunde> {

    private static final long serialVersionUID = 1L;

    private KundeDBService kundeDBService;

    public LazyKundeDataModel(KundeDBService kundeDBService) {
        this.kundeDBService = kundeDBService;
    }

    @Override
    public Kunde getRowData(String rowKey) {
        return kundeDBService.getKundeById(Integer.valueOf(rowKey));
    }

    @Override
    public Object getRowKey(Kunde kunde) {
        return kunde.getId();
    }

    @Override
    public List<Kunde> load(int first, int pageSize, String sortField, SortOrder sortOrder,
            Map<String, Object> filters) {


        List result = this.kundeDBService.load(first, pageSize, sortField, sortOrder, filters);

        // rowCount
        this.setRowCount(result.size());
        this.setPageSize(pageSize);

        return result;
    }
}

数据库服务:

@Stateless
public class KundeDBService {

    @PersistenceContext
    private EntityManager em;

    public static final String JOINING_OR = " OR ";
    public static final String JOINING_AND = " AND ";
    public static final String CONDITION_WHERE = " WHERE ";

    private static Logger logger = Logger.getLogger(KundeDBService.class.getName());

    public List<? extends Kunde> load(int first, int pageSize, String sortField, SortOrder sortOrder,
            Map<String, Object> filters) {

        List<? extends Kunde> result = new ArrayList<Kunde>();

        String selectQUERY = " select k from Kunde k ";

        if (filters.size() > 0) {
            selectQUERY += addFiltersToQuery(filters);
        }

        if (sortField != null) {
            selectQUERY += " order by k." + sortField;

            if (sortOrder != null) {
                selectQUERY += " " + (sortOrder);
            }
        }else { // sortField == null, default sort by customer's name

            selectQUERY += " order by k.vorname asc";

        }

        Query emQuery = em.createQuery(selectQUERY);

        emQuery.setFirstResult(first);
        emQuery.setMaxResults(pageSize);

        result = (List<Kunde>) emQuery.getResultList();
        return result;
    }

    private String addFiltersToQuery(Map<String, Object> filters) {

        if (filters.size() == 0)
            return StringUtils.EMPTY;

        StringBuilder whereCondition = new StringBuilder(CONDITION_WHERE);

        int pseudoCounter = 0;
        for (Entry<String, Object> entry : filters.entrySet()) {
            whereCondition.append("k." + entry.getKey() + " LIKE :" + "x" + (++pseudoCounter));
            whereCondition.append(JOINING_AND);
        }

        if (whereCondition.length() > CONDITION_WHERE.length()) {
            whereCondition.delete(whereCondition.length() - JOINING_AND.length(), whereCondition.length());
        }

        return whereCondition.toString();
    }

    public Kunde getKundeById(int id) {

        return em.find(Kunde.class, Integer.valueOf(id));

    }
}

1 个答案:

答案 0 :(得分:0)

我通过在KundenBean类上用注释javax.faces.bean.ManagedBean替换注释@Named来解决问题。我不确定究竟是什么问题,但我认为这与此处列出的问题有关

JSF View Scoped Bean Reconstructed Multiple Times

所以,现在改变和工作的KundenBean类看起来像这样:

@ManagedBean (name = "kundenBean")
@ViewScoped
public class KundenBean {

//rest of the lines unchanged