jsp用jst的foreach标签遍历集合的时候遇到个问题,集合中的var这里是什么类型,字符串还是集合中元素?l 20
我用的jstl1.1tomcat5.5版本这是我的jsp代码锘�%@pagelanguage="java"import="java.util.*"pageE...
我用的jstl1.1 tomcat5.5版本
这是我的jsp代码
锘�%@ page language="java" import="java.util.*" pageEncoding="gbk" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ page import="com.servlet.Product" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>for each杩唬</title>
</head>
<%
List<Product> products=new ArrayList<Product>();
Product p=null;
for(int i=0;i<10;i++){
p=new Product();
p.setName("浜у搧"+i);
p.setArea("鍦板尯"+i);
p.setPrice("浠锋牸"+i);
products.add(p);
}
session.setAttribute("products",products);
%>
<body>
<c:set var="products" value="${sessionScope.products}"/>
<table border="1">
<c:forEach var="product" items="products" >
<tr>
<td><c:out value="${product.name}"/></td>
<td><c:out value="${product.area}"/></td>
<td><c:out value="${product.price}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
抛出如下异常
Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.el.ELException: Unable to find a value for "name" in object of class "java.lang.String" using operator "."
at org.apache.commons.el.Logger.logError(Logger.java:481)
at org.apache.commons.el.Logger.logError(Logger.java:498)
at org.apache.commons.el.Logger.logError(Logger.java:611)
at org.apache.commons.el.ArraySuffix.evaluate(ArraySuffix.java:340)
at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:924)
at org.apache.jsp.demo_005fforeach_jsp._jspx_meth_c_005fout_005f0(demo_005fforeach_jsp.java:184)
at org.apache.jsp.demo_005fforeach_jsp._jspx_meth_c_005fforEach_005f0(demo_005fforeach_jsp.java:145)
at org.apache.jsp.demo_005fforeach_jsp._jspService(demo_005fforeach_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 展开
这是我的jsp代码
锘�%@ page language="java" import="java.util.*" pageEncoding="gbk" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ page import="com.servlet.Product" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>for each杩唬</title>
</head>
<%
List<Product> products=new ArrayList<Product>();
Product p=null;
for(int i=0;i<10;i++){
p=new Product();
p.setName("浜у搧"+i);
p.setArea("鍦板尯"+i);
p.setPrice("浠锋牸"+i);
products.add(p);
}
session.setAttribute("products",products);
%>
<body>
<c:set var="products" value="${sessionScope.products}"/>
<table border="1">
<c:forEach var="product" items="products" >
<tr>
<td><c:out value="${product.name}"/></td>
<td><c:out value="${product.area}"/></td>
<td><c:out value="${product.price}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
抛出如下异常
Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.el.ELException: Unable to find a value for "name" in object of class "java.lang.String" using operator "."
at org.apache.commons.el.Logger.logError(Logger.java:481)
at org.apache.commons.el.Logger.logError(Logger.java:498)
at org.apache.commons.el.Logger.logError(Logger.java:611)
at org.apache.commons.el.ArraySuffix.evaluate(ArraySuffix.java:340)
at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:924)
at org.apache.jsp.demo_005fforeach_jsp._jspx_meth_c_005fout_005f0(demo_005fforeach_jsp.java:184)
at org.apache.jsp.demo_005fforeach_jsp._jspx_meth_c_005fforEach_005f0(demo_005fforeach_jsp.java:145)
at org.apache.jsp.demo_005fforeach_jsp._jspService(demo_005fforeach_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 展开
3个回答
展开全部
<c:forEach var="product" items="products" > 这个改成<c:forEach var="product" items="${products}" > 改了再看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-06-04
展开全部
把c:foreach中的var改成id试试!从上面的错误中,你看看你的类中是否有name的get和set方法!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询