简体   繁体   English

java.lang.NumberFormatException:对于输入字符串:“id”

[英]java.lang.NumberFormatException: For input string: "id"

I am using SPRING MVC to develop a project to display a list of users in a JSP file.我正在使用 SPRING MVC 开发一个项目以在 JSP 文件中显示用户列表。 My Controller file has:我的控制器文件有:

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.显示上述 JSP 文件时,显示 java.lang.NumberFormatException: For input string: "id"。 Can anybody please help to find out the solution?有人可以帮忙找出解决方案吗?

Thank you very much.非常感谢。

NumberFormatException happens during an failed attempt to convert a string to a number. NumberFormatException在尝试将字符串转换为数字失败期间发生。 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).检查您的任何应该是数字的输入数据是否实际上正在接收不同的东西(例如:一个无法转换为数字的空字符串是可能的原因)。

First good rule: variables containing lists you schould naming plural for example, list of users put name "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.现在您知道包含什么变量了,“users”有用户列表,但“user”是用户列表中的一项。

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

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.除了 Holdrarocha 和 Damien 的回答之外,我认为您的问题是您在将 ID 添加到 User 对象之前没有检查它。

Ensure that the id variable in User is an integer (or long or whatever).确保 User 中的 id 变量是整数(或 long 或其他)。 This way if you fill it from the DB, your rowmapper will add numeric values.这样,如果您从数据库中填充它,您的行映射器将添加数值。

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 方法:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isNumeric(java.lang.String) http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isNumeric(java.lang.String)

I'm not sure your problem is, but I remember to put below code at the beginning of the jsp page我不确定你的问题是什么,但我记得在jsp页面的开头放下面的代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Since you made sure that userService.getUser() returns a list that can be iterated over using forEach in jsp, the only change i would think of is由于您确保 userService.getUser() 返回一个可以在 jsp 中使用 forEach 进行迭代的列表,所以我想到的唯一更改是

model.put("users", userService.getUser())

and in jsp file as follows:在jsp文件中如下:

<c:forEach items="${users}" var="user">

It may eliminate ambiguity and may actually solve your problem.它可以消除歧义,实际上可以解决您的问题。

I came accross same problem today and later realised that I was iterating over the object itself when I intend to iterate over a list of such objects.我今天遇到了同样的问题,后来意识到当我打算迭代这些对象的列表时,我正在迭代对象本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM