JSP中的$ {employee.id}抛出java.lang.NumberFormatException:对于inputstring:“id”

我有一个JSP页面,当在<c:forEach>显示来自下方的List<Employee>时,可以正常工作。

 @RequestMapping(value = { "getAllEmployees", "/" }) public ModelAndView getAllEmployees() { // logger.info("Getting the all Employees."); List<Employee> employeeList = employeeService.getAllEmployees(); return new ModelAndView("employeeList", "employeeList", employeeList); } 

而从下面的方法显示List<Employee>时,相同的JSP引发exception。

 @RequestMapping("searchEmployee") public ModelAndView searchEmployee(@RequestParam("searchName") String searchName) { // logger.info("Searching the Employee. Employee Names: " + searchName); List<Employee> employeeList = employeeService.getAllEmployees(searchName); System.err.println("Employee count = "+employeeList.size()); return new ModelAndView("employeeList", "employeeList", employeeList); } 

这是堆栈跟踪:

 java.lang.NumberFormatException: For input string: "id" java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) java.lang.Integer.parseInt(Integer.java:492) java.lang.Integer.parseInt(Integer.java:527) javax.el.ArrayELResolver.coerce(ArrayELResolver.java:151) javax.el.ArrayELResolver.getValue(ArrayELResolver.java:64) org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110) org.apache.el.parser.AstValue.getValue(AstValue.java:169) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943) org.apache.jsp.WEB_002dINF.pages.employeeList_jsp._jspx_meth_c_005fout_005f0(employeeList_jsp.java:358) org.apache.jsp.WEB_002dINF.pages.employeeList_jsp._jspx_meth_c_005fforEach_005f0(employeeList_jsp.java:312) org.apache.jsp.WEB_002dINF.pages.employeeList_jsp._jspx_meth_c_005fif_005f1(employeeList_jsp.java:273) org.apache.jsp.WEB_002dINF.pages.employeeList_jsp._jspService(employeeList_jsp.java:162) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

这是如何造成的,我该如何解决?

你的getAllEmployees(searchName)方法不会返回一个List<Employee> ,而是一个List<Object[]> 。 很可能还有一个由编译器生成的“未经检查的强制转换”警告,您忽略或取消了该警告。

证据是javax.el.ArrayELResolver参与堆栈跟踪。 这只在ELexpression式的基数是数组types时才涉及。 如果你真的有一个Employee而不是一个Object[] ,那么你应该期望javax.el.BeanELResolver处于ELexpression式${employee.id}所在的特定堆栈跟踪行。 由于${employee}实际上就是一个数组,所以EL会将id属性解释为一个数组索引,然后尝试将其parsing为一个Integer ,但是却无法这样做,因为您可以在堆栈跟踪的顶部行中看到这一点。

要解决这个问题,你有两个select:

  1. 修正getAllEmployees(searchName)方法返回一个真正的 List<Employee> 。 通常,这是通过直接查询Employee实体而不是单独的列/字段来完成的。

  2. List<Object[]>replace所有不正确的List<Employee>声明,并在EL中将其作为${employee[0]}${employee[1]}等对象数组处理。