Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

javascript - how to Use <c:forEach> in scripts tag on JSP page?

Hey How to use loop in tag in jsp page?

i want to use JSTL data to pass in data tables

my code is like :

        $(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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<%@ 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>

This would produce following result:

NAME 1
NAME 2
NAME 3
NAME 4
NAME 5

Above is simplest example.. following is with items 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>

this way you can use the <c:forEach> </c:forEach> TAG..

If you have any specific problem then please explain


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...