2011-05-27 77 views
1

我正在編寫一個使用servlet維護VideoData對象的ArrayList的Web應用程序(這些應用程序只包含有關諸如標題,電影類型等電影的基本信息)。在.jsp中迭代ArrayList(與MVC2兼容)

該servlet放入請求的範圍名單和請求和響應於一個jsp轉發(僅servlet代碼這裏示出的部分):

public class VideoServlet extends HttpServlet { 

    private ArrayList<VideoData> library = new ArrayList<VideoData>(); 

    public void doGet(HttpServletRequest request, 
         HttpServletResponse response) { 

     try { 

      // put ArrayList in Request's scope 
      request.setAttribute("the_table", library); 

      request.getRequestDispatcher("/listvideos.jsp").forward(request, 
        response); 

      ... 

的listvideos.jsp如下所示。我收到一個Tomcat錯誤,指出無法解析JSTL的uri。我在我的jsp代碼的其他部分中使用了EL,而不必像這樣有任何特殊的導入線,我不確定JSTL是否仍然是解決此類問題的首選方法,同時仍然嘗試遵守MVC2和將所有Java代碼保存在Servlet中。任何人都可以在這裏指出我正確的方向嗎?理想情況下,我想要一個純粹的EL解決方案,如果可能的話。

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 
'http://www.w3.org/TR/html4/loose.dtd'> 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

<html> 

    <head> 

     <title>Cattle Drive Assignment Servlets-4: Videos</title> 

    </head> 

    <body> 

     <h1>Cattle Drive Assignment Servlets-4: Videos</h1> 

     <form method='post' action='/videos/VideoServlet'> 

      <a href="http://localhost:8080/videos/addvideo.jsp">Add a video</a> 

      <br> 
      <br> 

      <table border="1"> 

       <tr> 

        <th>Title</th> 

        <th>Star</th> 

        <th>Type</th> 

        <th>VHS</th> 

        <th>DVD</th> 

        <th>Description</th> 

       </tr> 

       <c:forEach items="${the_table}" var="movie"> 

       <tr> 

        <td>${movie.getTitle()}</td> 

        <td>${movie.getStar()}</td> 

        <td>${movie.getType()}</td> 

        <td>${movie.inVHS()}</td> 

        <td>${movie.inDVD()}</td> 

        <td>${movie.getDesc()}</td> 

       </tr> 

       </c:forEach> 

      </table> 

     </form> 

    </body> 

</html> 

回答

3

您的代碼看起來基本正確。看起來像你看到的錯誤表明在類路徑中找不到JSTL標籤庫。請確保jstl.jarstandard.jar在您的戰爭WEB-INF/lib文件夾中。

+0

或者當您使用Tomcat 6/7時,單獨使用jstl-1.2.jar也足夠了。另見http://stackoverflow.com/tags/jstl/info – BalusC 2011-05-27 11:58:22