简体   繁体   中英

How to fix error NumberFormatException when use c:foreach in jsp

Issues

I have a error when display record, I try varStatus but that still this error, What i should do to fix that bug?? In a controller of another entity, it work! Tks a alot!!!

Repository

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

Service

@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>

Controller

  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;

Error

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)

tks so much!!!

I would expect your .jsp might look something like this:

<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>
   ...

This assumes:

  • "TopStaff" is a list of staff members.
  • "staff" is the current staff record in the loop
  • The properties in the "staff" object are all valid (no "bad data")
  • ${TopStaff} , ${staff.name} , etc. don't have any "blank" spaces inside the braces.
  • You reference a valid varStatus property like "loop.index", "loop.begin", "loop.end", etc.

'Hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM