Reputation: 1
Hi i want to use jstl variable in scriplet i I want to print a "hello" when num==3 it tried get the following code but value of i remains zero.
I want get the value of num
and increment it by 1 then check if num==3
then print hello once condition is true reassign value again to zero.
<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>
EDIT
I want to display max 3 records in a row like if i 15 records then there will be 5 rows,. and with in row there will be 3 columns problem is when i write like this
<tr> <td>${tile}</td> <td>price<td>
in a for loop it shows first record 3 times but i want new record in each column.
Here is my modified code:
<% 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>
Upvotes: 0
Views: 906
Reputation: 46841
I want get the value of num and increment it by 1 then check if num==3 then print hello once condition is true reassgin value again to zero
Read inline comments for more info.
Sample code: (Modify it as per your requirement)
<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>
EDIT
I want to display max 3 records in a row like if i 15 records then there will be 5 rows,. and with in row there will be 3 columns.
<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>
screenshot:
Upvotes: 1
Reputation: 11579
<c:forEach items="${Products}" var="emp" begin="0" end="${size}" varStatus="status" >
<c:if test="${status.index==3}">
<h1>hello</h1>
</c:if>
</c:forEach>
Upvotes: 0