javaWeb - EL 表达式 & JSTL 标签库

EL 表达式 & JSTL 标签库

EL 表达式

什么是 EL 表达式,EL 表达式的作用?

<%--
    a)什么是 EL 表达式,EL 表达式的作用?
        EL 表达式的全称是:Expression Language。是表达式语言。
        EL 表达式的什么作用:EL 表达式主要是代替 jsp 页面中的表达式脚本在 jsp 页面中进行数据的输出。
        因为 EL 表达式在输出数据的时候,要比 jsp 的表达式脚本要简洁很多。
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-a.jsp</title>
</head>
<body>
    <%
        request.setAttribute("key","值");
    %>
    <%--
        表达式脚本输出 key 的值是: <%=request.getAttribute("key1")%><br/>
    --%>
    表达式脚本输出 key 的值是: <%=request.getAttribute("key1")==null ? "" : request.getAttribute("key1")%><br/>
    EL 表达式输出 key 的值是: ${key1}
    <%--
        EL 表达式的格式是:${表达式}
        EL 表达式在输出 null 值的时候,输出的是空串。jsp 表达式脚本输出 null 值的时候,输出的是 null 字符串。
    --%>
</body>
</html>

EL 表达式搜索域数据的顺序

<%--
  b) EL 表达式搜索域数据的顺序
    EL 表达式主要是在 jsp 页面中输出数据。
        主要是输出域对象中的数据。
        当四个域中都有相同的 key 的数据的时候,EL 表达式会按照四个域的从小到大的顺序去进行搜索,找到就输出。
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-b.jsp</title>
</head>
<body>
    <%
        // 往四个域中都保存了相同的 kye 的数据。
        pageContext.setAttribute("key","pageContext");
        request.setAttribute("key","request");
        session.setAttribute("key","session");
        application.setAttribute("key","application");
    %>
    ${ key }
</body>
</html>

EL 表达式输出 Bean 的普通属性,数组属性。List 集 合属性,map 集合属性

Person.java
package com.sq.pojo;

import java.util.List;
import java.util.Map;

/**
 * i. 需求——输出 Person 类中普通属性,数组属性。list 集合属性和 map 集合属性。
 */
public class Person {

	private String name;
	private List<String> phones;
	private List<String> cities;
	private Map<String,Object> map;
//	private int age = 18;

	public int getAge(){
		return 18;
	}

	public Person() {
	}

	public Person(String name, List<String> phones, List<String> cities, Map<String, Object> map) {
		this.name = name;
		this.phones = phones;
		this.cities = cities;
		this.map = map;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<String> getPhones() {
		return phones;
	}

	public void setPhones(List<String> phones) {
		this.phones = phones;
	}

	public List<String> getCities() {
		return cities;
	}

	public void setCities(List<String> cities) {
		this.cities = cities;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	@Override
	public String toString() {
		return "Person{" +
				"name=" + name +
				", phones=" + phones +
				", cities=" + cities +
				", map=" + map +
				'}';
	}
}

输出的代码:
<%@ page import="com.sq.pojo.Person" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-c.jsp</title>
</head>
<body>
    <%
        Person person = new Person();
        person.setName("前哥好帅!!!");
        person.setPhones(Arrays.asList(new String[]{"11111111111", "22222222222", "33333333333"}));

        List<String> cities = new ArrayList<String>();
        cities.add("北京");
        cities.add("上海");
        cities.add("深圳");
        person.setCities(cities);

        Map<String,Object> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        person.setMap(map);

//        pageContext.setAttribute("person",person);
        pageContext.setAttribute("p",person);
    %>
    <%--
        根据属性的 getXxx() 方法获取值
    --%>
    输出 Person: ${ p }<br/>
    输出 Person 的 name 属性: ${ p.name }<br/>

    输出 Person 的 phones 数组的属性值: ${ p.phones[0] }<br/>
    输出 Person 的 phones 数组的属性值: ${ p.phones[1] }<br/>
    输出 Person 的 phones 数组的属性值: ${ p.phones[2] }<br/>

    输出 Person 的 cities 集合中的元素值: ${ p.cities }<br/>
    输出 Person 的 List 集合中个别元素值: ${ p.cities[0] }<br/>
    输出 Person 的 List 集合中个别元素值: ${ p.cities[1] }<br/>
    输出 Person 的 List 集合中个别元素值: ${ p.cities[2] }<br/>

    输出 Person 的 Map 集合: ${ p.map }<br/>
    输出 Person 的 Map 集合中某个 key 的值: ${ p.map.key1 }<br/>
    输出 Person 的 Map 集合中某个 key 的值: ${ p.map.key2 }<br/>
    输出 Person 的 Map 集合中某个 key 的值: ${ p.map.key3 }<br/>

    输出 Person 的 age 属性: ${ p.age }<br/>

</body>
</html>

EL 表达式——运算

<%--
  d) EL 表达式——运算
        语法:${ 运算表达式 } , EL 表达式支持如下运算符:
        1)关系运算
        关系运算符 说 明 范 例 结果
            == 或 eq 等于 ${ 5 == 5 } 或 ${ 5 eq 5 } true
            != 或 ne 不等于 ${ 5 !=5 } 或 ${ 5 ne 5 } false
            < 或 lt 小于 ${ 3 < 5 } 或 ${ 3 lt 5 } true
            > 或 gt 大于 ${ 2 > 10 } 或 ${ 2 gt 10 } false
            <= 或 le 小于等于 ${ 5 <= 12 } 或 ${ 5 le 12 } true
            >= 或 ge 大于等于 ${ 3 >= 5 } 或 ${ 3 ge 5 } false
        2)逻辑运算
        逻辑运算符 说 明 范 例 结果
            && 或 and 与运算 ${ 12 == 12 && 12 < 11 } 或 ${ 12 == 12 and 12 < 11 } false
            || 或 or 或运算 ${ 12 == 12 || 12 < 11 } 或 ${ 12 == 12 or 12 < 11 } true
            ! 或 not 取反运算 ${ !true } 或 ${not true } false
        3)算数运算
        算数运算符 说 明 范 例 结果
            + 加法 ${ 12 + 18 } 30
            - 减法 ${ 18 - 8 } 10
            * 乘法 ${ 12 * 12 } 144
            / 或 div 除法 ${ 144 / 12 } 或 ${ 144 div 12 } 12
            % 或 mod 取模 ${ 144 % 10 } 或 ${ 144 mod 10 } 4

--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-d.jsp</title>
</head>
<body>
<%-- 关系运算 --%>
    ${ 12 == 12 } 或 ${ 12 eq 12 }<br/>
    ${ 12 != 12 } 或 ${ 12 ne 12 }<br/>
    ${ 12 < 12 } 或 ${ 12 lt 12 }<br/>
    ${ 12 > 12 } 或 ${ 12 gt 12 }<br/>
    ${ 12 <= 12 } 或 ${ 12 le 12 }<br/>
    ${ 12 >= 12 } 或 ${ 12 ge 12 }<br/>

    <hr><%-- 分割线 --%>

<%-- 逻辑运算 --%>
    ${ 12 == 12 && 12 > 11 } 或 ${ 12 == 12 and 12 > 11 }<br/>
    ${ 12 == 12 || 12 > 11 } 或 ${ 12 == 12 or 12 > 11 }<br/>
    ${ !true } 或 ${ not true }<br/>

    <hr><%-- 分割线 --%>

<%-- 算术运算 --%>
    ${ 12 + 12 }<br/>
    ${ 12 - 12 }<br/>
    ${ 12 * 12 }<br/>
    ${ 18 / 12 } 或 ${ 18 div 12 }<br/><%-- 1.5 或 1.5 --%>
    ${ 18 % 12 } 或 ${ 18 mod 12 }<br/>

</body>
</html>

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %><%--
  i. empty 运算
    empty 运算可以判断一个数据是否为空,如果为空,则输出 true,不为空输出 false。
    以下几种情况为空:
        1、值为 null 值的时候,为空
        2、值为空串的时候,为空
        3、值是 Object 类型数组,长度为零的时候
        4、list 集合,元素个数为零
        5、map 集合,元素个数为零
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-e.jsp</title>
</head>
<body>
    <%
//        1、值为 null 值的时候,为空
        request.setAttribute("emptyNull",null);
//        request.setAttribute("emptyNull",new Object());
//        2、值为空串的时候,为空
        request.setAttribute("emptyStr","");
//        request.setAttribute("emptyStr"," ");/* "" 中间不能有任何内容 */
//        3、值是 Object 类型数组,长度为零的时候
        request.setAttribute("emptyArr",new Object[]{});
//        request.setAttribute("emptyArr",new Object[]{""});
//        4、list 集合,元素个数为零
        List<String> list = new ArrayList<>();
//        list.add("");
        request.setAttribute("emptyList",list);
//        5、map 集合,元素个数为零
        Map<String,Object> map = new HashMap<String,Object>();
//        map.put("key1","value1");
        request.setAttribute("emptyMap",map);
    %>
    ${ empty emptyNull }<br/><%-- 为空显示: true --%>
    ${ empty emptyStr }<br/>
    ${ empty emptyArr }<br/>
    ${ empty emptyList }<br/>
    ${ empty emptyMap }<br/>

    <hr/>
<%--
    ii. 三元运算
    表达式 1?表达式 2:表达式 3
        如果表达式 1 的值为真,返回表达式 2 的值,
        如果表达式 1 的值为假,返回表达式 3 的值。
--%>
    ${ 12 == 12 ? "前哥帅呆":"前哥骗人!" }<br/>
    ${ 12 != 12 ? "前哥帅呆":"前哥骗人!" }<br/>

</body>
</html>

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%--
  iii. “.”点运算 和 [] 中括号运算符
    .点运算,可以输出 Bean 对象中某个属性的值。
    []中括号运算,可以输出有序集合中某个元素的值。
    并且[]中括号运算,还可以输出 map 集合中 key 里含有特殊字符的 key 的值。
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-f.jsp</title>
</head>
<body>
    <%
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("a.a.a","aaaValue");
        map.put("b+b+b","bbbValue");
        map.put("c-c-c","cccValue");

        request.setAttribute("map",map);
    %>
<%--    ${ map }<br/>    --%>
<%--    ${ map.aaa }<br/>    --%>
    ${ map['a.a.a'] }<br/><%-- '' 或 "" 都行 --%>
<%--    ${ map["a.a.a"] }<br/>--%>
<%--    ${ map.b+b+b }<br/> 0:找不到用 0 代替  0+0+0=0 --%>
    ${ map['b+b+b'] }<br/>
    ${ map['c-c-c'] }<br/>
</body>
</html>

EL 表达式的 11 个隐含对象

<%--
    e)  EL 表达式的 11 个隐含对象
        EL 个达式中 11 个隐含对象,是 EL 表达式中自己定义的,可以直接使用。
        变量                          类型                      作用
        pageContext              PageContextImpl            它可以获取 jsp 中的九大内置对象
        pageScope                Map<String,Object>         它可以获取 pageContext 域中的数据
        requestScope             Map<String,Object>         它可以获取 Request 域中的数据
        sessionScope             Map<String,Object>         它可以获取 Session 域中的数据
        applicationScope         Map<String,Object>         它可以获取 ServletContext 域中的数据
        param                    Map<String,String>         它可以获取请求参数的值
        paramValues              Map<String,String[]>       它也可以获取请求参数的值,获取多个值的时候使用。
        header                   Map<String,String>         它可以获取请求头的信息
        headerValues             Map<String,String[]>       它可以获取请求头的信息,它可以获取多个值的情况
        cookie                   Map<String,Cookie>         它可以获取当前请求的 Cookie 信息

  i. EL 获取四个特定域中的属性
        pageScope ====== pageContext 域
        requestScope ====== Request 域
        sessionScope ====== Session 域
        applicationScope ====== ServletContext 域
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-scope.jsp</title>
</head>
<body>
    <%
        pageContext.setAttribute("key1","pageContext1");
        pageContext.setAttribute("key2","pageContext2");
        request.setAttribute("key2","request");
        session.setAttribute("key2","session");
        application.setAttribute("key2","application");
    %>
<%--    ${ key1 }<br/>  --%>
<%--    ${ key2 }<br/>相同时,输出最小的,无选择性  --%>
<%--    ${ pageScope.key1 }<br/>    --%>
    ${ pageScope.key2 }<br/><%--pageContext2--%>
<%--    ${ requestScope }<br/> 结果:{key2=request} --%>
    ${ requestScope.key2 }<br/><%--request--%>
    ${ sessionScope.key2 }<br/><%--session--%>
    ${ applicationScope.key2 }<br/><%--application--%>

</body>
</html>

<%--
  ii. pageContext 对象的使用 (动态获取,地址改变,获取的值立刻改变)
        1. 协议:
        2. 服务器 ip:
        3. 服务器端口:
        4. 获取工程路径:
        5. 获取请求方法:
        6. 获取客户端 ip 地址:
        7. 获取会话的 id 编号:
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-pageContext.jsp</title>
</head>
<body>
    <%--
        request.getScheme() 它可以获取请求的协议
        request.getServerName() 获取请求的服务器 ip 或域名
        request.getServerPort() 获取请求的服务器端口号
        getContextPath() 获取当前工程路径
        request.getMethod() 获取请求的方式(GET 或 POST)
        request.getRemoteHost() 获取客户端的 ip 地址
        session.getId() 获取会话的唯一标识
    --%>
<%--    <%=request.getScheme()%><br/>  http  --%>
<%--    <%=request.getServerName()%><br/>  localhost  --%>
<%--    <%=request.getServerPort()%><br/>  8080  --%>
<%--    <%=request.getContextPath()%><br/>  /EL_JSTL_09  --%>
<%--    <%=request.getMethod()%><br/>  GET  --%>
<%--    <%=request.getRemoteHost()%><br/>  127.0.0.1  --%>
<%--    <%=session.getId()%><br/>  B4779FA0D152342AF8435AF6A050F90E  --%>

    <%--    企业里采用这种方式
        <%
            pageContext.setAttribute("req",request);
        %>
        1. 协议:${ req.scheme }<br/> 显示: http
    --%>
    1. 协议:${ pageContext.request.scheme }<br/><%--http--%>
    2. 服务器 ip:${ pageContext.request.serverName }<br/><%--localhost--%>
    3. 服务器端口:${ pageContext.request.serverPort }<br/><%--8080--%>
    4. 获取工程路径:${ pageContext.request.contextPath }<br/><%--/EL_JSTL_09--%>
    5. 获取请求方法:${ pageContext.request.method }<br/><%--GET--%>
    6. 获取客户端 ip 地址:${ pageContext.request.remoteHost }<br/><%--127.0.0.1--%>
    7. 获取会话的 id 编号:${ pageContext.session.id }<br/><%--B4779FA0D152342AF8435AF6A050F90E--%>
</body>
</html>

<%--
  iii. EL 表达式其他隐含对象的使用
        param           Map<String,String>       它可以获取请求参数的值
        paramValues     Map<String,String[]>     它也可以获取请求参数的值,获取多个值的时候使用。
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-other_el_obj.jsp</title>
</head>
<body>
<%--
    http://localhost:8080/EL_JSTL_09/other_el_obj.jsp?a=1
                                                     ?参数名=参数
                                                     参数名 = Map 中的 key
--%>
<%--    ${ param } 显示:{} 表示 map 集合    --%>
<%--
    通过 ?参数名=参数值 输入
    ?username=sq168&password=123456
--%>
    输出请求参数 username 的值: ${ param.username }<br/><%--只有一个值的时候使用--%>
    输出请求参数 password 的值: ${ param.password }<br/>
<%--    ${ paramValues }<br/> 显示数组地址    --%>
<%--    ${ param.hobby }<br/> 只显示一个值 --%>
    输出请求参数 username 的值: ${ paramValues.username[0] }<br/>
    输出请求参数 username 的值: ${ paramValues.hobby[0] }<br/>
    输出请求参数 username 的值: ${ paramValues.hobby[1] }<br/>

    <hr/>

<%--
    header Map<String,String> 它可以获取请求头的信息
    headerValues Map<String,String[]> 它可以获取请求头的信息,它可以获取多个值的情况
--%>
<%--    ${ header }<br/>    --%>
    输出请求头[User-Agent]的值: ${ header['User-Agent'] }<br/>
    输出请求头[Connection]的值: ${ header.Connection }<br/>
    输出请求头[Connection]的值: ${ header['Connection'] }<br/>
    输出请求头[User-Agent]的值: ${ headerValues['User-Agent'][0] }<br/>

    <hr/>
    ${ cookie }<br/>
    ${ cookie.JSESSIONID }<br/>
<%--
    cookie Map<String,Cookie> 它可以获取当前请求的 Cookie 信息
--%>
    获取 Cookie 的名称: ${ cookie.JSESSIONID.name }<br/>
    获取 Cookie 的值: ${ cookie.JSESSIONID.value }<br/>
    <hr/>

    ${ initParam }<br/><%--{url=jdbc:mysql:///test, username=root}--%>
<%--
    initParam Map<String,String> 它可以获取在 web.xml 中配置的<context-param>上下文参数
--%>
    输出&lt;Context-param&gt;username 的值: ${ initParam.username }<br/><%--输出<Context-param>username 的值: root--%>
    输出&lt;Context-param&gt;url 的值: ${ initParam.url }<br/><%--输出<Context-param>url 的值: jdbc:mysql:///test--%>

</body>
</html>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
<!-- 修改 web.xml 的时候,一定要重新部署 -->
    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <!--"///"表示省略了 localhost:3306 等同于:jdbc:mysql://localhost:3306/test -->
        <param-value>jdbc:mysql:///test</param-value>
    </context-param>

</web-app>

JSTL 标签库(次重点****)

core.jsp

<%--2、第二步,使用 taglib 指令引入标签库。--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
f) JSTL 标签库的使用步骤
  1、先导入 jstl 标签库的 jar 包。
    taglibs-standard-impl-1.2.1.jar
    taglibs-standard-spec-1.2.1.jar
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-core.jsp</title>
</head>
<body>
    <%--
        g) core 核心库使用
           i.<c:set /> (使用很少)
            作用:set 标签可以往域中保存数据

            域对象.setAttribute(key,value);
            scope 属性设置保存到哪个域
                page 表示 PageContext 域(默认值)
                request 表示 Request 域
                session 表示 Session 域
                application 表示 ServletContext 域
            var 属性设置 key 是多少
            value 属性设置值
    --%>
    保存之前: ${ requestScope.abc }<br/>
    <c:set scope="request" var="abc" value="abcValue"/>
    保存之后: ${ requestScope.abc }<br/>

    <hr/>
    <%--
        ii.<c:if />
            if 标签用来做 if 判断。
            test 属性表示判断的条件(使用 EL 表达式输出)
    --%>
    <c:if test="${ 12 == 12 }">
        <h1>12等于12</h1>
    </c:if>
    <c:if test="${ 12 != 12 }">
        <h1>12等于12</h1>
    </c:if>

    <hr/>
    <%--
        iii.<c:choose> <c:when> <c:otherwise>标签
        作用:多路判断。跟 switch ... case .... default 非常接近

        choose 标签开始选择判断
        when 标签表示每一种判断情况
            test 属性表示当前这种判断情况的值
        otherwise 标签表示剩下的情况

        <c:choose> <c:when> <c:otherwise>标签使用时需要注意的点:
            1、标签里不能使用 html 注释,要使用 jsp 注释
            2、when 标签的父标签一定要是 choose 标签
    --%>
    <%
        request.setAttribute("height", 191);
    %>
    <c:choose>
        <%-- 这是 html 注释 --%>
        <c:when test="${ requestScope.height > 190 }">
            <h2>小巨人</h2>
        </c:when>
        <c:when test="${ requestScope.height > 180 }">
            <h2>很高</h2>
        </c:when>
        <c:when test="${ requestScope.height > 170 }">
            <h2>还可以</h2>
        </c:when>
        <c:otherwise>
            <c:choose>
                <c:when test="${requestScope.height > 160}">
                    <h3>大于 160</h3>
                </c:when>
                <c:when test="${requestScope.height > 150}">
                    <h3>大于 150</h3>
                </c:when>
                <c:when test="${requestScope.height > 140}">
                    <h3>大于 140</h3>
                </c:when>
                <c:otherwise>
                    其他小于 140
                </c:otherwise>
            </c:choose>
        </c:otherwise>
    </c:choose>
</body>
</html>

Student

package com.sq.pojo;

/**
 * @author 21115
 * @ date 2021/9/28
 */
public class Student {
	//4.编号,用户名,密码,年龄,电话信息
	private Integer id;
	private String username;
	private String password;
	private Integer age;
	private String phone;

	public Student() {
	}

	public Student(Integer id, String username, String password, Integer age, String phone) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.age = age;
		this.phone = phone;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "Student{" +
				"id=" + id +
				", username='" + username + '\'' +
				", password='" + password + '\'' +
				", age=" + age +
				", phone='" + phone + '\'' +
				'}';
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 修改 web.xml 的时候,一定要重新部署 -->
    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <!--"///"表示省略了 localhost:3306 等同于:jdbc:mysql://localhost:3306/test -->
        <param-value>jdbc:mysql:///test</param-value>
    </context-param>
</web-app>

forEach.jsp

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.sq.pojo.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 21115
  Date: 2021/9/27
  Time: 23:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL_JSTL_09-forEach.jsp</title>
    <title>Title</title>
    <style>
        table{
            border: 1px lavender solid;
            width: 600px;
            border-collapse: collapse;
        }
        td,th{
            border: 1px lavender solid;
        }
    </style>
</head>
<body>
    <%--
        1.遍历 1 到 10,输出
            begin 属性设置开始的索引
            end 属性设置结束的索引
            var 属性表示循环的变量(也是当前正在遍历到的数据)
            for( int i = 1; i < 10; i++)
    --%>
    <table border="1">
        <c:forEach begin="1" end="10" var="i">
            <tr>
                <td>第${ i }行</td>
            </tr>
        </c:forEach>
    </table><br/>
    <%--
        <c:forEach begin="1" end="10" var="i">
            <h1> ${ i } <h1/>
        </c:forEach>
    --%>

    <hr>
    <%--
        2.遍历 Object 数组
            for (Object item: arr)
            items 表示遍历的数据源(遍历的集合)
            var 表示当前遍历到的数据
    --%>
    <%
        request.setAttribute("arr",new String[]{"11111111111","22222222222","33333333333"});
    %>
    <c:forEach items="${ requestScope.arr }" var="item">
        ${ item }<br/>
    </c:forEach><br/>

    <hr>
    <%-- 3. 遍历 Map 集合 --%>
    <%
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
//        for(Map.Entry<String,Object> entry : map.entrySet()){
//        }
        request.setAttribute("map",map);
    %>
    <c:forEach items="${ requestScope.map }" var="entry">
        <h1>${entry.key} = ${entry.value}</h1>
    </c:forEach>

    <%--4.遍历 List 集合---list 中存放 Student 类,有属性:编号,用户名,密码,年龄,电话信息--%>
    <%
        List<Student> studentList = new ArrayList<Student>();
        for (int i = 1; i <= 10; i++) {
            studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i));
        }
        request.setAttribute("stus", studentList);
    %>
    <table>
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
            <th>电话</th>
            <th>操作</th>
        </tr>
        <%--
        items 表示遍历的集合
        var 表示遍历到的数据
        begin 表示遍历的开始索引值
        end 表示结束的索引值
        step 属性表示遍历的步长值
        varStatus 属性表示当前遍历到的数据的状态
        for(int i = 1; i < 10; i+=2)
        --%>
        <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
            <tr>
                <td align="center">${stu.id}</td>
                <td align="center">${stu.username}</td>
                <td align="center">${stu.password}</td>
                <td align="center">${stu.age}</td>
                <td align="center">${stu.phone}</td>
                <td align="center">${status.step}</td>
                <%--
                    public interface LoopTagStatus{
                        public Object getGurrent();         表示获取当前遍历到的数据
                        public int getIndex();              表示获取便利的索引
                        public int getCount();              表示遍历的个数

                        public boolean isFirst():           表示当前遍历到数据是否是第一条,
                        public boolean isLast():            或最后一条

                        public Integer getBegin():
                        public Integer getEnd();            获取 begin,end,step属性值
                        public Integer getStep();
                    }
                --%>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值