El表达式 :
概念 :
Expression Language 表达式语言
作用 :
替换和简化jsp页面中java代码的编写
语法 :
${表达式}
- 注意 : JSP是默认支持EL表达式的
- ① 在page中 , 添加属性 isELIgnored=“true” 忽略页面中所有的EL表达式
- ② 直接在表达式前边加一个 " \ "
使用 :
运算 :
- 运算符 :
-
算数运算符 : + , - , * , /(div) , %(mod)
-
比较运算符 : > , < , >= , <= , == , !=
-
逻辑运算符 : && (and) , || (or) , ! (not)
-
空运算符 : empty
- empty 用于判断字符串 , 集合 , 数组对象是否为null ,
- 或者长度是否为0
-
只要有一个满足就返回true
-
使用 :
-
${empty 字符串|集合|数组}
-
获取值 :
(1)el表达式只能从域对象中获取值 ,
(2)语法 :
①**${域名称.键名}** : 从指定域中获取指定的键名
-
域名称 :
- pageScope --> pageContext
- requestScope --> request
- sessionScope --> session
- applicationScope --> application (ServletContext对象)
-
<% //在域中存储数据 request.setAttribute("name" , "张三"); session.setAttribute("age" , "23"); %> <h3>获取值</h3> ${requestScope.name} <%-- 浏览器输出张三 --%> <hr> ${sessionScope.age} <%-- 浏览器输出23 --%> <hr> ${sessionScope.haha} <%-- 为空浏览器不输出 --%>
②**${键名} 😗*
表示依次从最小的域中查找是否有该键对应的值 , 直到找到为止
③Map集合 :
-
获取 : Map集合的值
- ${域名称.键名.key名称}
- ${requestScope.map.name}
- ${域名.map[ " key名称 " ] }
- ${map[“name”]}**
- ${域名称.键名.key名称}
-
本质上会去调用 , 对象的getter方法
-
${requestScope.u.birStr} 获取Map集合中的birStr这个元素的内容
-
<body> <% User user = new User(); user.setName("思尘"); user.setAge(21); user.setBirthday(new Date()); request.setAttribute("u" , user); %> <h3>获取域中值</h3> ${requestScope.u} <%--输出为cn.sichen.domain.User@4d2b3f0b--%> <%-- 通过的是对象的属性来获取 setter或getter方法 , 去掉set或get , 再将剩余部分 , 首字母变成小写 setName --> Name --> name --%> <br> ${requestScope.u.name} ${requestScope.u.age} ${requestScope.u.birthday} <%--做日期格式化--%> ${requestScope.u.birStr} </body>
User.java中的格式化日期操作
public String getBirStr(){
//1.格式化日期对象 ,
if (birthday != null){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//2.返回字符串即可
return simpleDateFormat.format(birthday);
}else{
return "";
}
}
-
值如果是一个List集合的话 :
${ 域名城.键名 [索引] }
-
${requestScope.list[1]} -
如果发生脚标越界现象 : 是不会报错的 , 只会返回一个空的字符串
-
-
如果List集合中存储了 , 一个user , 那么在获取user中内容的时候
-
${requestScope.list[1].name} //这里选择user对应的脚标 , 代表的就是user , 直接使用user中的属性即可
-
隐式对象 :
el表达式中有11个隐式对象
pageContext 能够获取其他八个对象
${pageContext.对象名}
- ${pageContext.request.contextPath} : 动态获取虚拟目录
JSTL
maven使用JSTL的完整依赖 (这是较老版本的 , 包还是javax )
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.web/jstl-impl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
使用 最新版的jakarta包下的 , 直接导入即可
还需要导入两个standard的依赖包 , 解决 下边的爆红问题
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- jstl-api -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>2.0.0</version>
</dependency>
<!--standard-impl-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
<scope>runtime</scope>
</dependency>
<!--standard-spec-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
概念 :
JavaServer Pages Tag Library : JSP标准标签库
- 是由Apache组织提供的开源的免费的JSP标签
作用 :
用于简化和替换jsp页面上的代码
步骤:
-
(1) 导入jstl相关的jar包
-

-
(2) 引入标签库
-
taglib 指令 : <%@taglib prefix=“c” uri="…路径" %>
-
c : 是前缀 , uri 填的是路径
-
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%--这个是高版本的使用这个 , 一般约定俗成的前缀为 c --%> -
(3) 使用标签库
常用的JSTL标签 :
(1) if (相当于java代码中的 if 语句)
<%
c:if标签
1.属性
* test 必须属性 , 接收boolean表达式
如果表达式为true , 则显示if标签体的内容 , 如果为false , 则不 显示标签体内容
* 一般情况下 : test属性值会结合EL表达式来使用
2.注意
* c:if 没有else语句 , 想要表达另一种情况 , 再写一个c:if
%>
<body>
<c:if test="true">
hello jstl~~~
</c:if>
<hr>
<%
//判断request域中的一个list是否为空 , 如果不为null则显示遍历集合
List list = new ArrayList();
list.add("aaa");
request.setAttribute("list" , list);
request.setAttribute("number" , 6);
%>
<c:if test="${not empty requestScope.list}">
遍历集合
</c:if>
<hr>
<c:if test="${requestScope.number %2 != 0}">
${requestScope.number}为奇数!
</c:if>
<c:if test="${requestScope.number %2 == 0}">
${requestScope.number}是偶数!
</c:if>
</body>
(2) choose (相当于java代码中的 switch 语句)
<body>
<%--
完成数字编号对应星期几案例
1.域中存储一数字
2.使用choose取出数字 相当于switch声明
3.使用when标签对数字判断 相当于case声明
4.otherwise 标签做其他情况的声明 , 相当于default声明
--%>
<%
request.setAttribute("number" , 70);
%>
<c:choose>
<c:when test="${requestScope.number == 1}">今天是星期一 !</c:when>
<c:when test="${requestScope.number == 2}">今天是星期二 !</c:when>
<c:when test="${requestScope.number == 3}">今天是星期三 !</c:when>
<c:when test="${requestScope.number == 4}">今天是星期四 !</c:when>
<c:when test="${requestScope.number == 5}">今天是星期五 !</c:when>
<c:when test="${requestScope.number == 6}">今天是星期六 !</c:when>
<c:when test="${requestScope.number == 7}">今天是星期七 !</c:when>
<c:otherwise>输入有误!</c:otherwise>
</c:choose>
</body>
(3) foreach (相当于java代码中的 for 循环)
<body>
<%--
foreach : 相当于java代码的for循环
1.完成重复的操作
for (int i = 0 ; i < 10 ; i ++){}
* 完成重复操作有一组属性 ,
begin : 开始值
end : 结束值
var : 临时变量
step : 步长 相当于for循环中的++操作 , 这里规定一次循环加多少值
varStatus : 指定一个值,使用这个值调index 和 count两个属性
- index : 容器中元素的索引从0开始 (在不同循环中使用 , 展示的是实际变量的值)
- count : 循环次数
2.遍历容器
List<User> list;
for(User user : list){
}
* 完成遍历容器有一组属性 :
items : 容器对象 相当于for(User user : list)中的list
var : 容器中的元素的临时变量 相当于for(User user : list)中的user
--%>
<c:forEach begin="1" end="10" var="i" step="2" varStatus="s">
${i} index: ${s.index} count : ${s.count}<br>
</c:forEach>
<%
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list" , list);
%>
<c:forEach items="${requestScope.list}" var="str" varStatus="c">
${str} index : ${c.index} count : ${c.count} <br>
<%--输出为 :
aaa index : 0 count : 1
bbb index : 1 count : 2
ccc index : 2 count : 3
--%>
</c:forEach>
</body>
案例 : 展示list集合中的数据到一个表格
需求 :
-
在request域中有一个存有user对象的list集合 , 需要使用jstl加el将集合数据展示到jsp页面的表格table中
<body> <% List list = new ArrayList(); list.add(new User("思尘" , 21 , new Date())); list.add(new User("尘" , 21 , new Date())); list.add(new User("爱思尘" , 21 , new Date())); request.setAttribute("list" , list); %> <table border="1" width="500" align="center"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>生日</th> </tr> <c:forEach items="${requestScope.list}" var="user" varStatus="s"> <c:if test="${s.count %2 == 0}"> <tr bgcolor="red"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birStr}</td> </tr> </c:if> <c:if test="${s.count %2 != 0}"> <tr bgcolor="green"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birStr}</td> </tr> </c:if> </c:forEach> </table> </body>
```</tr> </c:if> <c:if test="${s.count %2 != 0}"> <tr bgcolor="green"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birStr}</td> </tr> </c:if> </c:forEach> </table>
本文详细介绍了EL表达式在JSP中的应用,包括其语法、运算符、获取值的方法,以及如何利用JSTL标签库进行条件判断和循环操作。涵盖了隐式对象、Map操作和使用JSTL的常见标签如if、choose和forEach。
467

被折叠的 条评论
为什么被折叠?



