primefaces - 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:
托管 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();
}
}
解决方案
您似乎想使用“高级” 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>
我确定已经渲染了一些东西。
推荐阅读
- amazon-web-services - 由 DynamoDB 流触发的 AWS Lambda 函数的输出如何处理?
- sql-server - 如何将 Angular 2 应用程序中的 json 数据发布到本地 asp.net mvc webapi 服务?
- java - 找不到资源java
- node.js - 长请求 uri 在调用响应对象的 redirect() 函数时被截断
- sql - SQL Server 2008 - 从子查询连接组
- cobol - 在运行时显示 loadlib
- sql - 如何使用表中的触发器更新特定的列和行
- mysql - 更新内部连接中的两个表会产生奇怪的匹配行数
- javascript - 我想通过使用单选按钮/下拉菜单/列表/等来更改值
- c# - 使单元测试的方法可测试c#