EL表达式的全称:Expression Language(表达式语言)
EL表达式的作用:
代替jsp页面中的表达式脚本(由于比jsp的表达式脚本简洁
)在jsp页面中进行数据的输出.
EL表达式的格式:
${表达式}
EL
表达式在输出null值
的时候,输出的是空串
jsp
表达式脚本输出null值
的时候,输出的是null字符串
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%request.setAttribute("key","值1");
%>
表达式脚本输出key的值是:<%=request.getAttribute("key")%>
EL表达式输出key的值是:${key}
EL表达式主要在jsp页面中输出数据.
主要输出域对象
中的数据.
注意:
当四个域中都有相同的key的数据
的时候,EL表达式会按照四个域的从小到大的顺序
去进行搜索,找到就输出.
<%//pageContext.setAttribute("key","pageContext");//request.setAttribute("key"," request");//session.setAttribute("key","session");//application.setAttribute("key","application");
%>
${key}
示例-输出Person类中的普通属性,数组属性,list集合属性和map集合属性.
public class Person {private String name;private String[] phones;private List cities;private Map map;public Person() {}public Person(String name, String[] phones, List cities, Map 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 String[] getPhones() {return phones;}public void setPhones(String[] phones) {this.phones = phones;}public List getCities() {return cities;}public void setCities(List cities) {this.cities = cities;}public Map getMap() {return map;}public void setMap(Map map) {this.map = map;}@Overridepublic String toString() {return "Person{" +"name=" + name +", phones=" + Arrays.toString(phones) +", cities=" + cities +", map=" + map +'}';}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%Person person = new Person();person.setName("结果");person.setPhones(new String[]{"1866666","12333333","154688888"});List cities = new ArrayList();cities.add("北京");cities.add("上海");cities.add("深圳");person.setCities(cities);Map map = new HashMap<>();map.put("key1","value1");map.put("key2","value2");map.put("key3","value3");person.setMap(map);pageContext.setAttribute("p",person);%>输出Person:${p}
输出Person的name属性值:${p.name}
输出Person的phones数组属性值:${p.phones[2]}
输出Person的cities集合中的元素值:${p.cities}
输出Person的cities集合中的个别元素值:${p.cities[2]}
输出Person的Map集合:${p.map}
输出Person的Map集合中某个key的值:${p.map.key3}
输出Person的age属性:${p.age}
注意:
这里的点运算,实际上是调用类中的get方法.
语法:${运算表达式}
,EL表达式支持以下运算符
${12 == 12}或者${12 eq 12}
${12 != 12}或者${12 ne 12}
${12 < 12}或者${12 lt 12}
${12 > 12}或者${12 gt 12}
${12 >= 12}或者${12 ge 12}
${12 <= 12}或者${12 le 12}
${12 == 12 && 12 > 11}或者${12 == 12 and 12 > 11}
${12 == 12 || 12 > 11}或者${12 == 12 or 12 > 11}
${! true}或者${not true}
(3).算数运算
${12 + 12}
${12 - 12}
${12 * 12}
${12 / 12} 或 ${12 div 12}
${12 % 12} 或 ${12 mod 12}
(4).empty运算
可以判断一个数据是否为空,如果为空
,则输出true
,不为空
输出false
.
为空的几种情况:
<%request.setAttribute("emptyNull",null);request.setAttribute("emptyStr","");request.setAttribute("emptyArr",new Object[]{});List list = new ArrayList<>();request.setAttribute("emptyList",list);Map map = new HashMap<>();map.put("key1","value");request.setAttribute("emptyMap",map);%>${empty emptyNull}
${empty emptyStr}
${empty emptyArr}
${empty emptyList}
${empty emptyMap}
(5).三元运算
表达式1 ? 表达式2 : 表达式3
如果1为真,则执行2,否则执行3
(6)."."
点运算和[]
中括号运算符
Bean对象
中某个属性的值.有序集合
中某个元素的值,还可以输出map集合
中key里含有特殊字符
的key的值.
<%Map map = new HashMap<>();map.put("a.a.a","aaaValue");map.put("bbb","bbbValue");map.put("ccc","cccValue");request.setAttribute("map",map);
%>${map['a.a.a']}
${map.bbb}
这些隐含对象,是EL表达式中自己定义的,可以直接使用.
变量:
pageScope 类型:
Map作用:
可以获取pageContext
域中的数据.变量:
requestScope 类型:
Map作用:
可以获取Request
域中的数据.变量:
sessionScope 类型:
Map作用:
可以获取Session
域中的数据.变量:
applicationScope 类型:
Map作用:
可以获取ServletContext
域中的数据.<%pageContext.setAttribute("key1","pageContext1");request.setAttribute("key1","request");session.setAttribute("key1","session");application.setAttribute("key1","application");%>${pageScope.key1}
${requestScope.key1}
变量:
pageContext 类型:
PageContextlmpl 作用:
可以获取jsp中的九大内置对象.
<%--request.getScheme() 可以获取请求的协议request.getServerName() 可以获取请求的服务器ip或域名request.getServerPort() 可以获取请求的服务器端口号getContextPath() 可以获取当前工程路径request.getMethod() 可以获取请求的方式(GET/POST)request.getRemoteHost() 可以获取客户端的ip地址request.getId() 可以获取会话的唯一标识--%><%//pageContext.setAttribute("req",request);%>1.协议:${pageContext.request.scheme}
2.服务器ip:${pageContext.request.serverName}
3.服务器端口号:${pageContext.request.serverPort}
4.获取当前工程路径:${pageContext.request.contextPath}
5.获取请求的方式:${pageContext.request.method}
6.获取客户端的ip地址:${pageContext.request.remoteHost}
7.获取会话的唯一标识:${pageContext.session.id}
变量:
param 类型:
Map作用:
可以获取请求参数的值变量:
paramValues 类型:
Map作用:
可以获取多个请求参数的值变量:
header 类型:
Map作用:
可以获取请求头的信息变量:
headerValues 类型:
Map作用:
可以获取请求头的信息,多个值的情况变量:
cookie类型:
Map作用:
可以获取当前请求的Cookie信息变量:
initParam 类型:
Map作用:
可以获取在web.xml中配置的 context-param上下文参数输出请求参数username的值:${param.username}
输出请求参数password的值:${param.password}
输出请求参数username的值:${paramValues.username[0]}
输出请求参数hobby的值:${paramValues.hobby[0]}
输出请求参数hobby的值:${paramValues.hobby[1]}
输出请求头【User-Agent】的值:${header['User-Agent']}
输出请求头【Connection】的值:${header['Connection']}
输出请求头【Connection】的值:${header.Connection}
输出请求头【User-Agent】的值:${headerValues['User-Agent'][0]}
获取Cookie的名称:${cookie.JSESSIONID.name}
获取Cookie的值:${cookie.JSESSIONID.value}
${initParam}
${initParam.username}
输出<context-param>username的值: ${initParam.username}
JSTL标签库
全称是指 JSP Standard Tag Library JSP标准标签库.
作用:
代替jsp页面的代码脚本,使得整个jsp页面变得更加简洁.
(1).先导入jstl标签库的jar包
(2).使用taglib指令引入标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--Created by IntelliJ IDEA.User: asusDate: 2022/11/15Time: 10:39To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
(1).
作用:
set标签可以往域中保存数据.
<%--域对象.setAttribute(key,value);scope 属性设置保存到哪个域page 表示PageContext域request 表示Request域session 表示Session域application 表示ServletContext域var 设置key是多少value 设置值
--%>
保存之前: ${requestScope.abc}
保存之后: ${requestScope.abc}
(2).
作用:
用来做if
判断.
<%--test属性表示判断条件(用EL表达式输出)
--%>
12 == 12
(3).
作用:
多路判断,跟switch,case,default非常接近.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--Created by IntelliJ IDEA.User: asusDate: 2022/11/15Time: 10:39To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--
choose标签开始选择判断
when标签表示每一种判断情况
test属性表示当前这种判断情况的值
otherwise标签表示剩下的情况
--%>
<%request.setAttribute("height",178);
%>
190}">巨人
180}">很高
170}">还可以
160}" >大于160
注意:
otherwise和when都必须
在choose标签内,when也可以在otherwise内
,但必须
有choose的包裹.
(4).
作用:
遍历输出使用.
<%--
begin 属性设置开始的索引
end 属性设置结束的索引
var 属性表示循环的变量(也是当前正在遍历到的数据)
items 表示遍历的数据源(遍历的集合)
step 属性表示遍历的步长值
varStatus 属性表示当前遍历到的数据的状态
--%>
<%-- 1.遍历输出1—10 --%>
第${i}行
<%-- 2.遍历Object数组 --%>
<%request.setAttribute("arr",new String[]{"123456","456789","123789"});
%>
${item}
<%-- 3.遍历Map集合 --%>
<%Map map = new HashMap();map.put("key1","value1");map.put("key2","value2");map.put("key3","value3");request.setAttribute("map",map);
%>
${entry}
${entry.key}
${entry.value}
<%-- 4.遍历List集合 --%>
<%List studentArrayList = new ArrayList();for(int i = 1;i <= 10;i++){studentArrayList.add(new Student(i,"username" + i,"pass" + i,18 + i,"phone" + i));}request.setAttribute("stu",studentArrayList);
%>
编号 用户名 密码 年龄 电话 操做1 操做2 ${item.id} ${item.username} ${item.password} ${item.age} ${item.phone} ${status.step} ${status.first}