java.lang.NumberFormatException:对于输入字符串:“id”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21966870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NumberFormatException: For input string: "id"
提问by user3109487
I am using SPRING MVC to develop a project to display a list of users in a JSP file. My Controller file has:
我正在使用 SPRING MVC 开发一个项目,以在 JSP 文件中显示用户列表。我的控制器文件有:
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", userService.getUser()); //userService.getUser() returns a List
The JSP file has:
JSP 文件有:
<c:if test="${!empty user}">
<table>
<tr>
<td>User Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>Gender</td>
<td>City</td>
</tr>
<c:forEach items="${user}" var="user">
<tr>
<td><c:out value="${user.id}"/></td>
<td><c:out value="${user.firstName}"/></td>
<td><c:out value="${user.lastName}"/></td>
<td><c:out value="${user.gender}"/></td>
<td><c:out value="${user.city}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
When displaying the above JSP file, java.lang.NumberFormatException: For input string: "id" is shown. Can anybody please help to find out the solution?
显示上述 JSP 文件时,显示 java.lang.NumberFormatException: For input string: "id"。有人可以帮忙找出解决方案吗?
Thank you very much.
非常感谢。
回答by helderdarocha
NumberFormatException
happens during an failed attempt to convert a string to a number. Check if any of your input data that should be a number is actually receiving something different (ex: an empty string, which can't be converted to a number, is a possible cause).
NumberFormatException
在尝试将字符串转换为数字失败时发生。检查您的任何应该是数字的输入数据是否实际上收到了不同的东西(例如:一个无法转换为数字的空字符串是一个可能的原因)。
回答by Damian S.
First good rule: variables containing lists you schould naming plural for example, list of users put name "users":
第一个好规则:包含您应该命名为复数的列表的变量,例如,用户列表输入名称“users”:
<c:forEach items="${users}" var="user">
Now you know what variable contain, "users" have list of users but "user" is one item of users list.
现在你知道什么变量包含,“用户”有用户列表,但“用户”是用户列表的一项。
Second rule: if you want check, whether a variable is a number, do not catch exceptions, it's slow. Better and faster is regex:
第二条规则:如果你想检查一个变量是否是一个数字,不要捕捉异常,它很慢。更好更快的是正则表达式:
if (anyString.matches("\d+")){
isANumber();
}
else{
notNumber();
}
回答by Somaiah Kumbera
In addition to heldrarocha and Damien's answers, I think your problem is that you are not checking ID before adding it into the User object.
除了heldrarocha 和Damien 的答案之外,我认为您的问题是在将 ID 添加到 User 对象之前您没有检查 ID。
Ensure that the id variable in User is an integer (or long or whatever). This way if you fill it from the DB, your rowmapper will add numeric values.
确保 User 中的 id 变量是一个整数(或 long 或其他)。这样,如果您从数据库填充它,您的行映射器将添加数值。
If it HAS to be a String, ensure that whatever goes in is numeric.
如果它必须是一个字符串,请确保输入的是数字。
An easy way to check if a String is numeric is to user Apache Commons StringUtils isNumeric method:
检查字符串是否为数字的一种简单方法是使用 Apache Commons StringUtils isNumeric 方法: