【发布时间】:2014-04-27 17:54:03
【问题描述】:
我在 SelectOneMenu 中显示自定义内容时遇到了问题。我现在正在使用 Primefaces 3.5,我测试了展示中的示例,它工作正常,所以不幸的是问题是 PEBCAC。
这是我的转换器代码:
@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String id)
{
groups = getGroups();//gets the groups here
pType toReturn = new pType();
if(groups.size()>0){
toReturn = groups.get(0);
return toReturn;
}
return "";
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{
if (o == null || o.equals(""))
{
return "---";
} else
{
try{
pType val = (pType)o;
return String.valueOf(val.getRecordID());
}catch(Exception ex){
ex.printStackTrace();
return "---";
}
}
}
这是我现在正在使用的 xhtml:
<p:selectOneMenu value="#{controller.selectedVal}"
rendered="#{controller.showMenu}" effect="fade"
converter="converter" panelStyle="width:150px" var="p"
style="width:160px" filter="true" filterMatchMode="contains">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems var="pType"
itemLabel="#{controller.getNumber(pType)}"
itemValue="#{pType}" value="#{controller.savedValues}">
<p:column>#{p.var1} - #{p.var2}
</p:column>
</f:selectItems>
</p:selectOneMenu>
控制器是 SessionScoped 的 managedbean。变量 savedValues 是一个对象列表,它确实填充了正确的数据,并且在调用时具有数据。现在使用上面的代码,列表刚刚打开,里面没有数据。如果我将 var 切换为“pType”而不是“p”,下拉菜单将包含从 controller.getNumber(pType) 获取的值的列表,尽管菜单中没有加载自定义数据。但是我看到的所有示例都表明使用的变量来自 selectOneMenu,这也是我有点困惑的地方,因为它似乎应该总是来自 selectItems 的 value 字段。
【问题讨论】:
标签: jsf primefaces