EL表达式
EL表达式
1.EL表达式核心点是将对象存储到某个域中,在需要的页面中直接使用表达式来使用,来减少强制 类型转换的操作;
EL表达式可自动类型转换
<%
//存储
session.setAttribute("name","曾璐");
//存储对象
Emp emp = new Emp();
emp.setEname("樊瑞泽");
emp.setSex("男");
session.setAttribute("emp",emp);
%>
name:${name}
<hr>
ename:${emp.ename}
sex:${emp.sex}
EL运算
1.关系运算符
== 或 eq | 判断是否相等 |
---|---|
< 或 lt | < 或 lt |
> 或 gt | 判断是否大于 |
empty | 判断是否为空 |
!或not | – |
2.逻辑运算符
&& 或 and | 与 |
---|---|
or | 或 |
! 或or | 取反 |
.3.。多个作用域有相同的key
<%
request.setAttribute("name","a");
application.setAttribute("name","b");
session.setAttribute("name","c");
pageContext.setAttribute("name","d");
%>
<%=
${name}
%>
EL 获取四个特定域中的属性
EL JSP
pageScope pageContext
requestScope Request
sessionScope Session
applicationScope ServletContext
<body>
<%
// 从上往下, 优先级从高到低
pageContext.setAttribute("key1", "pageContext1");
pageContext.setAttribute("key2", "pageContext2");
request.setAttribute("key2", "request");
session.setAttribute("key2", "session");
application.setAttribute("key2", "application");
%>
${ key2 } // 默认选择优先级最高 值:pageContext2
${ pageScope.key2 } // 值: pageContext2
${ requestScope.key2 } // 值: request
${ sessionScope.key2 } // 值: session
${ applicationScope.key2 } // 值: application
</body>
JSTL标准标签库
jstl是一个标准标签库;可以简化jsp页面中的小脚本的使用频率,让jsp页面更整洁
步骤 :
1 .关联jia包
2 .引入jia包
引入方式:<!%--prefix:别名 uri:引入的标准库路径--%>
例:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
标签展示
<%--1~5循环--%>
<c:forEach begin="1" end="5" var="index">
${index}
</c:forEach>
<%--遍历数组
items:指定要遍历的对象
var:每次遍历对应的对象
begin:开始值
end:结束值
varStatus:下标对象
--%>
<%--遍历数组-->
<%
int[] arr = {1,2,3,4}
session.setAttribute("arr",arr);
%>
<c:forEach items="${strArr}" var="str" varStatus="indexObj">
${str},${indexObj.index}
</c:forEach>
<%
List<Emp> empList = new ArrayList<>();
empList.add(new Emp(1,"李白"));
empList.add(new Emp(2,"杜甫"));
empList.add(new Emp(3,"苏轼"));
session.setAttribute("empList",empList);
%>
<!--
for(int i=0;i<empList.size();i++){empList.get(i);}
for(Emp emp : empList){}-->
<c:forEach items="${empList}" var="emp">
姓名:${emp.ename},序号:${emp.eno}<br>
</c:forEach>
<hr>
<%
// 创建Map集合
Map map = new HashMap<>();
map.put("法师","安其拉");
map.put("射手","后羿");
map.put("战士","亚瑟");
session.setAttribute("map",map);
%>
<%--遍历Map集合--%>
<c:forEach items="${map}" var="map">
${map.key},${map.value}
</c:forEach>