如何将列表从 Servlet 发送到 JSP [英] How to send List from Servlet to JSP
本文介绍了如何将列表从 Servlet 发送到 JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 List
和一些用户在 Servlet 中的帖子,我如何将此列表发送到用户的 JSP 页面?我需要拆分服务器部分和布局部分.谢谢.
I have List<PostData>
with some posts for user in Servlet , how can i send this list to user's JSP page?? I need to split the server part and layout part.
Thanks.
这是我的清单:
public List<PostData> getPostforUser(String komy)
{
ArrayList<PostData> posts = new ArrayList<PostData>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from posts where komy='"+komy+"'");
while(rs.next())
{
PostData postdata = new PostData();
postdata.setId(rs.getInt("id"));
postdata.setOtkogo(rs.getString("otkogo"));
postdata.setKomy(rs.getString("komy"));
postdata.setText(rs.getString("text"));
postdata.setDate(rs.getString("'date'"));
posts.add(postdata);
}
}catch (SQLException e) {
e.printStackTrace();
}
return posts;
}
一切正常,我在下面创建了连接和其他东西.
All is work, i made Connections and another things below.
推荐答案
您可以在您的属性中设置列表并在您的 jsp 上循环遍历它.
You can set the list in your attribute and loop throught it on ur jsp.
request.setAttribute("posts", posts);
在你的jsp中:
<table>
<c:forEach items="${posts}" var="post">
<tr>
<td>${post.id}</td>
....
</tr>
</c:forEach>
</table>
如果没有 jstl,将会是这样的:
Without jstl, would be something like this:
<%
ArrayList<PostData> posts=(ArrayList<PostData>) request.getAttribute("posts");
for (PostData post: posts) {
%>
<tr>
<td><%=post.id%></td>
....
</tr>
<%}%>
这篇关于如何将列表从 Servlet 发送到 JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文