Primefaces selectOneListbox 不显示项目文本(项目中也不包含数据)

Primefaces selectOneListbox not displaying item text (nor is the data contained in the items)

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

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

我的 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:

托管 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();
    }
}

您似乎想使用 'Advanced' p:selectOneListbox,their showcase 中的

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

其中 p:selectOneListboxf:selectItems 都有一个 var 属性,其中 p:selectOneListbox 的 var 属性正在该组件内的显式附加标记中使用。这些额外的 p:column 标签及其内容负责 'advanced' 渲染。你没有任何这些,我认为(抱歉,没有尝试)导致渲染 'nothing'。如果你想添加一些东西来渲染

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

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

我确定渲染了一些东西。