<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # JSTL `forEach`標簽 > 原文: [http://zetcode.com/articles/jstlforeach/](http://zetcode.com/articles/jstlforeach/) JSTL `forEach`教程展示了如何使用 JSTL 庫中的`forEach`標簽。 ## JSTL JavaServerPages 標準標記庫(JSTL)是有用的 JSP 標記的集合,這些標記提供了許多 JSP 應用所共有的核心功能。 ## `forEach`標簽 JSTL `<c:foreach>`標簽是基本的迭代標簽。 它遍歷各種 Java 集合類型。 `<c:foreach>`標記包含以下屬性: * `items` - 要迭代的項目集合 * `begin` - 起始項目的索引 * `end` - 結束項目的索引 * `step` - 迭代步驟 * `var` - 迭代中當前項目的變量 ## `forEach` taglib 聲明 `<c:foreach>`標記屬于核心 JSTL 標記。 ```java <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> ``` 要使用標簽,我們需要包含此聲明。 ## JSTL Maven 依賴項 要使用 JSTL 庫,我們需要以下 Maven 依賴項: ```java <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> ``` ## `forEach`標簽示例 以下 JSP 頁面包含`<c:foreach>`標記。 除了`<c:foreach>`標簽之外,我們還使用`<c:out>`顯示變量。 `index.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP Page</title> </head> <body> <c:forEach var="counter" begin="1" end="8"> <c:out value="${counter}"/> </c:forEach> </body> </html> ``` 該示例在輸出中顯示值`1..8`。 ## `forEach`標簽示例 II 下一個 JSP 示例讀取從鏈接發送的參數。 `index.html` ```java <!DOCTYPE html> <html> <head> <title>Start Page</title> <meta charset="UTF-8"> </head> <body> <a href="target.jsp?name=Jane&age=23&occupation=accountant">Show page</a> </body> </html> ``` `index.html`頁面包含一個鏈接,該鏈接將三個參數發送到`target.jsp`頁面。 `target.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP Page</title> </head> <body> <c:forEach var="par" items="${param}"> <c:out value="${par.key}"/>: <c:out value="${par.value}"/> <br> </c:forEach> </body> </html> ``` JSP 頁面在隱式`param`對象(它是一個映射)中接收參數。 ```java <c:forEach var="par" items="${param}"> <c:out value="${par.key}"/>: <c:out value="${par.value}"/> <br> </c:forEach> ``` 我們遍歷映射并打印鍵/值對。 ## `forEach`標簽示例 III HTML `<select>`是提供選項菜單的控件。 借助其`multiple`屬性,用戶可以從控件中選擇多個值。 `index.html` ```java <!DOCTYPE html> <html> <head> <title>Languages</title> <meta charset="UTF-8"> </head> <body> <form action="target.jsp"> <div>Select languages:</div> <select name="languages" size="7" multiple="multiple"> <option value='Ada'>Ada</option> <option value='C'>C</option> <option value='C++'>C++</option> <option value='Cobol'>Cobol</option> <option value='Eiffel'>Eiffel</option> <option value='Objective-C'>Objective-C</option> <option value='Java'>Java</option> </select> <button type="submit">Submit</button> </form> </body> </html> ``` 我們創建一個`<select>`控件,其中包含七個值。 當我們提交表單時,選定的值將發送到`target.jsp`文件。 `target.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Languages</title> </head> <body> <c:forEach items="${paramValues.languages}" var="lang"> <c:out value="${lang}"/> </c:forEach> </body> </html> ``` 可從隱式`paramValues`對象(它是一個映射)獲得`<select>`控件的值。 關鍵是請求參數名稱(`languages`),值在字符串數組中。 ```java <c:forEach items="${paramValues.languages}" var="lang"> <c:out value="${lang}"/> </c:forEach> ``` 我們遍歷數組并打印其元素。 ## `forEach`標簽示例 IV 以下示例在 HTML 表中顯示數據。 `index.html` ```java <!DOCTYPE html> <html> <head> <title>Start Page</title> <meta charset="UTF-8"> </head> <body> <p> <a href="MyServlet">Show all cities</a> </p> </body> </html> ``` 在`index.html`頁面中,我們有一個調用`MyServlet`的鏈接。 Servlet 使用服務方法加載數據,并分派到 JSP 頁面。 `City.java` ```java package com.zetcode.bean; public class City { private Long id; private String name; private int population; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public City(Long id, String name, int population) { this.id = id; this.name = name; this.population = population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } } ``` 這是`City`類; 它包含`id`,`name`和`population`屬性。 `MyServlet.java` ```java package com.zetcode.web; import com.zetcode.bean.City; import com.zetcode.service.CityService; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"}) public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); List<City> cities = CityService.getAllCities(); request.setAttribute("cities", cities); request.getRequestDispatcher("showCities.jsp").forward(request, response); } } ``` Servlet 使用`CityService.getAllCities()`讀取數據,使用`setAttribute()`將列表對象設置為屬性,然后轉發到`showCities.jsp`。 `CityService.java` ```java package com.zetcode.service; import com.zetcode.bean.City; import java.util.ArrayList; import java.util.List; public class CityService { public static List<City> getAllCities() { List<City> cities = new ArrayList<>(); cities.add(new City(1L, "Bratislava", 432000)); cities.add(new City(2L, "Budapest", 1759000)); cities.add(new City(3L, "Prague", 1280000)); cities.add(new City(4L, "Warsaw", 1748000)); cities.add(new City(5L, "Los Angeles", 3971000)); cities.add(new City(6L, "New York", 8550000)); cities.add(new City(7L, "Edinburgh", 464000)); cities.add(new City(8L, "Berlin", 3671000)); return cities; } } ``` `getAllCities()`方法返回城市列表。 `showCities.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Cities</title> </head> <body> <h2>Cities</h2> <table> <thead> <tr> <th>Id</th> <th>Name</th> <th>Population</th> </tr> </thead> <tbody> <c:forEach items="${cities}" var="city"> <tr> <td>${city.id}</td> <td>${city.name}</td> <td>${city.population}</td> </tr> </c:forEach> </tbody> </table> </body> </html> ``` 在`showCities.jsp`中,我們在帶有`<c:forEach>`標簽的 HTML 表格中顯示城市。 ```java <td>${city.id}</td> <td>${city.name}</td> <td>${city.population}</td> ``` 使用點運算符從城市對象讀取屬性。 在本教程中,我們介紹了 JSTL 庫中的`<c:forEach>`標簽。 您可能也會對以下相關教程感興趣: [Java servlet JSON 教程](/articles/javaservletjson/), [Java servlet 復選框教程](/articles/javaservletcheckbox/), [Java servlet 圖像教程](/articles/javaservletimage/), [Java Servlet HTTP 標頭](/articles/javaservlethttpheaders/)或 [Java 教程](/lang/java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看