gpt4 book ai didi

JSF 页面中的 java.lang.NumberFormatException

转载 作者:行者123 更新时间:2023-11-29 07:13:46 25 4
gpt4 key购买 nike

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

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

最佳答案

对于 <f:selectItems>标记,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}" />

另见:

关于JSF 页面中的 java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com