1. EL表达式
Expersion Language 表达式 通过在JSP页面当中以 $ 美元符号开头 一对尖括号{} 表示一个EL表达式
在JSP里的充当辅助编写一些代码 减少JSP表达式 能很好的整洁JSP页面
1.1 语法
基本使用:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:useBean id="person" class="bean.Person">
<jsp:setProperty name="person" property="id" value="1"></jsp:setProperty>
<jsp:setProperty name="person" property="username" value="老王吧"></jsp:setProperty>
</jsp:useBean>
${person.id} --------- ${person.username}
</body>
</html>
效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mUPA08JM-1647956835589)(C:\Users\10501\AppData\Roaming\Typora\typora-user-images\image-20220322195524768.png)]
1.2 作用域对象取值
通过Servlet设置request、page、session、application作用域的值 ,在EL表达式中获取
分别对应着requestScope、pageScope、sessionScope、applicationScope
通过EL隐含对象获取他们的值
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
pageContext.setAttribute("username","老王吧");
session.setAttribute("assistant","助手");
application.setAttribute("here","我一直在这里");
%>
欢迎用户${pageScope.get("username")}<br>
我是你的助手${sessionScope.get("assistant")}<br>
${applicationScope.get("here")}
</body>
</html>
1.2.1 获取应用上下文
通过JSP内置对象里的pageContext对象一共有四种方式获取当前的ContextPath应用上下文
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<p>通过\${pageContext.servletContext.contextPath}获取:${pageContext.servletContext.contextPath}</p>
<p>通过\${pageContext.request.contextPath}获取:${pageContext.request.contextPath}</p>
<p>通过\${pageContext.session.servletContext.contextPath}获取:${pageContext.session.servletContext.contextPath}</p>
<p>通过\${pageContext.servletConfig.servletContext.contextPath}获取:${pageContext.servletConfig.servletContext.contextPath}</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a4RUAcr9-1647956835590)(C:\Users\10501\AppData\Roaming\Typora\typora-user-images\image-20220322202732714.png)]
1.3 EL运算符
1.3.1 存取运算符
EL标识能通过 J a v a B e a n . p r o p e r t y N a m e 或 者 {JavaBean.propertyName} 或者 JavaBean.propertyName或者{JavaBean[“propertyName”]}访问值
示例:
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
String[] books = {"java","c++","c#"};
String[] peoples = {"Tomcat","Jackson","Malone"};
session.setAttribute("books",books);
session.setAttribute("peoples",peoples);
HashMap<String , Object> hashMap = new HashMap<>();
hashMap.put("books",books);
hashMap.put("peoples",peoples);
List list = new ArrayList();
list.add(books);
list.add(peoples);
session.setAttribute("hashMap",hashMap);
session.setAttribute("list",list);
%>
<p>从数组中获取数据${sessionScope.get("books")[0]}---${sessionScope.get("peoples")[1]}</p>
<p>从集合中获取数据${sessionScope.get("list")[0][2]}---${sessionScope.get("hashMap")["peoples"][2]}</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6OZtinCp-1647956835591)(C:\Users\10501\AppData\Roaming\Typora\typora-user-images\image-20220322212622344.png)]
1.3.2 Empty运算符
语法 **${empty object.property} ** 返回当前变量或对象的内容是空指针或空字符串、空数组、空的list容器 或null 是则return true 否则 return false
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<% session.setAttribute("abc","abc");%>
empty Session作用域里的abc是否为空 : ${empty sessionScope.get("abc")}
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v0xomnvA-1647956835591)(C:\Users\10501\AppData\Roaming\Typora\typora-user-images\image-20220322214638566.png)]
1.4 禁用EL表达式
通过在web.xml中设置jsp-config 或在 JSP页面EL表达式前面加上 反斜杠\ 表示禁用EL表达式
示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:useBean id="person" class="bean.Person">
<jsp:setProperty name="person" property="id" value="1"></jsp:setProperty>
<jsp:setProperty name="person" property="username" value="老王吧"></jsp:setProperty>
</jsp:useBean>
${person.id} --------- \${person.username}
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
</web-app>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yYtrT5vu-1647956835592)(C:\Users\10501\AppData\Roaming\Typora\typora-user-images\image-20220322200052461.png)]