【问题标题】:java.lang.NumberFormatException in JSF pageJSF 页面中的 java.lang.NumberFormatException
【发布时间】:2012-07-14 13:28:06
【问题描述】:

我有一个想要在 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)

似乎我无法在选择菜单中显示整数。 我该如何解决这个问题?

【问题讨论】:

    标签: java jsf jsf-2


    【解决方案1】:

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

    你的具体问题是因为你使用造成的

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

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

    这是正确的用法:

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

    另见:

    【讨论】:

      【解决方案2】:

      它试图将groupIdint groupId bean 字段绑定,这是无效的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        • 2015-12-19
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        相关资源
        最近更新 更多