首页 > 解决方案 > 从托管 bean 中的 ADF SelectOneChoice 获取选定项目

问题描述

我目前正在使用 Jdev v12.2.1.4.0 (Oracle 12c) 修改 ADF Fusion Web 应用程序。

在其中一个 jsf 页面上,我在表格列中有一个 SelectOneChoice。jsf 实现如下所示:

    <af:column 
        headerText="#{ManagedBean.column5HeaderText}"
        sortable="false"  
        visible="true" 
        id="c5">
        <af:selectOneChoice 
            binding="#{ManagedBean.bindingErrorCaseSelectOneChoice}"
            label="error case" 
            unselectedLabel="---" 
            autoSubmit="true"
            id="soc1">
            <f:selectItems value="#{ManagedBean.errorCases}" id="si1"/>
        </af:selectOneChoice>
    </af:column>

我省略了该required属性,因为该过程没有必要在这里选择一个值。我的 ManagedBean.java 的连贯部分如下:

    //declaring
    private RichSelectOneChoice bindingErrorCasesSelectOneChoice;
    private  List<SelectItem> errorCases = new ArrayList<SelectItem>();

    //...

    //populating errorCases List from a database
    public void getErrorCasesFromDB() {
    errorCases= new ArrayList<SelectItem>();
    
    try {
        //HC is a helper class to connect to a specific database
        Conection conn = HC.getConn();
        PreparedStatement pstmt = conn.prepareStatement("some SQL");
        ResultSet rs = pstmt.executeQuery();
        
        while (rs.next()) {
            errorCases.add(new SelectItem("i"+ rs.getRow(), rs.getString(1)));
        }
        conn.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

当我运行 jsf 页面时,表内的 SelectOneChoices 会被渲染,并且所有预期的项目都被登记。每当我尝试访问 SelectOneChoice 的选定项目时,我都会遇到问题。

当我点击页面上的按钮时,我想读取 selectedItem 的值,所以我想我可以省去在 valueChangeListener 中处理它,并在我的按钮操作中执行以下操作:

    public void buttonSaveReceivedResults(ActionEvent actionEvent) {
        //...
        if (bindingErrorCaseSelectOneChoice.getValue != null) {
            //... insert the selected value into an SQL statement
            //in the case the unselected label is selected, skip
            System.out.println(bindingErrorCasesSelectOneChoice.getValue().toString())
        }
    }

这个块总是被跳过。此外,当我检查进程时,getValue()调用总是返回 null,即使我从列表中选择了一个项目。现在我问你们,链条中缺少的部分在哪里?我是否正确地进行了数据绑定。我是否以错误的方式访问元素?提前致谢。

标签: javaoraclejdeveloper

解决方案


value 属性存储 af:selectOneChoice 组件的输出。由于您尚未添加它,因此返回的值为 null。


推荐阅读