我有一个JSP页面,当List<Employee>从下方的方法显示时,该页面工作正常<c:forEach>。
List<Employee>
<c:forEach>
@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会引发异常。
@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[]>。很可能还会有编译器生成的“未经检查的强制转换”警告,你忽略或取消了该警告。
getAllEmployees(searchName)
List<Object[]>
证据是参与javax.el.ArrayELResolver堆栈跟踪。仅在EL表达式的基数为数组类型时才涉及此内容。如果确实有一个Employee而不是Object[],那么你将期望javax.el.BeanELResolver在${employee.id}要评估EL表达式的特定堆栈跟踪行中。与${employee}你实际的情况一样,EL会将id属性解释为数组索引,然后尝试将其解析为Integer,但是这样做失败,如你在堆栈跟踪的顶部看到的那样。
javax.el.ArrayELResolver
Object[]
javax.el.BeanELResolver
${employee.id}
${employee}
要解决此问题,你有两种选择:
修复getAllEmployees(searchName)返回实数 的方法List<Employee>。通常,这是通过Employee直接查询实体而不是单个列/字段来完成的。
更换所有不正确的List<Employee>通过声明List<Object[]>和在EL处理它作为一个对象数组像这样${employee[0]},${employee[1]}等
${employee[0]}
${employee[1]}