如何在JSP页面的scripts标签中使用<c:foreach>?</c:foreach>

时间:2013-08-29 05:32:45

标签: javascript jquery jsp foreach jstl

嘿如何在jsp页面中使用循环标记?

我想使用JSTL数据传入数据表

我的代码就像:

        $(document).ready(function() {


            /* Init DataTables */
            var startString = "[";
            var mainString = "";
            var endString = "]";


            var temp = ${k.size()};
        <c:forEach items="${k}" var="stdn" varStatus="status">
            temp--;
            if (temp === 0) {
                mainString = mainString + "{key:\"" + "${stdn.key}" + "\",name:\"" + "${stdn.value.name}" + "\",rollno:\"" + "${stdn.value.rollNo}" + "\",marks:\"" + "${stdn.value.marks}" + "\"}";
            } else {
                mainString = mainString + "{key:\"" + "${stdn.key}" + "\",name:\"" + "${stdn.value.name}" + "\",rollno:\"" + "${stdn.value.rollNo}" + "\",marks:\"" + "${stdn.value.marks}" + "\"},";
            }
        </c:forEach>
                var finalString = startString + mainString + endString;
                var final = eval(finalString);

1 个答案:

答案 0 :(得分:19)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:forEach>YOUR CODE </title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
   NAME <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>

这将产生以下结果:

NAME 1
NAME 2
NAME 3
NAME 4
NAME 5

以上是最简单的例子.. 以下是项目var

<table>
      <c:forEach var="student" items="${person.person}" varStatus="counter">
        <c:choose>
          <c:when test="${counter.count % 2 == 0}">
            <c:set var="rowStyle" scope="page" value="odd"/>
          </c:when>
          <c:otherwise>
            <c:set var="rowStyle" scope="page" value="even"/>
          </c:otherwise>
        </c:choose>
        <tr class="ÃÂ${rowStyle}">
          <td>${student.name}</td>
          <td>${student.age}</td>
          <td>${student.height}</td>
        </tr>
      </c:forEach>
    </table>

这样你可以使用<c:forEach> </c:forEach> TAG ..

如果您有任何具体问题,请解释