2014-03-03 43 views
2

使用Countries類型,它是一個bean類使用下面的代碼時,我只得到了一個空白頁面輸出的ArrayList印刷的ArrayList使用JSTL在JSP

<% 
    ArrayList<Countries> countryList = (ArrayList<Countries>) request.getAttribute("al"); 
%> 

<c:forEach items="${countryList}" var="item"> 
    <c:out value="${item.code}"></c:out> 
    <c:out value="${item.name}"></c:out> 
</c:forEach> 

回答

2

你不需要設置請求屬性again.You可以使用下面的代碼

<c:forEach items="${al}" var="item"> 
    <c:out value="${item.code}"></c:out> 
    <c:out value="${item.name}"></c:out> 
</c:forEach> 

這樣你也可以擺脫scriptlet。

希望它有助於

1

原因是EL(表達式語言)在任何有效範圍內都找不到名爲countryList的變量。在小腳本中聲明的變量是不是到EL可見的,所以你必須將它添加到一個有效的範圍內,例如這樣的要求:

<% 
    ArrayList<Countries> countryList = (ArrayList<Countries>) request.getAttribute("al"); 
    request.setAttribute("countryList", countryList); 
%> 

<c:forEach items="${countryList}" var="item"> 
    <c:out value="${item.code}"></c:out> 
    <c:out value="${item.name}"></c:out> 
</c:forEach> 
+0

沒有必要在請求屬性的請求範圍內再次刷新列表作爲它已經存在。 – Gautam