本文介绍了如何将列表从Servlet发送到JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Servlet中有List<PostData>
个带有供用户使用的帖子,我如何将该列表发送到用户的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.
推荐答案
您可以在属性中设置列表,并在ur 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!