${employee.id} from List in JSP throws java.lang.NumberFormatException: For input string: "id"
${employee.id} from List in JSP throws java.lang.NumberFormatException: For input string: "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);
}
而相同的 JSP 在从下面的方法显示 List<Employee>
时抛出异常。
@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)
方法不是 return List<Employee>
,而是 List<Object[]>
。很可能还有编译器生成的 "unchecked cast" 警告,您忽略或抑制了它。
证据是 javax.el.ArrayELResolver
参与了堆栈跟踪。这仅在 EL 表达式的基是数组类型时涉及。如果您确实有一个 Employee
而不是 Object[]
,那么您会期望在要计算 EL 表达式 ${employee.id}
的特定堆栈跟踪行处有 javax.el.BeanELResolver
。由于 ${employee}
在您的情况下实际上是一个数组,EL 会将 id
属性 解释为数组索引,然后尝试将其解析为 Integer
,但失败了正如您在堆栈跟踪的顶行中看到的那样。
要解决这个问题,您有 2 个选择:
将getAllEmployees(searchName)
方法修正为return一个真实List<Employee>
。通常,这是通过直接查询 Employee
实体而不是单个 columns/fields.
来完成的
将所有不正确的 List<Employee>
声明替换为 List<Object[]>
并在 EL 中将其作为对象数组处理,如 ${employee[0]}
、${employee[1]}
等。
我有一个 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);
}
而相同的 JSP 在从下面的方法显示 List<Employee>
时抛出异常。
@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)
方法不是 return List<Employee>
,而是 List<Object[]>
。很可能还有编译器生成的 "unchecked cast" 警告,您忽略或抑制了它。
证据是 javax.el.ArrayELResolver
参与了堆栈跟踪。这仅在 EL 表达式的基是数组类型时涉及。如果您确实有一个 Employee
而不是 Object[]
,那么您会期望在要计算 EL 表达式 ${employee.id}
的特定堆栈跟踪行处有 javax.el.BeanELResolver
。由于 ${employee}
在您的情况下实际上是一个数组,EL 会将 id
属性 解释为数组索引,然后尝试将其解析为 Integer
,但失败了正如您在堆栈跟踪的顶行中看到的那样。
要解决这个问题,您有 2 个选择:
将
getAllEmployees(searchName)
方法修正为return一个真实List<Employee>
。通常,这是通过直接查询Employee
实体而不是单个 columns/fields. 来完成的
将所有不正确的
List<Employee>
声明替换为List<Object[]>
并在 EL 中将其作为对象数组处理,如${employee[0]}
、${employee[1]}
等。