我正在尝试将一个post容器从我的servlet传递给jsp,所以我可以显示该jsp页面上的所有元素。每个元素都有时间,名称和内容属性。但是,jsp文件无法迭代这些元素。我该如何解决?
我有课:
public class post {
public post(long time, String name, String content) {
super();
this.time = time;
this.name= name;
this.content= content;
}
public String name;
public String content;
public long time;
}
和我做的servlet:
post[] messages;
messages = sortMessages(data); // construct array of post
for (int i = 0; i < messages.length; i++) {
request.setAttribute("posts", messages[i]);
}
在jsp文件中,我希望通过以下方式显示所有帖子:
<c:forEach var="post" items="${posts}">
<div class="post">
<h2>${post.name}</h2>
<p>${post.content}</p>
</div>
然而我收到错误:
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.index_jsp._jspService(index_jsp.java:358)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
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:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
serv.viewMessages.doGet(viewMessages.java:122)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:274)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
org.apache.jsp.index_jsp._jspx_meth_c_005fforEach_005f0(index_jsp.java:411)
org.apache.jsp.index_jsp._jspService(index_jsp.java:318)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
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:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
serv.viewMessages.doGet(viewMessages.java:122)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:2)
post[] messages;
messages = sortMessages(data); // construct array of post
for (int i = 0; i < messages.length; i++) {
request.setAttribute("posts", messages[i]);
}
当您在循环中执行request.setAttribute("posts", messages[i]);
时,您只是覆盖以前的值。那么&#34;帖子&#34;将仅包含for循环中提供的最后一个值。相反,你应该创建一个arraylist
并将值保存在arraylist对象中,然后将该对象传递给jsp并在那里使用jstl。
List<String> list = new ArrayList<String>();
for (int i = 0; i < messages.length; i++) {
list.add(messages[i]);
}
request.setAttribute("posts", list);
这样您就可以发送包含所有值的列表对象,现在在jsp中,您可以使用此对象通过for循环获取所有值。
<c:forEach var="message" items="${requestScope.posts}">
<c:out value="${message}"/>
</c:forEach>
答案 1 :(得分:1)
items
的{{1}}属性期望集合类型对象能够迭代它。但是在您的servlet中,您将<c:forEach>
属性设置为posts
对象的类型。不要将post
属性设置为posts
,只需将其设置为messages[i]
即可。而且你的servlet中不需要for循环。
messages
为了使您能够在JSP中打印post[] messages;
messages = sortMessages(data); // construct array of post
request.setAttribute("posts", messages);
对象的属性,您需要提供getter方法。
post
答案 2 :(得分:0)
通过以下方式更改它解决了iteratin问题:
request.setAttribute("posts", messages);
但是我无法访问其字段并收到错误:
Property 'name' not found on type serv.post