有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAJSF页面中的lang.NumberFormatException

我有一个要在JSF页面中显示的用户组列表:

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

这是生成列表的托管bean代码:

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

打开JSF页面时,出现以下错误:

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)

似乎我无法在选择菜单中显示整数。 我怎样才能解决这个问题


共 (2) 个答案

  1. # 1 楼答案

    对于<f:selectItems>标记,value属性必须表示所有可用项的列表。只有在指定了var属性时才能使用itemValue(和itemLabel)属性,该属性表示列表中的每个单独项目

    您的特定问题是由于您使用了

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

    这在语法上是无效的。#{AddAccountController.usergroups}返回一个List,该List只能通过像so#{AddAccountController.usergroups[0]}这样的整数索引进一步访问第一项。但是您试图使用groupid,它不是有效的索引值,因为它不是Integer。但你不应该这样访问它

    这是正确的用法:

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

    另请参见:

  2. # 2 楼答案

    它试图将groupId与bean的int{}字段绑定,这是无效的