2012-02-20 190 views
4

我想根據請求將我自己的對象數組發送到JSP頁面。將servlet對象數組發送到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

我做了什麼錯?

回答

3

你可能

  • 忘記導入JSP中的第三類,使用<%@ page import="com.foo.bar.Article" %>
  • 忘記的getAttribute()的結果轉換的文章數組:

Article[] articles = (Article[]) request.getAttribute("articles");

請注意,您不應該在JSP中擁有任何Java代碼。您應該使用JSP EL,JSTL和其他自定義標籤。閱讀How to avoid Java code in JSP files?

2
request.getAttribute("articles"); 

以上將總是返回字符串,所以你需要做一個演員,用這個

Article[] articles = (Article[]) request.getAttribute("articles"); 

,並導入你的JSP頁面你的文章類,在進口水平

<%@ page import="yourpackage.Article"%> 
添加此
11

您應該避免使用JSTL來使用scriptlet。 請通過下面的例子:

POJO類的例子:

public class Article { 
    private int id; 
    private String title; 

    public Article(int id, String title) { 
     this.id = id; 
     this.title = title; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

} 

一個Servlet的實例:

public class TestServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     Article[] articles = 
       new Article[] {new Article(1, "Article one"), new Article(2, "Article two")}; 
     request.setAttribute("articles", articles); 

     RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp"); 
     dispatcher.forward(request, response); 
    } 

} 

JSP頁面的一個例子:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    <c:forEach items="${articles}" var="article"> 
    <c:out value="${article.id} ${article.title}"/><br /> 
    </c:forEach> 
</body> 
</html> 

結果HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 

    1 Article one<br /> 

    2 Article two<br /> 

</body> 
</html> 

我希望這個例子能幫助你。

+0

謝謝例如...但我有一個問題:當我在JSP上寫入時''c:forEach items =「$ {articles}」var =「article」>'我的循環知道,$ {articles}數據是通過請求從servlet發送的,而不是其他方式...或者我應該注意它的位置,或初始化一個變量'articles' – 2012-02-20 14:11:15

+0

的範圍?做一些像那樣的範圍=「請求」 – 2012-02-20 14:31:46

+0

是的,範圍。 JSP/Servlet API中有4個範圍: 1)應用程序 - 全局範圍[ServletContext](http://bit.ly/xChDI5) 2)會話 - 每個HTTP會話一個[HttpSessionContext](http:// bit.ly/zkPokb) 3)請求 - 每請求一個[ServletRequest](http://bit.ly/wMhTt5) 4)頁 - 處理請求[PageContext](http:// bit。 ly/A0Btl8) 您可以選擇適合您需求的撥款電話。 查看關於此主題的好資料 - [JSP中有哪些不同的範圍?](http://bit.ly/aNUu7i) – 2012-02-20 15:35:39