我有一个JSP页面,可以在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)
这是如何引起的?如何解决?
答案 0 :(得分:2)
您的getAllEmployees(searchName)
方法不会返回List<Employee>
,而是List<Object[]>
。很可能还有一个&#34;未经检查的演员&#34;由您忽略或禁止的编译器生成的警告。
证据是javax.el.ArrayELResolver
参与堆栈跟踪。这仅在EL表达式的基础是数组类型时才会涉及。如果你真的有一个Employee
而不是Object[]
,那么你应该在特定的堆栈跟踪线上预期javax.el.BeanELResolver
来评估EL表达式${employee.id}
。由于${employee}
在你的情况下实际上是一个数组,EL会将id
属性解释为数组索引,然后尝试将其解析为Integer
,但是你没能这样做请参阅堆栈跟踪的顶行。
要解决此问题,您有2个选项:
修复getAllEmployees(searchName)
方法以返回真实 List<Employee>
。通常,这是通过直接查询Employee
实体而不是通过单列/字段来完成的。
将所有不正确的List<Employee>
声明替换为List<Object[]>
,并将其作为对象数组处理,如${employee[0]}
,${employee[1]}
等。