JSP页面中的参数在servlet中不可见

时间:2014-05-24 09:55:53

标签: java html jsp servlets

JSP页面代码:

<c:forEach items="${studentInfoList}" var="studentInfo">
    <tr>
        <td><a href="#">${studentInfo.student.surname} ${studentInfo.student.name} ${studentInfo.student.patronymic}</a></td>
        <td> ${studentInfo.group.name} </td>
        <td> ${studentInfo.semester.getYear()} </td>
        <td> ${studentInfo.speciality.title} </td>
        <td>
            <form name="changeStudent" method="POST" action="controller">
                <input type="hidden" name="command" value="openChangeStudentPage"/>
                <imput type="hidden" name="studentId" value="${studentInfo.student.id}"/>
                <input type="submit" value="Change"/>
            </form>
        </td>
    </tr>
</c:forEach>

在浏览器中生成的代码:

<tr>
     <td><a href="#">Surname Name Patr</a></td>
     <td> KV-01 </td>
     <td> 4 </td>
     <td> Computer eng. </td>
     <td>
         <form name="changeStudent" method="POST" action="controller">
             <input type="hidden" name="command" value="openChangeStudentPage"/>
             <imput type="hidden" name="studentId" value="2"/>
             <input type="submit" value="Change"/>
         </form>
     </td>
</tr>

现在在servlet中,我尝试通过以下代码获取“studentId”字段并添加到请求学生字段:

int studentId = Integer.parseInt(request.getParameter("studentId"));
Student student = DAOFactory.getInstance().getStudentDAO().findStudentById(studentId);
request.setAttribute("student", student);

但在线int studentId = Integer.parseInt(...)出现错误:

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:454)
java.lang.Integer.parseInt(Integer.java:527)
ua.kpi.fpm.portal.command.administrator.OpenChangeStudentPageCommand.execute(OpenChangeStudentPageCommand.java:26)
ua.kpi.fpm.portal.controller.Controller.processRequest(Controller.java:53)
ua.kpi.fpm.portal.controller.Controller.doPost(Controller.java:85)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

我无法找出发生此错误的原因以及为什么来自表单的studentId可以为null。

1 个答案:

答案 0 :(得分:3)

应该是

<input type="hidden" name="studentId" value="${studentInfo.student.id}"/>

而不是

<imput type="hidden" name="studentId" value="${studentInfo.student.id}"/>

查看无效的imput代码。