代码之家  ›  专栏  ›  技术社区  ›  Mike

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

  •  0
  • Mike  · 技术社区  · 7 年前

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

    我反复检查,itemValue指向其中包含数据的company.name。此外,itemLabel获取公司的唯一符号,因此也可以,因为这些数据都是有效的。然而,我遗漏了一些东西,因为UI显示一个空白框,明显高于其默认的空框——正如我所说,在chrome的调试器中以空div的形式包含3000个空项。

    下面是我的列表框中的SelectOneXHTML代码。我在托管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>                                   
    

    Pic of chrome debugger

    托管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();
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Kukeltje    7 年前

    您似乎想使用“高级”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:selectOneListbox f:selectItems 具有一个var属性,其中 p:selectOneListbox p:column

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

    我肯定有东西被渲染了。