首页 > 解决方案 > Primefaces selectOneListbox不显示项目文本(项目中也不包含数据)

问题描述

我的 PrimeFaces selectOneListbox 中有 3000 个项目 - 从 chrome 调试器中的对象检查器可以看出。div 行都在那里,但其中没有用于显示文本的数据。div 都是空的。

我仔细检查了 itemValue 指向其中包含数据的 company.name 。此外,itemLabel 获得了公司的唯一符号,所以这也应该没问题,因为这些数据都是有效的。但是,我错过了一些东西,因为 UI 显示了一个空白框,并且明显高于其默认的空值 - 正如我所说,它包含 3000 个空项目,以 chrome 的调试器中的空 div 的形式出现。

我的 xhtml 中 selectOneListbox 的代码如下。我已经在我的托管 bean 中尝试了将地图作为集合和列表(因此我将值更改为我想尝试的任何变量)。地图和公司列表都产生了相同的结果 - javascript 中包含 3000 个条目的列表(如下所示)

<p:selectOneListbox id="symbolPicker" value="#{simulationBean.company}" converter="companyConverter" var="t" filter="true" filterMatchMode="contains">
    <f:selectItems value="#{simulationBean.companyMap}" var="company" itemLabel="#{company.symbol}" itemValue="#{company}" />
</p:selectOneListbox>                                   

空项目的javascript:

chrome调试器的图片

托管 bean 仅存储所有显然已初始化 @PostConstruct 的公司列表,因为在我的调试器中正在枚举对象(其中 3000 个) - 所以公司在那里并且它们的数据是有效的(我可以在调试器)。

我猜罪魁祸首将在下一个对象中。我无法想象我错过了什么 - 似乎它必须非常明显 - 这是公司实体本身:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.google.gson.JsonObject;

@Entity
@Table(name = "company")
public class Company implements Serializable {

    private static final long serialVersionUID = 1L;

    private String symbol;

    @Column
    private String name;

    @Column(columnDefinition="market_category")
    private String marketCategory;

    @Column(columnDefinition="test_issue")
    private Integer testIssue;

    @Column(columnDefinition="good_status")
    private Integer goodStatus;

    @Column(columnDefinition="round_lot")
    private Integer roundLot;

    @Column
    private Integer etf;

    public Company() {}

    public Company(String symbol, String name, String marketCategory, Integer testIssue, Integer goodStatus, Integer roundLot, Integer etf) {
        super();
        this.symbol = symbol;
        this.name = name;
        this.marketCategory = marketCategory;
        this.testIssue = testIssue;
        this.goodStatus = goodStatus;
        this.roundLot = roundLot;
        this.etf = etf;
    }

    @Id
    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMarketCategory() {
        return marketCategory;
    }

    public void setMarketCategory(String marketCategory) {
        this.marketCategory = marketCategory;
    }

    public Integer getTestIssue() {
        return testIssue;
    }

    public void setTestIssue(Integer testIssue) {
        this.testIssue = testIssue;
    }

    public Integer getGoodStatus() {
        return goodStatus;
    }

    public void setGoodStatus(Integer goodStatus) {
        this.goodStatus = goodStatus;
    }

    public Integer getRoundLot() {
        return roundLot;
    }

    public void setRoundLot(Integer roundLot) {
        this.roundLot = roundLot;
    }

    public Integer getEtf() {
        return etf;
    }

    public void setEtf(Integer etf) {
        this.etf = etf;
    }

    @Override
    public String toString() {
        return symbol;
    }

    public String toDebugString() {
        return "Company [symbol=" + symbol + ", name=" + name + ", marketCategory=" + marketCategory + ", testIssue="
                + testIssue + ", goodStatus=" + goodStatus + ", roundLot=" + roundLot + ", etf=" + etf + "]";
    }

    @Transient
    public JsonObject getJsonObject() {

        JsonObject result = new JsonObject();

        result.addProperty("symbol", symbol);
        result.addProperty("name", name);
        result.addProperty("marketCategory", marketCategory);
        result.addProperty("testIssue", testIssue);
        result.addProperty("goodStatus", goodStatus);
        result.addProperty("roundLot", roundLot);
        result.addProperty("etf", etf);
        return result;
    }

    @Override
    public boolean equals(Object obj) {

        // null check
        if (obj == null) {
            return false;
        }

        // this instance check
        if (this == obj) {
            return true;
        }

        // instanceof Check and actual value check
        if ((obj instanceof Company) && (((Company) obj).getSymbol() == this.symbol)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {

        return symbol.hashCode();
    }
}

现在我也有一个转换器:

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.appzany.stockService.BeanUtil;
import com.appzany.stockService.entity.Company;
import com.appzany.stockService.stockservice.StockService;

@FacesConverter("companyConverter")
public class CompanyConverter implements Converter {

    private StockService stockService = null;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if(stockService == null)
            stockService = BeanUtil.getBean(StockService.class);
        return stockService.getCompany(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((Company)value).getSymbol();
    }
}

标签: primefaces

解决方案


您似乎想使用“高级” p:selectOneListbox 在他们的展示中如下

<p:selectOneListbox id="advanced" value="#{selectOneView.theme}" converter="themeConverter" var="t" filter="true" filterMatchMode="contains">

    <f:selectItems value="#{selectOneView.themes}" var="theme" itemLabel="#{theme.displayName}" itemValue="#{theme}" />

    <p:column>
        <h:graphicImage name="showcase/images/themeswitcher/themeswitcher-#{t.name}.png" alt="#{t.name}" styleClass="ui-theme" />
    </p:column>

    <p:column>
        <h:outputText value="#{t.displayName}" />
    </p:column>
</p:selectOneListbox>

其中 thep:selectOneListbox和 thef:selectItems都有一个 var 属性,该 var 的 var 属性p:selectOneListbox在该组件内的显式附加标签中使用。这些附加p:column标签及其内容负责“高级”渲染。你没有这些,我认为(对不起,没有尝试)会导致渲染“什么都没有”。如果你想添加一些东西来渲染

<p:column>
    <h:outputText value="#{t.name}" />
</p:column>

<p:column>
    <h:outputText value="#{t.symbol}" />
</p:column>

我确定已经渲染了一些东西。


推荐阅读