1
votes

I have a list of user groups which I want to display in JSF page:

<h:panelGroup>
    <h:selectOneMenu value="#{AddAccountController.formMap['GROUPID']}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{AddAccountController.usergroups.groupid}" itemValue="#{AddAccountController.usergroups.groupname}" />
    </h:selectOneMenu>
</h:panelGroup>

This is the managed bean code which generates the list:

private List<listGroupsObj> usergroups = new ArrayList<>();
......
public void initListGroups() throws SQLException {

        if (ds == null) {
            throw new SQLException("Can't get data source");
        }
        /* Initialize a connection to Oracle */
        Connection conn = ds.getConnection();

        if (conn == null) {
            throw new SQLException("Can't get database connection");
        }
        /* With SQL statement get all settings and values */
        PreparedStatement ps = conn.prepareStatement("SELECT * from USERGROUPS");

        try {
            //get data from database        
            ResultSet result = ps.executeQuery();
            while (result.next()) {
                /* Put the the data from Oracle into Hash Map */                
                usergroups.add(new listGroupsObj(result.getInt("GROUPID"), result.getString("GROUPNAME")));

            }
        } finally {
            ps.close();
            conn.close();
        }
    } 

    public class listGroupsObj {
        private int groupid;
        private String groupname;

        public listGroupsObj(int groupid, String groupname){
            this.groupid = groupid;
            this.groupname = groupname;           
        }

        public int getGroupid()
        {
            return groupid;
        }   

        public void setGroupid(int groupid)
        {
            this.groupid = groupid;
        }

        public String getGroupname()
        {
            return groupname;
        }

        public void setGroupname(String groupname)
        {
            this.groupname = groupname;
        }
        @Override
        public String toString()
        {
            return groupname;
        }
    }

    // Get the list with User Groups
    public List<listGroupsObj> getusergroups() {       
        return usergroups;
    }

When I open the JSF page I get this error:

java.lang.NumberFormatException: For input string: "groupid"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)

It seems that I cannot display integer into the select menu. How I can solve this problem?

2

2 Answers

5
votes

For the <f:selectItems> tag, the value attribute must represent the list of all available items. The itemValue (and itemLabel) attributes can only be used when the var attribute is specified, which represents each individual item of the list.

Your particular problem is caused because you used

<f:selectItems value="#{AddAccountController.usergroups.groupid}" />

which is syntactically invalid. The #{AddAccountController.usergroups} returns a List which can only be accessed further by an integer index like so #{AddAccountController.usergroups[0]} for the 1st item. But you tried to use groupid which isn't a valid index value as it's not an Integer. But you shouldn't be accessing it this way.

This is the proper usage:

<f:selectItems value="#{AddAccountController.usergroups}" var="usergroup"
    itemValue="#{usergroup.groupid}" itemLabel="#{usergroup.groupname}" />

See also:

1
votes

It tries to bind groupId with int groupId field of bean and that is invalid