java.lang.NumberFormatException: For input string in JSP Page

后端 未结 1 663
面向向阳花
面向向阳花 2020-11-30 15:04

Hi can someone please help why this is giving an error when trying to display the values in a JSP page. I don\'t have any number been converted or String been converted to N

相关标签:
1条回答
  • 2020-11-30 16:00

    The ${userRecords} here

    ${userRecords.user_first_name}
    ${userRecords.user_middle_name}
    

    is a List<User>, however you're attempting to access it as if it's a single User. This is not valid. In EL, a List can only be accessed with an integer index, indicating the position of the list item you'd like to access, like so

    ${userRecords[0].user_first_name}
    ${userRecords[0].user_middle_name}
    

    The exception is also basically telling that it expected an integer value instead of a string value. If you look closer at the stack trace, you'll see that a ListELResolver is involved.

    However, your concrete problem is bigger. You should actually be iterating over the list. Use the JSTL <c:forEach> for that. E.g. (simplified from your odd code snippet):

    <table>
        <c:forEach items="${userRecords}" var="user">
            <tr>
                <td>${user.user_first_name}</td>
                <td>${user.user_middle_name}</td>
            </tr>
        </c:forEach>
    </table>
    

    (note: keep the XSS attack hole in mind if you really intend to redisplay them as input values)

    By the way, I'd work on your Java code conventions. Those underscores are really not Java-ish.

    0 讨论(0)
提交回复
热议问题