jstl <c:foreach>標籤

這個標籤是通過腳本嵌入Java實現forwhile迴圈的一個好的選擇。 <c:forEach>標籤是一個常用的標籤,使用它來迭代一組對象。 <c:forTokens>標籤用於將字串分割成令牌,並遍曆每個令牌。

屬性

<c:foreach>標籤具有以下屬性 -

屬性 描述 必需 默認
delims 用作分隔符號的字元 None

檔:c_foreach.jsp -

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>c:foreach 示例</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <c:forEach var="i" begin="1" end="10">
         當前值: <c:out value="${i}" />
            <br />
        </c:forEach>
    </div>

</body>
</html>

執行上面示例代碼,得到以下輸出結果 -

當前值: 1
當前值: 2
當前值: 3
當前值: 4
當前值: 5
當前值: 6
當前值: 7
當前值: 8
當前值: 9
當前值: 10

c:forTokens 示例

迭代令牌,由提供的分隔符號分隔。

檔:fortokens.jsp -

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>c:fortoken示例</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <c:forTokens items = "Java,Python,Jsp,Servlet" delims = "," var = "name">
         <c:out value = "${name}"/><br/>
      </c:forTokens>
    </div>

</body>
</html>

執行上面示例代碼,得到以下輸出結果 -

Java
Python
Jsp
Servlet

上一篇: Jstl教學 下一篇: JSP+MySQL實例