EL表达式&JSTL标签库

1. EL 表达式

EL 表达式的全称是:Expression Language 表达式语言。 
EL 表达式的什么作用:EL 表达式主要是代替 jsp 页面中的表达式脚本在 jsp 页面中进行数据的输出。
因为 EL 表达式在输出数据的时候,要比 jsp 的表达式脚本要简洁很多。
<body> 
<% request.setAttribute("key","值"); %>
表达式脚本输出 key 的值是: <%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%><br/> 
EL 表达式输出 key 的值是:${key1} 
</body>

: EL 表达式在输出 null 值的时候,输出的是空串。jsp 表达式脚本输出 null 值的时候,输出的是 null 字符串。

2.EL表达式——运算

2.1 empty 运算
可以判断一个数据是否为空,如果为空,则输出 true,不为空输出 false。
以下几种情况为空:
1、值为 null 值的时候,为空
2、值为空串的时候,为空
3、值是 Object 类型数组,长度为零的时候
4、list 集合,元素个数为零
5、map 集合,元素个数为零
格式 :  ${empty list }
2.2  三元运算
格式 : ${ 12 != 12 ? "胡歌帅呆":"胡歌又骗人啦" }
 
3. EL  表达式的 11 个隐含对象 (类似于内置对象) 
变量                                 类型                                     作用
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 信息
initParam                   Map<String,String>               它可以获取在 web.xml 中配置的<context-param>上下文参数
 
4 . EL获取域中的属性 : 
<body>
<% pageContext.setAttribute("key1", "pageContext1");
    pageContext.setAttribute("key2", "pageContext2");
    request.setAttribute("key2", "request");
    session.setAttribute("key2", "session");
    application.setAttribute("key2", "application"); 
%>

${ applicationScope.key2 } </body>

5. pageContext 对象的使用

1.协议:
2.服务器 ip:
3.服务器端口:
4.获取工程路径:
5.获取请求方法:
6.获取客户端 ip 地址:
7.获取会话的 id 编号:
<body>
<%-- request.getScheme() 它可以获取请求的协议 
request.getServerName() 获取请求的服务器 ip 或域名 
request.getServerPort() 获取请求的服务器端口号 
getContextPath() 获取当前工程路径 
request.getMethod() 获取请求的方式(GET 或 POST) 
request.getRemoteHost() 获取客户端的 ip 地址 
session.getId() 获取会话的唯一标识 --%>
<% 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>

6 . JSTL 标签库 

全称是指 JSP Standard Tag Library JSP 标准标签库
EL 表达式主要是为了替换 jsp 中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个 jsp 页面变得更佳简洁。
使用方法 : 1.导包(两个)
2.使用taglib指令引入标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
7. JSTL的常用标签
<%--
<c:set /> :标签可以往域中保存数据
scope属性 : 表示保存到哪个域中 page:pageScope   request : requestScope   session :sessionScope   application : ServletContext
var : key值
vlaue : value
--%>
<c:set scope="page" var="key1" value="value1"/>
<c:set scope="request" var="key2" value="value2"/>
<c:set scope="session" var="key3" value="value3"/>
<c:set scope="application" var="key4" value="value4"/>

<%-- EL 表达式 --%>
 ${pageScope.key1}<br>
 ${requestScope.key2}<br>
 ${sessionScope.key3}<br>
 ${applicationScope.key4}<br>
<%--
<c:if /> : 判断
不能多条件判断 , 只能判断一次
test属性: 判断的条件 , 用EL表达式
--%>
<c:if test="${12 == 12}">
    <h1>条件成立 ! </h1>
</c:if>
<%--
<c:choose /> : 多条件判断 , 用来弥补c:if 的缺点
注意点 : 1. c:choose  标签中不能使用html注释 , 只能使用jsp注释
         2. c:when 的父标签必须是 c:choose
--%>
<%
request.setAttribute("height",155);
%>
<c:choose>
    <c:when test="${requestScope.height > 190}">
        <h2>非常高</h2>
    </c:when>
    <c:when test="${requestScope.height > 170}">
        <h2>正常身高</h2>
    </c:when>
    <c:when test="${requestScope.height > 160}">
        <h2>比较矮</h2>
    </c:when>
    <c:otherwise>
        <h2>有缺陷啊 ! </h2>
    </c:otherwise>
</c:choose>
forEach 标签的使用
<%-- 1.遍历1 - 10

注意 : begin 表示的是开始的索引
      end 表示的是结束的索引
--%>

<table border="1">
    <c:forEach begin="1" end="10" var="i">
        <tr>
            <td>${i}</td>
        </tr>
    </c:forEach>
</table>
<%--  2.遍历String 数组 --%>
<%
pageContext.setAttribute("arr",new String[]{"123456789","234556723","4572376535"});
%>
<c:forEach items="${pageScope.arr}" var="item">
    ${item}<br>
</c:forEach>
<%--  3. 遍历map 集合 --%>
<%
    Map<String,String> map = new HashMap<>();
    map.put("key1","value1");
    map.put("key2","value2");
    map.put("key3","value3");
    map.put("key4","value4");
    pageContext.setAttribute("map",map);
%>
<c:forEach items="${map}" var="item">
    <h2>${item.key} = ${item.value}</h2>
</c:forEach>
<%-- 4.遍历list集合  --%>
<%
    List<Student> list = new ArrayList<>();
    for (int i=0; i <10 ; i++){
        list.add(new Student(i,"username"+i,18+i,1863598+i));
    }
    pageContext.setAttribute("list",list);
%>
<table>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>电话</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${pageScope.list}" var="stu">
        <tr>
            <td>${stu.id}</td>
            <td>${stu.name}</td>
            <td>${stu.age}</td>
            <td>${stu.phone}</td>
            <td>删除 . 修改</td>
        </tr>
    </c:forEach>
</table>

原文地址:https://www.cnblogs.com/Anonymity-zhang/p/14682118.html