2017-02-20 178 views
0

我試圖編寫一個簡單的支持票證系統。我有以下代碼:從JSP頁面訪問ArrayList

售票類:

public class Ticket implements Serializable{ 

private static final long serialVersionUID = -4585877638895523067L; 

private int ticketID; 
private int userID; 
private String topic; 
private String desc; 
private String category; 
private int state; 
private String answer; 

public Ticket(int ID, int userID, String topic, String desc, String category, int state, String answer){ 
    this.ticketID = ID; 
    this.userID = userID; 
    this.topic = topic; 
    this.desc = desc; 
    this.category = category; 
    this.state = state; 
    this.answer = answer; 
} 

public int getTicketid() { 
    return ticketID; 
} 

public int getUserid() { 
    return userID; 
} 

public String getTopic() { 
    return topic; 
} 

public String getDesc() { 
    return desc; 
} 

public String getCategory() { 
    return category; 
} 

public int getState() { 
    return state; 
} 

public String getAnswer() { 
    return answer; 
} 

}

的Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    session = request.getSession(true); 
    String action = request.getParameter("action"); 
    RequestDispatcher dispatcher; 
    if (session.getAttribute("isLoged") == null || session.getAttribute("isLoged").equals(false)) { 
     dispatcher = getServletContext().getRequestDispatcher("/Login"); 
    }else{ 
     du = new DAOUser(); 
     ticketlist = du.getTickets((Integer)session.getAttribute("userid")); 
     session.setAttribute("tickets", ticketlist); 
     dispatcher = getServletContext().getRequestDispatcher("/account.jsp"); 
     if(action!=null){ 

     } 
    } 
    dispatcher.forward(request, response); 
} 

最後的JSP文件中我想要顯示的數據門票。

<tbody> 
    <c:forEach items="${sessionScope.tickets}" var="ticket"> 
    <tr> 
     <td>${ticket.ticketID}</td> 
     <td>${ticket.topic}</td> 
     <td>${ticket.category}</td> 
     <td>${ticket.status}</td> 
    </tr> 
    </c:forEach> 
</tbody> 

在Servlet使用的UserDAO的類提供通過票務對象形成的ArrayList。有了這個代碼,表格裏面什麼都沒有顯示......我已經嘗試了不同的技術來訪問和顯示數據,但無法完成。

+0

是否檢查ticketlist是不是空的? – mlg

回答

0

而不是

<c:forEach items="${sessionScope.tickets}" var="ticket"> 

嘗試

<c:forEach items="${tickets}" var="ticket"> 
+0

我之前嘗試過...這是在將代碼添加到會話中的代碼中的錯誤。 –

+0

您可以嘗試將一些alert/sysout只是爲了調試它,並確保該塊正在執行。你也可以在Tomcat下查找爲這個jsp創建的java文件 - work - Catalina-- localhost-- projectname-- org-- apache-- jsp - Your.jsp和Your.java – Maverick

+0

我把Alert該foreach並沒有被執行......但不知道爲什麼。當我進入tomcat來查看它的jsp是否與forEach ... –