您好我想在scriplet中使用jstl變量我想在num == 3時打印「hello」它嘗試獲取以下代碼,但是我的值保持爲零。在Scriplet中訪問JSTL變量
我想得到num
的值,並將其增加1,然後檢查num==3
然後在條件爲真時打印hello再次將值重新賦值爲零。
<c:set var="num" value="0"></c:set>
<c:forEach items="${requestScope.Products}" var="emp" begin="0" end="${size}" >
<c:if test="${num==3}">
<h1>hello</h1>
</c:if>
<%! int var=0;%>
<% var=Integer.parseInt(pageContext.getAttribute("num").toString());%>
<%
System.out.println(var);
var=var+1;%>
<h2><c:out value="${num}"></c:out></h2>
<td width="100">
<img src="images/${emp.image}" width="100" height="100"/>
Title<p>${emp.title}</p>
Price<p>${emp.price}</p>
<input type="submit" value="Buy No" class="bluebutton"/>
</c:forEach>
編輯
我想在像如果我15條記錄,然後將有5行的行可以顯示最多3條記錄,。並與排將有3列問題是,當我寫這樣
<tr> <td>${tile}</td> <td>price<td>
在
for循環它顯示第一條記錄3次,但我想在每列的新紀錄。
這是我修改後的代碼:
<% int size=Integer.parseInt(request.getAttribute("size").toString());
%>
<h1><%= size%></h1>
<table border="1" width="50%">
<tr >
<c:set var="num" value="0"></c:set>
<c:forEach items="${requestScope.Products}" var="emp" begin="0" end="${size}" varStatus="loop">
<c:choose>
<c:when test="${num==3}">
<tr>
</tr>
<c:set var="num" value="0"></c:set>
</c:when>
<c:otherwise>
<td width="100">
<img src="images/${emp.image}" width="100" height="100"/>
Title<p>${emp.title}</p>
Price<p>${emp.price}</p>
<input type="submit" value="Buy No" class="bluebutton"/>
</td>
</c:otherwise>
</c:choose>
<c:set var="num" value="${num + 1}" />
<h2><c:out value="${num}"></c:out></h2>
</c:forEach>
</tr>
</table>
** 「你好」 當我== 3 **哪裏是'i'在你的代碼? – Braj
如果代碼中有'int var',目的是什麼? – Braj
通過var我得到的數值,這是jstl變量 – user3638008