javaoraclejdeveloper

Getting selected item from an ADF SelectOneChoice in managed bean


I am currently reworking an ADF Fusion Web application using Jdev v12.2.1.4.0 (Oracle 12c).

On one of the jsf pages I have a SelectOneChoice inside a table column. The jsf-implementation looks like this:

    <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>

I left out the required attribute because it is not necessary for the process to select a value here. The coherent parts of my ManagedBean.java are as following:

    //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();
    }
}

As I run the jsf page the SelectOneChoices inside the table get rendered and all the expected items are enlisted. I am facing a problem whenever i try to access the selected item of the SelectOneChoice.

I want to read the value of the selectedItem when I hit a button on the page, so I figured I could leave out having to deal with that in a valueChangeListener, and did the following in my button action:

    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())
        }
    }

This block always gets skipped. Also when i inspected the process, the getValue() call always returned null, even if i select an item from the list. Now i'm asking you guys, where is the missing part in the chain? Did I do the data bindings correctly. Do I access the elements in the wrong way? Thanks in advance.


Solution

  • The value attribute stores the output of af:selectOneChoice component. Since you have not added it, value returned was null.