0

I am using SPRING MVC to develop a project to display a list of users in a JSP file. 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:

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

Thank you very much.

3
  • 1
    could you post the whole exception stack-trace? Commented Feb 23, 2014 at 10:11
  • 1
    items="${user}" var="user" which user is what.. Replacing by var="u" and using u in the foreach might help Commented Feb 23, 2014 at 10:13
  • I know this answer comes pretty late but I believe that your user variable within the foreach is actually a collection. Check that out. Commented Jul 24, 2014 at 16:54

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

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

Comments

0

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.

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.

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:

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

Comments

0

I'm not sure your problem is, but I remember to put below code at the beginning of the jsp page

<%@ 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" %>

1 Comment

Hi there! it'd be great if you could format your code using the code tags for better readability! thanks.
0

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

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

and in jsp file as follows:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.