jsp文件无法迭代servlet中的复杂元素

ulydmbyx  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(475)

我正在尝试将一个posts容器从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 &lt;forEach&gt;
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 &lt;forEach&gt;
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)
vnzz0bqm

vnzz0bqm1#

这个 items 的属性 <c:forEach> 期望集合类型对象能够对其进行迭代。但是在servlet中,您设置了 posts 属性的类型 post 对象。而不是设置 posts 属性为 messages[i] 把它调成 messages . 您不需要servlet中的for循环。

post[] messages;
messages = sortMessages(data); // construct array of post
request.setAttribute("posts", messages);

并使您能够打印 post 对象,您需要提供getters方法。

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;

    public String getName() {
        return name;
    }

    public String getContent() {
        return content;
    }

    public long getTime() {
        return time;
    }
}
rekjcdws

rekjcdws2#

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]); 在循环中,您只是覆盖上一个值。因此,“post”将只包含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);

通过这种方式发送包含所有值的list对象,现在在jsp中,您可以使用此对象通过for循环获取所有值。

<c:forEach var="message" items="${requestScope.posts}">
    <c:out value="${message}"/>
</c:forEach>
0qx6xfy6

0qx6xfy63#

通过以下方式更改它解决了iteratin问题:

request.setAttribute("posts", messages);

但是,我无法访问其字段并获取错误:

Property 'name' not found on type serv.post

相关问题