首页 > 解决方案 > 在 jsp 中使用 c:foreach 时如何修复错误 NumberFormatException

问题描述

问题

显示记录时出现错误,我尝试 varStatus 但仍然出现此错误,我应该怎么做才能修复该错误?在另一个实体的控制器中,它可以工作!太感谢了!!!

存储库

public List<RecordFormHomePage> topStaff() {
    Session session = this.sessionFactory.getCurrentSession();
    List<RecordFormHomePage> recordsList = session.createSQLQuery("select `StaffId`,sum(case when `Type`=1 then 1 else 0 end) as achievement, sum(case when `Type`=0 then 1 else 0 end) as mistake, sum(case when `Type`=1 then 1 else 0 end)-sum(case when `Type`=0 then 1 else 0 end) as total from Records group by `StaffId`\n" + 
            "").list();

    return recordsList;
} 

服务

@Transactional
public List<RecordFormHomePage> topStaff() {

    return recordRepository.topStaff();
}

JSP

<c:forEach var="staff" items="${TopStaff }" varStatus="stt">
    <tr>
        <td>${staff.stt.total }</td>
    </tr>
 </c:forEach>

控制器

  public String index(Model model) {
    model.addAttribute("TopStaff", recordService.topStaff());
    return "/homepage/index";
}

记录表格主页

public class RecordFormHomePage implements Serializable{
int StaffId;
int Achievement;
int mistake;
int total;

错误

java.lang.NumberFormatException: For input string: "stt"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:580)
java.lang.Integer.parseInt(Integer.java:615)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:144)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:61)
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:190)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:944)
org.apache.jsp.WEB_002dINF.views.homepage.index_jsp._jspx_meth_c_005fforEach_005f0(index_jsp.java:810)
org.apache.jsp.WEB_002dINF.views.homepage.index_jsp._jspService(index_jsp.java:266)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
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:1257)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

太感谢了!!!

标签: javahibernatespring-mvc

解决方案


我希望您的 .jsp 看起来像这样:

<table>
   <tr>
      <th>Number</th><th>Name</th></tr>
   <c:forEach var="staff" items="${TopStaff}" varStatus="loop">
   <tr>
      <td>${loop.index}</td><td>${staff.name}</td></tr>
       ...
   </c:forEach>
   ...

这假设:

  • “TopStaff”是工作人员的列表。
  • "staff" 是循环中的当前人员记录
  • “staff”对象中的属性都是有效的(没有“bad data”)
  • ${TopStaff},${staff.name}等在大括号内没有任何“空白”空格。
  • 您引用了一个有效的 varStatus 属性,例如“loop.index”、“loop.begin”、“loop.end”等。

'希望有帮助...


推荐阅读