Java 在Scriplet中访问JSTL变量
嗨,我想在scriplet中使用jstl变量,我想在num==3时打印一个“hello”,它试图获取以下代码,但i的值仍然为零 我想获取Java 在Scriplet中访问JSTL变量,java,jsp,jstl,el,Java,Jsp,Jstl,El,嗨,我想在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=
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>
你好
Title${emp.Title}
价格${emp.Price}
编辑 我想在一行中显示最多3条记录,就像如果我有15条记录,那么将有5行,。行中有3列,问题是当我这样写的时候
<tr> <td>${tile}</td> <td>price<td>
${tile}价格
在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>
Title${emp.Title}
价格${emp.Price}
你好
我想获取num的值并将其递增1,然后检查num==3,然后在条件为真时打印hello,然后再次将值重新计算为零
阅读内联评论了解更多信息
示例代码:(根据您的要求进行修改)
你好
编辑 我想在一行中显示最多3条记录,就像如果我有15条记录,那么将有5行,。行中有3列
<c:set var="Products" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" scope="request"/>
<c:set var="beginTR" value="true" /> <!-- to check for tr start -->
<table border="1">
<c:forEach items="${requestScope.Products}" var="emp"
varStatus="status">
<c:if test="${status.index%3==0}"> <!-- check for columns no -->
<c:if test="${beginTR}">
<tr>
<c:set var="beginTR" value="false" />
</c:if>
<c:if test="${!beginTR}">
</tr>
<c:set var="beginTR" value="true" />
</c:if>
</c:if>
<td>
<c:out value="${emp}"></c:out> <!-- Fit your actual code here -->
</td>
</c:forEach>
</table>
截图:
“你好”当i==3时,代码中的
i
在哪里?如果代码中的int var
有什么作用?通过var,我得到num的值,这是jstl变量,在您的情况下,当num
为零时,您的代码中显示的是什么?始终为零
您在代码中的任何地方都没有修改它c:if和c:when之间的区别在这里找到答案时,您可以使用c:when
以及c:others
,就像Java中的if-else
一样非常感谢您的支持
<c:set var="num" value="0"></c:set> <!-- initial value -->
<c:forEach items="${requestScope.Products}" var="emp">
<c:if test="${num==3}">
<h1>hello</h1>
<c:set var="num" value="0"></c:set> <!-- re-initialize value -->
</c:if>
<c:set var="num" value="${num + 1}" /> <!-- increment value -->
<h2>
<c:out value="${num}"></c:out>
</h2>
</c:forEach>
<c:set var="Products" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" scope="request"/>
<c:set var="beginTR" value="true" /> <!-- to check for tr start -->
<table border="1">
<c:forEach items="${requestScope.Products}" var="emp"
varStatus="status">
<c:if test="${status.index%3==0}"> <!-- check for columns no -->
<c:if test="${beginTR}">
<tr>
<c:set var="beginTR" value="false" />
</c:if>
<c:if test="${!beginTR}">
</tr>
<c:set var="beginTR" value="true" />
</c:if>
</c:if>
<td>
<c:out value="${emp}"></c:out> <!-- Fit your actual code here -->
</td>
</c:forEach>
</table>