【问题标题】:Accessing JSTL variable in Scriplet在 Scriptlet 中访问 JSTL 变量
【发布时间】: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 为零时,您的预期输出是什么
  • 它始终为零 &lt;c:set var="num" value="0"&gt;&lt;/c:set&gt; 你没有在代码中的任何地方修改它

标签: java jsp jstl el


【解决方案1】:

我想获取 num 的值并将其递增 1,然后检查是否 num==3,然后在条件为真时打印 hello 再次将值重新设置为零

阅读内联 cmets 了解更多信息。

示例代码:(根据您的要求修改)

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

截图:

【讨论】:

  • c:if 和 c:when 有什么区别
  • 在这里找到答案jstl_core_choose_tag
  • 您可以将c :whenc : otherwise 一起使用,就像JAVA 中的if-else 一样
  • 非常感谢您的支持
【解决方案2】:
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    相关资源
    最近更新 更多