0

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>
Braj
  • 46,415
  • 5
  • 60
  • 76
  • **"hello" when i==3** where is `i` in your code? – Braj May 18 '14 at 16:03
  • What is the purpose if `int var` in your code? – Braj May 18 '14 at 17:16
  • through var i am getting value of num which is jstl variable – user3638008 May 18 '14 at 18:42
  • What is expected output in your case when `num` is zero as shown in your code – Braj May 18 '14 at 18:43
  • Its always zero `` You are not modifying it any where in your code – Braj May 18 '14 at 18:45
  • You have assigned it to another int variable `var` and incrementing `var` will not impact the value of `num` as happen in JAVA as well. – Braj May 18 '14 at 18:46
  • 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 – user3638008 May 18 '14 at 18:49
  • OK Let me share you code – Braj May 18 '14 at 18:50
  • actually i want to display records in a grid view. i am developing a ecommerce site where i want to display items in form of grid as on other sites so can you help in that case – user3638008 May 18 '14 at 19:10
  • Where did you stuck? simply use ``
    – Braj May 18 '14 at 19:14
  • i am confused with for loop – user3638008 May 18 '14 at 19:16
  • Look at [How to create table dynamically using count and JSTL ForEach](http://stackoverflow.com/questions/14783572/how-to-create-table-dynamically-using-count-and-jstl-foreach) Find more on Google. – Braj May 18 '14 at 19:18
  • 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 ${tile} price in a for loop it shows first record 3 times but i want new record in each column – user3638008 May 18 '14 at 19:21
  • Let me share you a sample code. – Braj May 18 '14 at 19:22
  • I TRIED THIS BUT IT SHOWS 3 RECORDS IN 1ST ROW THEN 2 RECORDS EACH ROW – user3638008 May 18 '14 at 19:42
  • Just copy my sample code. It works that's why I have sharead screenshots as well. – Braj May 18 '14 at 19:43
  • Fit your code in my sample code. **Don't modify your question** Otherwise there will be no meaning of my last answer. Instead use EDIT section. Please revert back your post with new Edits. – Braj May 18 '14 at 19:43
  • I have already shared you a link. Just do some exercise with sample code then apply your knowledge in your actual code. – Braj May 18 '14 at 19:45
  • Look at my post and update `Fit your actual code here` part only. – Braj May 18 '14 at 19:56
  • in your code it shows 3 records in first row but after it shows only 2 records per row – user3638008 May 19 '14 at 05:49
  • Post your modified code. Change it in EDIT section only. – Braj May 19 '14 at 05:54

2 Answers2

1

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:

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
0
<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>
Alex
  • 11,451
  • 6
  • 37
  • 52