EL:Expression Language
即表达式语言
作用?
因为EL表达式在输出数据的时候要比jsp的表达式脚本简洁很多。EL主要用于输出域对象中的数据
${ 表达式 }
(在输出null值的时候输出是空串)
<body>
<%
request.setAttribute("key", "值");
%>
表达式脚本输出:<%= request.getAttribute("key") %> <br/>
EL表达式输出:${key}
body>
EL表达式搜索域数据的顺序:
<body>
<%
// JSP中的四个域对象,往四个域中都保存了相同key的数据,那EL输出什么呢?
pageContext.setAttribute("key", "pageContext");
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
%>
${ key }
body>
结论:会按照四个域的从小到大的顺序去进行搜索,找到就输出,和代码的先后顺序是没关系的
(域是范围从小到大顺序:pageContext -> request -> session -> application)
EL输出复杂的Bean对象:
${ personkey }
${ personkey.name }
${ personkey.map.key } ${ personkey.map.get("key") } ${ personkey.map["key"] } 也行
Ⅰ 关系元素
${ 12 == 12 } 或者 ${ 12 eq 12 } // 结果是false或者true
${ 12 != 12 } 或者 ${ 12 ne 12 }
> gt
< lt
>= ge
<= le
Ⅱ 逻辑运算
${ 10 == 10 && 1 == 1 } 或者 ${ 10 == 10 and 1 == 1 } // 结果是false或者true
|| or
! not
Ⅲ 算术运算
${10 + 12} - *
/ div
% mod
Ⅳ empty运算
可以判断一个数据是否为空,空则返回true,反之false
一下几种情况为空:
<%
pageContext.setAttribute("emptyNull", null);
%>
${ empty emptyNull }
Ⅴ 三元运算
Ⅵ .运算 []运算
.运算:可以输出Bean对象中某个属性的值
[]运算:可以输出有序集合中某个元素的值,还可以用于map,例如上面的 ${ personkey.map[“key”] }
EL中11个隐含对象:是EL中自己定义的可以直接使用
pageContext
:可以获得jsp中的九大内置对象(类型:PageContextImpl)
四个域对象对应的map:
pageScope
:可以获取pageContext域中的数据(类型:Map
requestScope
:可以获取Request域中的数据 (类型:Map
sessionScope
:可以获取Session域中的数据 (类型:Map
applicationScope
:可以获取ServletContext域中的数据 (类型:Map
param
:可以获取请求参数的值(类型:Map
paramValues
:可以获取请求参数的值,可多个值 (类型:Map
header
:可以获取请求头的信息 (类型:Map
headerValues
:可以获取请求头的信息 ,可多个值 (类型:Map
cookie
: 可以获取当前请求的Cokkie信息 (类型:Map
initParam
:可以获取在 web.xml中配置的 上下文参数 (类型:Map
pageContext 使用:
${pageContext.out}
${pageContext.session}
${pageContext.response}
....
作用:一般用来输出
协议,
<%= request.getScheme() %>
${pageContext.request.scheme}
服务器ip 端口,
<%= request.getServerName() %>
<%= request.getServerPort() %>
${pageContext.request.serverName}
${pageContext.request.serverPort}
工程路径,
<%= request.getContextPath() %>
${pageContext.request.contextPath}
请求方式,
<%= request.getMethod() %>
${pageContext.request.method}
客户端ip,
<%= request.getRemoteHost() %>
${pageContext.request.remoteHost}
会话的id
<%= session.getId() %>
${pageContext.session.id}
,等信息
<%
pageContext.setAttribute("req", request);
%>
${req.scheme}
四个域对象对应map:
<%
pageContext.setAttribute("key", "pageContext");
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
%>
${ key }
${ pageScope.key }
下面六个的使用:
输出请求参数username的值:${param.username}
输出请求参数hobby的第一个值:${paramValues.hobby[0]}
输出请求参数hobby的第二个值:${paramValues.hobby[1]}
获取请求头的信息:${header["User-Agent"]}
headerValues 多个值的请求头比较少
获取Cookie的名称:${cookie.JSESSIONID.name}
获取Cookie的值:${cookie.JSESSIONID.value}
获取上下文参数值:${initParam.username}
<context-param>
<param-name>usernameparam-name>
<param-value>contextparam-value>
context-param>
JSTL : JSP Standard Tag Library
JSP的标准标签库,是不断完善的开源标签库
EL表达式主要是为了替代JSP中的表达式脚本,而JSTL则是替代JSP中的代码脚本
JSTL里面能直接写EL表达式和HTML标签
JSTL 由五个不同功能的标签库组成:
功能范围 | URL | 前缀 |
---|---|---|
核心标签库(重点) | http://java.sun.com/jsp/jstl/core | c |
格式化 | http://java.sun.com/jsp/jstl/fmt | fmt |
函数 | http://java.sun.com/jsp/jstl/function | fn |
数据库(不使用) | http://java.sun.com/jsp/jstl/sql | sql |
XML(不使用) | http://java.sun.com/jsp/jstl/xml | x |
Ⅰ 先导入jstl标签库的 jar 包
Ⅱ 首先需要在jsp页面中用 taglib 指定 引入标签库 (idea能帮我们)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
作用:可以往域中保存数据
<c:set scope="request" var="abc" value="abcValue"> c:set> !!!!
== <% request.setAttribute("abc","abcValue"); %>
<c:if test="${12 == 12}">
<div>12等于12div>
c:if>
作用:用来做多路判断,类似switch…case…default
<% request.setAttribute("height", 178); %>
<c:choose>
<c:when test="${requestScope.height > 190}">
<h1>巨人h1>
c:when>
<c:otherwise>
<h2>剩下的h2>
c:otherwise>
c:choose>
作用:遍历输出
<c:forEach begin="1" end="10" var="i">
${ i } <br/>
c:forEach>
<table>
<c:forEach begin="1" end="10" var="i">
<tr>
<td>第 ${i} 行td>
tr>
c:forEach>
table>
<% request.setAttribute("arr", new String[]{"123", "456", "789"}); %>
<c:forEach items="${requestScope.arr}" var="s">
${s}
c:forEach>
<c:forEach items="${requestScope.map}" var="entry">
${entry} <br/>
${entry.key} ==> ${entry.value}
c:forEach>