selectonemenu中的所选项始终为null

时间:2013-12-03 14:25:29

标签: jsf-2 primefaces selectonemenu

我正在尝试使用<p:dataTable>来显示从<p:selectOneMenu>列表中选择的对象的内容。

<p:selectOneMenu>已正确填充选项。但是:

  1. 当我从<p:dataTable>
  2. 中选择一个项目时,<p:selectOneMenu>未更新
  3. 服务器日志表明#{MB.selectedDatabase}的值为null
  4. 永远不会调用
  5. #{MB.setSelectedDatabase()}
  6. 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;
     }
    

1 个答案:

答案 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"> 

另见

BalusC SelectOneMenu tutorial