EL表达式和JSTL标签库

1.EL表达式

什么是EL表达式?

EL表达式的全称是:Expression Language  。是表达式语言

EL表达式的作用:主要是代替警示牌页面中的表达式脚本在jsp页面中进行数据的输出

因为EL表达式在输出数据的时候,要比jsp的表达式脚本要简洁很多。

<body>


    <% 
        request.setAttribute("key","value");
    %>

    jsp表达式脚本的值是:
 <%=
        request.getAttribute("key")==null ? "": request.getAttribute("key")
  %>
<br/>

    EL表达式输出key的值是:${key}

</body>

注意点:

  • EL表达式的格式是:${表达式}
  • EL表达式在输出null值得时候,输出的是空串,jsp表达式输出null值得时候,输出的是null字符串。

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

<%--
  Created by IntelliJ IDEA.
  User: 龙思媛
  Date: 2021/2/28
  Time: 20:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    //往四个域中都保存相同的key值
    request.setAttribute("key","request");
    session.setAttribute("key","session");
    application.setAttribute("key","application");
   pageContext.setAttribute("key","pageContext");
%>
${key}
</body>
</html>

顺序是:

pageContext:表示当前页面范围内有效

request:一次请求内有效

session:一个会话范围内有效(打开浏览器访问服务器,知道关闭当前浏览器)

application:整个web工程范围内有效(只要web工程不停,数据都会在)

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

Person中又如下的属性:

<%@ page import="com.atguigu.pojo.Person" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 9:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Person person = new Person();
        person.setName("lsy");
        person.setPhones(new String[]{"18610541354","18688886666","18699998888"});

        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("p", person);
    %>

    输出Person:${ p }<br/>
    输出Person的name属性:${p.name} <br>
    输出Person的pnones数组属性值:${p.phones[2]} <br>
    输出Person的cities集合中的元素值:${p.cities} <br>
    输出Person的List集合中个别元素值:${p.cities[2]} <br>
    输出Person的Map集合: ${p.map} <br>
    输出Person的Map集合中某个key的值: ${p.map.key3} <br>
    输出Person的age属性:${p.age} <br>


</body>
</html>

4.EL表达式

运算表达式:

语法:${运算表达式},EL表达式支持如下运算符:

关系运算符:

==或eq等于${5==5}或者${5eq5}true
!=或ne不等于${5!=5}或者${5 ne 5}false
<或lt小于${ 5  < 5}或者${5 lt 5}false
>或gt大于${5>5}或者${5 gt 5}false
<=或le小于等于${5<=12}或者${5 le 12}true
>=或ge大于等于${3>=5}或者${5 ge 5}false

 

 

 

 

 

 

lu

逻辑运算:

算数运算:

 

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 9:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</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>
    ${ 18 % 12 } 或 ${ 18 mod 12 } <br>
</body>
</html>

 

empty运算:可以判断一个数据是否为空,如果为空,则输出true,不为空输出false。

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 9:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
//        1、值为null值的时候,为空
        request.setAttribute("emptyNull", null);
//        2、值为空串的时候,为空
        request.setAttribute("emptyStr", "");
//        3、值是Object类型数组,长度为零的时候
        request.setAttribute("emptyArr", new Object[]{});
//        4、list集合,元素个数为零
        List<String> list = new ArrayList<>();
//        list.add("abc");
        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/>
    ${ empty emptyStr } <br/>
    ${ empty emptyArr } <br/>
    ${ empty emptyList } <br/>
    ${ empty emptyMap } <br/>

  

</body>
</html>

“.”运算符合[]中括号运算符

注意:中括号中的值用单引号或者是多引号都可以

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 10:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("a.a.a", "aValue");
        map.put("b+b+b", "bValue");
        map.put("c-c-c", "cValue");

        request.setAttribute("map", map);
    %>

        ${ map['a.a.a'] } <br>
        ${ map["b+b+b"] } <br>
        ${ map['c-c-c'] } <br>

</body>
</html>

5.EL表达式的11个隐含对象

EL表达式中11个隐含对象,是EL表达式中自己定义的,可以直接使用。

pageContext对象的使用:

  1.     request.getScheme() 它可以获取请求的协议
  2.     request.getServerName() 获取请求的服务器ip或域名
  3.     request.getServerPort() 获取请求的服务器端口号
  4.     getContextPath() 获取当前工程路径
  5.     request.getMethod() 获取请求的方式(GET或POST)
  6.     request.getRemoteHost()  获取客户端的ip 地址
  7.     session.getId() 获取会话的唯一标识
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 11:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--
    request.getScheme() 它可以获取请求的协议
    request.getServerName() 获取请求的服务器ip或域名
    request.getServerPort() 获取请求的服务器端口号
    getContextPath() 获取当前工程路径
    request.getMethod() 获取请求的方式(GET或POST)
    request.getRemoteHost()  获取客户端的ip 地址
    session.getId() 获取会话的唯一标识
    --%>

<!--设置pageContext.setAttribute("req", request)是为了后续的方便使用-->
    <%
        pageContext.setAttribute("req", request);
    %>
    <%=request.getScheme() %> <br>
    1.协议: ${ req.scheme }<br>
    2.服务器ip:${ pageContext.request.serverName }<br>
    3.服务器端口:${ pageContext.request.serverPort }<br>
    4.获取工程路径:${ pageContext.request.contextPath }<br>
    5.获取请求方法:${ pageContext.request.method }<br>
    6.获取客户端ip地址:${ pageContext.request.remoteHost }<br>
    7.获取会话的id编号:${ pageContext.session.id }<br>

</body>
</html>

EL获取四个特定域中的属性

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/4
  Time: 11:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        pageContext.setAttribute("key1", "pageContext1");
        pageContext.setAttribute("key2", "pageContext2");
        request.setAttribute("key2", "request");
        session.setAttribute("key2", "session");
        application.setAttribute("key2", "application");
    %>
    ${ applicationScope.key2 }
</body>
</html>

EL表达式其他隐含对象的使用

当我们的请求地址为:

http://localhost:8080/javaWeb/other.jsp?username=lsy&password=123456&hobby=java&hobby=cpp

 


    输出请求参数username的值:${ param.username } <br>
    输出请求参数password的值:${ param.password } <br>

    输出请求参数username的值:${ paramValues.username[0] } <br>
    输出请求参数hobby的值:${ paramValues.hobby[0] } <br>
    输出请求参数hobby的值:${ paramValues.hobby[1] } <br>



    输出请求头【User-Agent】的值:${ header['User-Agent'] } <br>
    输出请求头【Connection】的值:${ header.Connection } <br>
    输出请求头【User-Agent】的值:${ headerValues['User-Agent'][0] } <br>


   获取Cookie的名称:${ cookie.JSESSIONID.name } <br>
    获取Cookie的值:${ cookie.JSESSIONID.value } <br>

    输出&lt;Context-param&gt;username的值:${ initParam.username } <br>
    输出&lt;Context-param&gt;url的值:${ initParam.url } <br>

6.JSTL标签库(****)

  • JSTL标签库:jsp Standard Tag Library  表示的是jsp标准标签库
  • EL表达式主要是替换jsp中的表达式脚本,而标签库则是为了替换代码脚本,这样会使整个jsp页面变得更加简洁。

JSTL标签库的使用步骤

1.先导入JSTL标签库的jar包。

2.使用taglib指令引入标签库。

core核心标签库的使用。

1.<c:set/>(使用很少)
        作用:set标签可以往域中保存数据

        域对象.setAttribute(key,value);

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

2.<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>
  1. <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", 180);
    %>

    <c:choose>
      

        <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>
  1. <c:forEach/>:遍历输出的时候使用。

1.遍历1到10,输出

  •         begin属性设置开始的索引
  •         end 属性设置结束的索引
  •         var 属性表示循环的变量(也是当前正在遍历到的数据)
<table border="1">
        <c:forEach begin="1" end="10" var="i">
            <tr>
                <td>第${i}行</td>
            </tr>
        </c:forEach>
    </table>

2.遍历Object数组

  •         for (Object item: arr)
  •         items 表示遍历的数据源(遍历的集合)
  •         var 表示当前遍历到的数据
 <%
        request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"});
    %>
    <c:forEach items="${ requestScope.arr }" var="item">
        ${ item } <br>
    </c:forEach> --%>
    <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>
    <hr>

4.遍历List集合

 

  •             items 表示遍历的集合
  •             var 表示遍历到的数据
  •             begin表示遍历的开始索引值
  •             end 表示结束的索引值
  •             step 属性表示遍历的步长值
  •             varStatus 属性表示当前遍历到的数据的状态
  •             for(int i = 1; i < 10; i+=2)
    <%
        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);
    %>

    <form action="" enctype=""></form>
    <table>

        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
            <th>电话</th>
            <th>操作</th>
        </tr>

     
    <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
        <tr>
            <td>${stu.id}</td>
            <td>${stu.username}</td>
            <td>${stu.password}</td>
            <td>${stu.age}</td>
            <td>${stu.phone}</td>
            <td>${status.step}</td>
        </tr>
    </c:forEach>
    </table>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值