我正在尝试使用<p:dataTable>
来显示从<p:selectOneMenu>
列表中选择的对象的内容。
<p:selectOneMenu>
已正确填充选项。但是:
<p:dataTable>
<p:selectOneMenu>
未更新
#{MB.selectedDatabase}
的值为null
。 #{MB.setSelectedDatabase()}
。 XHTML:
<h:form id="form">
<h:messages id="messages" />
<p:panelGrid columns="2">
<p:outputLabel value="Data:" />
<p:selectOneMenu immediate="true" id="customDatas" value="#{MB.selectedDatabase}" >
<p:ajax execute="customDatas" render="infos" listener="#{MB.whatIsSelected()}" />
<f:selectItem itemLabel="Select Data" itemValue="" />
<f:selectItems value="#{MB.datas}" var="data" itemLabel="#{data.name}" itemValue="#{data.id}"/>
</p:selectOneMenu>
</p:panelGrid>
<p:dataTable value="#{MB.selectedDatabase.infos}" var="item" id="infos" >
<p:column >
<f:facet name="header">
<h:outputText value="Info"/>
</f:facet>
<h:outputText value="#{item.info}"/>
</p:column>
</p:dataTable>
</h:form>
我的sessionScoped Bean的部分代码:
public Data selectedDatabase;
public void whatIsSelected(){
logger.log(Level.SEVERE, "Selected data is : {0}", selectedDatabase.getName());
infoBusinessLocal.getinfosbydata(selectedDatabase);
}
public Data getSelectedDatabase() {
return selectedDatabase;
}
public void setSelectedDatabase(Data selectedDatabase) {
logger.log(Level.SEVERE, "Set selectedDB is invoked");
this.selectedDatabase = selectedDatabase;
}
答案 0 :(得分:4)
使用Pojo类的SelectOneMenu需要页面和托管bean之间的JSf转换器。 Converter将字符串从页面转换为托管bean中的对象。
@FacesConverter("dataConverter")
public class DataConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
//return proper object, value is the string on the page
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
//return the string value of object which will be presented on the page
}
}
将转换器属性添加到SelectOneMenu
<p:selectOneMenu immediate="true" id="customDatas" value="#{MB.selectedDatabase}" converter="dataConverter">
另见