2013-10-22 99 views
3

VAR是靜態屬性以暴露電流元件(本地到主體)如何使用scriptlet/expression訪問c:forEach中的迭代變量?

如何通過的scriptlet /表達查看在var屬性?

初始化代碼

<% 
Employee e = new Employee(); 
e.setName("name"); 
e.setEmail("[email protected]"); 
java.util.List<Employee> empList = new java.util.ArrayList(); 
empList.add(e); 
request.setAttribute("empList", empList); %> 

的forEach代碼1個 deferredExpression錯誤

<c:forEach var="emp" items="${employees}"> 
    <c:out value="${emp.name}"/><br><%=emp.getName()%> 
</c:forEach> 

NOR

的forEach代碼2延遲redExpression錯誤

<c:forEach var="emp" items="${empList}" varStatus="status"> 
    Emp email: <%= ((Employee)(pageContext.findAttribute("emp"))).getName() %> 
</c:forEach> 

回答

-1
<c:forEach var="emp" items="${empList}" varStatus="status"> 
    <c:out value="Emp email: ${emp.email}"/> 
</c:forEach> 
+0

您使用表達式語言在這裏,沒有scriptlet而<%..%>或表達式<%=..%> – Joe

5

我有java.lang.NoSuchFieldError的:deferredExpression我每次改變,因爲我有不同版本的JSTL庫,現在我只能留下一個JSTL-1.2.jar more info about JSTL

JSTL的文檔文件說,它顯然「爲迭代的當前項導出範圍變量的名稱。這個範圍的變量具有嵌套的知名度」。嵌套意味着從開始標籤到結束標籤。

EL代碼

<c:forEach begin="0" end="5" var="countvar"> 
Iteration number ${ countvar + 24 } 
</c:forEach> 

替代JSP腳本

<c:forEach begin="0" end="5" var="countvar"> 
Iteration number 
<%= ((Integer) (pageContext.findAttribute("cv")).intValue()+24 %> 
</c:forEach> 

其他C:與收集

<% 
Employee e = new Employee(); 
e.setName("name"); 
e.setEmail("[email protected]"); 
java.util.List<Employee> empList = new java.util.ArrayList(); 
empList.add(e); 
request.setAttribute("empList", empList); 
%> 

<c:forEach var="emp" items="${empList}" varStatus="status"> 
    Emp email: <%= ((Employee)(pageContext.findAttribute("emp"))).getName() %> 
</c:forEach> 
2

我使用以下一般片斷的forEach例如:

<c:forEach items="<%= itemList %>" var="item"> 
    <% 
     ItemClass item = (ItemClass)pageContext.getAttribute("item"); 
    %> 
    <p>Value with scriptlet: <%= item.getValue() %></p> 
    <p>Value with EL ${item.value}</p> 
</c:forEach> 

你的情況與EL:

<c:forEach var="emp" items="<%= empList %>" varStatus="status"> 
    ${emp.name} email: ${emp.email} 
</c:forEach> 

你的情況與小腳本:

<c:forEach var="emp" items="<%= empList %>" varStatus="status"> 
    <% 
     Employee employee = (Employee)pageContext.getAttribute("emp"); 
    %> 
    <%= employee.getName() %> email: <%= employee.getEmail() %> 
</c:forEach>