我想通过请求将我自己的对象数组发送到 JSP 页面。
在 servlet 中的这部分代码中,我将获取数据,将其放入对象数组中,并将它们设置为请求。
if (request.getParameter("todo").equals("show_article_list")) {
try {
Article[] articles = this.getArticleList();
request.setAttribute("articles", articles);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("article/article_list.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
}
}
public Article[] getArticleList() throws Exception {
db data = new db();
Connection con = data.OpenConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM `article`");
ResultSet result = statement.executeQuery();
int size = 0;
if (result != null)
{
if (result.last()) {
size = result.getRow();
result.beforeFirst();
}
}
Article[] articles = new Article[size];
int i = 0;
while(result.next()){
articles[i] = new Article (
result.getInt(1),
result.getString(2),
result.getString(3),
result.getString(4));
i++;
}
return articles;
}
这是我的课:
public class Article {
public Integer getId(){return id;}
public String getTitle(){return title;}
public void setTitle(String title){this.title = title;}
public String getText(){return text;}
public void set(String text){this.text = text;}
public String getDescription(){return description;}
public void setDescription(String description){this.description= description;}
private Integer id;
private String title;
private String text;
private String description;
public Article(Integer Id, String Title, String Text, String Description)
{
id = Id;
title = Title;
text = Text;
description = Description;
}
}
在我的 JSP 页面上,我想使用循环这样的对象数组request.getAttribute("articles");
我怎样才能做到呢?
我必须使用<jsp:useBean/>
或者是其他东西?我尝试这样做:
Article[] articles = request.getAttribute("articles");
但我有一个错误:Article cannot be resolved to a type
我做错了什么?