2015-12-08 85 views
0

我想在Jsp頁面中使用JSTL打印List對象,但我收到了一些異常。下面是我的應用程序JSTL無法打印對象列表

public class ItemMaster implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = -1364853156929567630L; 

private BigDecimal itemId; 
private String itemNumber; 
private String description; 
private String lifeCyclePhase; 
public BigDecimal getItemId() { 
    return itemId; 
} 
public void setItemId(BigDecimal itemId) { 
    this.itemId = itemId; 
} 
public String getItemNumber() { 
    return itemNumber; 
} 
public void setItemNumber(String itemNumber) { 
    this.itemNumber = itemNumber; 
} 
public String getDescription() { 
    return description; 
} 
public void setDescription(String description) { 
    this.description = description; 
} 
public String getLifeCyclePhase() { 
    return lifeCyclePhase; 
} 
public void setLifeCyclePhase(String lifeCyclePhase) { 
    this.lifeCyclePhase = lifeCyclePhase; 
} 

下面的模型類是列表

@RequestMapping(value="/searchQuery", method=RequestMethod.POST) 
    public ModelAndView submitForm(HttpServletRequest request, ModelAndView model) { 
    String[] listBox1 = request.getParameterValues("selectedright"); 
    List<ItemMaster> lists=itemDao.fetchRecords(listBox1); 
    model.addObject("queryResult",lists); 
    model.setViewName("results");  
    return model; 
    } 

下面的控制器是我的jsp頁面的地方我試圖找回值段

<c:forEach var="queryResult" items="${queryResult}" varStatus="status"> 
      <tr> 
     <td>${status.index + 1}</td> 
       <td>${queryResult.itemId}</td> 
       <td>${queryResult.itemNumber}</td> 
       <td>${queryResult.description}</td> 
       <td>${queryResult.lifeCyclePhase}</td> 
      </tr> 
      </c:forEach> 

我得到的錯誤是

java.lang.NumberFormatException: For input string: "itemId" 
java.lang.NumberFormatException.forInputString(Unknown Source) 
java.lang.Integer.parseInt(Unknown Source) 
java.lang.Integer.parseInt(Unknown Source) 
javax.el.ListELResolver.coerce(ListELResolver.java:157) 
javax.el.ListELResolver.getValue(ListELResolver.java:70) 
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110) 
org.apache.el.parser.AstValue.getValue(AstValue.java:167) 
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) 
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:936) 
org.apache.jsp.WEB_002dINF.views.results_jsp._jspService(results_jsp.java:145) 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:725) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431) 
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:725) 
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.doPost(FrameworkServlet.java:868) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:644) 
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:725) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
+0

我建議纔去任何進一步閱讀本。你的方法和變量名稱是一團糟。 http://www.oracle.com/technetwork/java/codeconventions-135099.html –

+0

@AlanHay我做了這些更改仍然收到相同的錯誤 –

+0

您無法直接訪問BigDecimal值,因爲您需要用戶jstl格式化程序。請檢查我的回答 –

回答

1

由於您需要獲取BigDecimal值,因此您必須使用JSTL格式的taglib來格式化BigDecimal的顯示。

及以下的taglib的JSP

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

現在檢索列表,

<c:forEach var="queryResult" items="${queryResult}" varStatus="status"> 
    <tr> 
     <td>${status.index + 1}</td> 
     <td><fmt:formatNumber value="${queryResult.itemId}" minFractionDigits="2"/></td> 
     <td>${queryResult.itemNumber}</td> 
     <td>${queryResult.description}</td> 
     <td>${queryResult.lifeCyclePhase}</td> 
    </tr> 
</c:forEach> 
+0

所做的更改仍然得到同樣的error..org.apache.jasper.JasperException:發生處理JSP頁面/WEB-INF/views/results.jsp例外,在48行 45:\t \t \t \t 46:\t \t 47:\t \t \t​​$ {status.index + 1} 48:​​< fmt:formatNumber value =「$ {queryResult.itemId}」minFractionDigits =「2」/> 49: \t \t \t \t \t​​$ {} queryResult.itemNumber java.lang.NumberFormatException:對於輸入字符串 「的itemId」 \t java.lang.NumberFormatException.forInputString(來源不明) –

+0

有你的JSP –

+0

添加標籤庫是的,我已添加taglib <%@ taglib prefix =「form」uri =「http://www.springframework.org/tags/form」%> <%@ taglib prefix =「fmt」uri =「http:// java .sun.com/jsp/jstl/fmt「%> –