【问题标题】:org.apache.jasper.JasperException: An exception occurred processing [WEB-INF/views/person.jsp] at line [24]org.apache.jasper.JasperException:在第 [24] 行处理 [WEB-INF/views/person.jsp] 时发生异常
【发布时间】:2019-11-03 13:32:29
【问题描述】:

尝试从https://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial 运行代码。添加功能并列出作品。但更新数据会导致 JSP 错误。 下面是错误日志 org.apache.jasper.JasperException:在第 [24] 行处理 [WEB-INF/views/person.jsp] 时发生异常

        22: <form:form action="${addAction}" commandName="person">
        23: <table>
        24:     <c:if test="${!empty person.name}">
        25:     <tr>
        26:         <td>
        27:             <form:label path="id">


    Root Cause
        java.lang.NumberFormatException: For input string: "name"
        java.lang.NumberFormatException.forInputString(Unknown Source)
        java.lang.Integer.parseInt(Unknown Source)

我在 JSP 方面没有太多经验。所以无法确定特定点,尝试了许多解决方案,并认为似乎是在将输入传递给字段名称。

我的 SQL 表名为 Person,如下所示

id      int(11)     PRIMARY Key auto_increment
name    varchar(20)             
country varchar(20)     

以下是我的表单的完整 JSP 代码。

<form:form action="${addAction}" commandName="person">
<table>
<c:if test="${!empty person.name}">
<tr>
    <td>
        <form:label path="id">
            <spring:message text="ID"/>
        </form:label>
    </td>
    <td>
        <form:input path="id" readonly="true" size="8"  disabled="true" />
        <form:hidden path="id" />
    </td> 
</tr>
</c:if>
<tr>
    <td>
        <form:label path="name">
            <spring:message text="Name"/>
        </form:label>
    </td>
    <td>
        <form:input path="name" />
    </td> 
</tr>
<tr>
    <td>
        <form:label path="country">
            <spring:message text="Country"/>
        </form:label>
    </td>
    <td>
        <form:input path="country" />
    </td>
</tr>
<tr>
    <td colspan="2">
        <c:if test="${!empty person.name}">
            <input type="submit"
                value="<spring:message text="Edit Person"/>" />
        </c:if>
        <c:if test="${empty person.name}">
            <input type="submit"
                value="<spring:message text="Add Person"/>" />
        </c:if>
    </td>
</tr>


【问题讨论】:

    标签: spring spring-mvc jsp


    【解决方案1】:

    看起来您发送的是 Object[] 而不是 Person 对象。

    在此处阅读更多信息Getting NumberFormatException while getting the value in JSP using JSTL

    要修复它,在将属性添加到请求之前,请确保您传递了正确的对象类型(Person)

    final TypedQuery<Person> query = entityManager.createQuery("SELECT person.name from Person person", Person.class);
    final List<Person> resultList = query.getResultList();
    Person person = resultList.get(0);
    
    map.addAttribute("person", person);
    

    如果这没有帮助,请发布您的后端代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2013-08-09
      相关资源
      最近更新 更多