我正在尝试使用JSTL在Jsp页面中打印列表对象,但出现了一些异常。以下是我的App的模型类
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)
由于您需要获取BigDecimal价值,因此必须使用JSTL格式taglib来格式化的显示BigDecimal。
BigDecimal
在JSP的taglib下面
<%@ 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>