【发布时间】:2014-05-18 13:19:37
【问题描述】:
嗨,我想在 scriplet 中使用 jstl 变量,我想在 num==3 时打印一个“hello”,它尝试获取以下代码,但 i 的值仍然为零。
我想获取 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>
编辑
我想在一行中显示最多 3 条记录,如果我有 15 条记录,那么将有 5 行。并且在行中会有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>
【问题讨论】:
-
"hello" when i==3 你的代码中的
i在哪里? -
如果你的代码中有
int var,目的是什么? -
通过 var 我得到了 num 的值,它是 jstl 变量
-
当您的代码中显示
num为零时,您的预期输出是什么 -
它始终为零
<c:set var="num" value="0"></c:set>你没有在代码中的任何地方修改它