在jsp中使用c:foreach时如何修复错误NumberFormatException

时间:2019-02-23 06:14:23

标签: java hibernate spring-mvc

问题

  

显示记录时出现错误,我尝试使用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";
}

RecordFormHomePage

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)

非常感谢!

1 个答案:

答案 0 :(得分:0)

我希望您的.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”对象中的属性全部有效(没有“坏数据”)
  • ${TopStaff}${staff.name}等在花括号内没有任何“空白”。
  • 您引用有效的varStatus属性,例如“ loop.index”,“ loop.begin”,“ loop.end”等。

'希望有帮助...