${employee.id} from List in JSP throws java.lang.N

2019-01-03 03:39发布

I have a JSP page which works fine when showing List<Employee> from below method in a <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);
}

Whereas the same JSP throws an exception when showing List<Employee> from below method.

@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);
}

Here is the stack trace:

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)

How is this caused and how can I solve it?

1条回答
叼着烟拽天下
2楼-- · 2019-01-03 04:20

Your getAllEmployees(searchName) method doesn't return a List<Employee>, but a List<Object[]>. Most likely there's also an "unchecked cast" warning generated by the compiler which you ignored or suppressed.

The evidence is the involvement of javax.el.ArrayELResolver in the stack trace. This is only involved when the base of an EL expression is of an array type. If you really had an Employee instead of an Object[], then you'd expect javax.el.BeanELResolver at the particular stack trace line where the EL expression ${employee.id} is to be evaluated. As ${employee} is in your case actually an array, EL will interpret the id property as an array index and then tries to parse it as an Integer, but failed to do so as you can see in top lines of the stack trace.

To solve this problem, you've 2 options:

  1. Fix the getAllEmployees(searchName) method to return a real List<Employee>. Usually, this is to be done by querying the Employee entity directly instead of invididual columns/fields.

  2. Replace all incorrect List<Employee> declarations by List<Object[]> and handle it in EL as an object array like so ${employee[0]}, ${employee[1]}, etc.

查看更多
登录 后发表回答