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 标记。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
要使用标签,我们需要包含此声明。
JSTL Maven 依赖项
要使用 JSTL 库,我们需要以下 Maven 依赖项:
<dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency>
forEach标签示例
以下 JSP 页面包含<c:foreach>标记。 除了<c:foreach>标签之外,我们还使用<c:out>显示变量。
index.jsp
<%@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
<!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
<%@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对象(它是一个映射)中接收参数。
<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
<!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
<%@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),值在字符串数组中。
<c:forEach items="${paramValues.languages}" var="lang"><c:out value="${lang}"/></c:forEach>
我们遍历数组并打印其元素。
forEach标签示例 IV
以下示例在 HTML 表中显示数据。
index.html
<!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
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
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 {@Overrideprotected 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
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
<%@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 表格中显示城市。
<td>${city.id}</td><td>${city.name}</td><td>${city.population}</td>
使用点运算符从城市对象读取属性。
在本教程中,我们介绍了 JSTL 库中的<c:forEach>标签。
您可能也会对以下相关教程感兴趣: Java servlet JSON 教程, Java servlet 复选框教程, Java servlet 图像教程, Java Servlet HTTP 标头或 Java 教程。
