2014-05-18 53 views
0

您好我想在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> 
+0

** 「你好」 當我== 3 **哪裏是'i'在你的代碼? – Braj

+0

如果代碼中有'int var',目的是什麼? – Braj

+0

通過var我得到的數值,這是jstl變量 – user3638008

回答

1

我想要得到num的值,並通過1增加它然後檢查如果num == 3再打印一次打招呼條件爲真又值reassgin爲零

閱讀行內意見了解更多信息。

示例代碼:(修改它按照您的要求)

<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> 

編輯

我想在一行中顯示最多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> 

截圖:

enter image description here

+0

c:if和c之間有什麼區別? – user3638008

+0

在這裏找到答案[jstl_core_choose_tag](http://www.tutorialspoint.com/jsp/jstl_core_choose_tag.htm) – Braj

+0

你可以使用'c:when'和' c:否則'就像'if-else'在JAVA – Braj

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>