当前位置:网站首页>EL expression
EL expression
2022-08-04 06:44:00 【Louzen】
EL表达式
转自 EL表达式详解 - Shallow but words and letters
一、介绍
- Expression Language表达式语言
- 是一种在JSP页面获取数据的简单方式(只能获取数据,不能设置数据)
- 在JSP2.0开始引入概念
- 在JSP页面的任何静态部分均可通过:${expression}来获取到指定表达式的值
二、EL获取数据(Only from the big four domain to obtain properties)
四大域:(Order from top to bottom is growing up)
- pageContext
- request
- session
- application
如果没有使用EL的内置对象,则查找数据顺序是依次按照由小到大范围从四大域中查找指定名称的属性值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${name }
</body>
</html>
The results of the page above is linjie,即从pageContext中取数据
三、EL中的内置对象
EL有11个内置对象,There are primary domain attributes related to4个和其他4个
EL的11个内置对象,除了pageContext以外,其他10A built-in object types arejava.util.Map类型
1、${pageContext} 获取到 pageContext 对象,It is not in four domains to find,But in his first defined object looking for,If found out.
2、${pageScope} 得到的是 page 域 (pageContext) 中保存数据的 Map集合.That is specified in the page 域中查找.
3、${requestScope}、${sessionScope}、${applicationScope} 和上面的 pageScope 一样,Is in the specific domain to retrieve data.
4、${param} 获取存在 request Request parameters Map,Commonly used in the data on the echo.
5、${paramValues} 获取存在 request The request parameter name of the same value String[] 数组.
8、${header} 获取 HTTP 请求头的 Map 对象.
9、${headValues} 获取 HTTP The request header value Map 对象
10、${cookie} 获取所有 cookie 的 Map 对象
11、${initParam} Access to save all Web 应用初始化参数的 Map 对象
转自:[Mr_Demen
- JSP的ELExpression of the default data fields and approaches to](https://blog.csdn.net/myspacedemen/article/details/78353635)
1、Domain properties related to(4个)
pageScope:从page范围域属性空间中查找指定的key
requestScope:从request范围域属性空间中查找指定的key(里面存放attribute键值,不存放param)
sessionScope:从session范围域属性空间中查找指定的key
applicationScope:从application范围域属性空间中查找指定的key
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }<br>
name=${pageScope.name }<br>
name=${sessionScope.name }<br>
name=${requestScope.name }<br>
</body>
</html>
2、Other important built-in objects(4个)
2.1 pageContext
该pageContext与JSP内置对象pageContext是同一个对象.通过该对象,可以获取到request、response、session、servletContext、servletConfig等对象注意:这些对象在EL里不是内置对象,这些对象只能通过pageContext获取
- 在EL中直接 ${pageContext.request} 即可获取request对象,其底层调用的是pageContext.getRequest()方法.同理,Can also use similar methods for other objects
重点:其中最常用的:${pageContext.request.contextPath},代表web应用下的根,As you can see belowactionThe path to the readability is stronger
// Regster.java
package linjie.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regster extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
<form action="${pageContext.request.contextPath }/regster" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
<input type="submit" value="点击">
</form>
</body>
</html>
2.2 param(获取请求中的指定参数)
- This is the underlying call request.getParameter()
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
<form action="${pageContext.request.contextPath }/show.jsp" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
<input type="submit" value="点击">
</form>
</body>
</html>
<!-- show.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
name=${param.name }<br>
age=${param.age }<br>
</body>
</html>
request.getParam(“key”)获取到的是请求url后面跟着的参数,例如www.baidu.com?username=张三
request.getParam(“username”) 得到的值就是 张三,getParam All the results are string,因为urlThe address is in itself a string,不指定类型
request没有setParam方法,Not to setparam的键值对
request.getAttribute Is to get on the server side setAttribute 设置进去的值
As in the server code,To do a page jump,A方法跳到B方法,可以在A方法中setAttribute设置键值对,再在B方法中getAttribute 取出来
2.3 paramValues
- Get in the request so the value of the specified argument,其底层实际调用 request.getParameterValues()
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
<form action="${pageContext.request.contextPath }/show.jsp" method="POST">
xxx<input type="text" name="name"/><br>
yyy<input type="text" name="age"/><br>
爱好:
<input type="checkbox" name="hobby" value="sleep">睡觉
<input type="checkbox" name="hobby" value="play">玩
<input type="checkbox" name="hobby" value="eat">吃
<input type="submit" value="点击">
</form>
</body>
</html>
<!-- show.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
name=${param.name }<br>
age=${param.age }<br>
hobby[0]=${paramValues.hobby[0] }<br>
hobby[1]=${paramValues.hobby[1] }<br>
</body>
</html>
2.4 initParam
- 获取初始化参数,其底层调用的是 ServletContext.getInitParameter()
<!-- web.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>07eltttt</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--初始化参数 -->
<context-param>
<param-name>name</param-name>
<param-value>林杰</param-value>
</context-param>
<servlet>
<display-name>Regster</display-name>
<servlet-name>Regster</servlet-name>
<servlet-class>linjie.com.Regster</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Regster</servlet-name>
<url-pattern>/regster</url-pattern>
</servlet-mapping>
</web-app>
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
name=${initParam.name }
</body>
</html>
四、EL访问Bean的属性
1、什么是 java bean
JavaBean是公共Java类,但是为了编辑工具识别
需要满足至少三个条件
- 有一个public默认构造器(例如无参构造器)
- 属性使用public 的get,set方法访问,也就是说设置成private同时get,set方法与属性名的大小也需要对应.例如属性name,get方法就要写成,public String getName(){},N大写.
- 需要序列化.这个是框架,Tool cross-platform reflect the state must be
2、EL访问Bean属性
- EL可以通过 ${key.属性} 的方式获取到指定值,其底层实际调用的是该对象的相应属性的get方法
// Demo.java
package linjie.com;
/* *Bean */
public class Demo {
private String name;
private int age;
public Demo(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return super.toString();
}
}
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="linjie.com.Demo"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Demo test=new Demo("linjie",12);
request.setAttribute("ELttt", test);
%>
name=${requestScope.ELttt.name }<br>
age=${requestScope.ELttt.age }<br>
<!-- 若访问为null的对象的属性,EL是不会抛出空指针异常的,只是不显示而已 -->
names=${requestScope.ELtttxx.name }<br>
</body>
</html>
requestScope 相当于 request 存放有attribute的地方,通过setAttribute和getAttribute来操作requestScope
五、EL访问数组中的数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String[] names={"xlj","lucy","king"};
pageContext.setAttribute("names", names);
%>
name[1]=${names[1] }<br>
<!-- If access to array element subscript beyond the upper limit of an array subscript,ELDon't throw cross-border abnormal,只是不显示 -->
names[5]=${names[5] }<br>
</body>
</html>
package linjie.com;
/* *Bean */
public class Stu {
private String sname;
private String address;
public Stu() {
super();
}
public Stu(String sname, String address) {
super();
this.sname = sname;
this.address = address;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return super.toString();
}
}
<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="linjie.com.*"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Stu[] stus=new Stu[3];
stus[0]=new Stu("xlj","A");
stus[1]=new Stu("lucy","B");
stus[2]=new Stu("kingA","C");
pageContext.setAttribute("stus",stus);
%>
stus[1].Sname=${stus[1].sname }
</body>
</html>
在这里ELExpression does not specify where the values,But direct value to use as name,This operation will be in accordance with the order frompageContext、request、session、application中取数据
六、EL获取List中的数据
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> names=new ArrayList<String>();
names.add("xlj");
names.add("lucy");
pageContext.setAttribute("names", names);
%>
<!-- 因为List底层是数组,所以可以这样写 -->
names[1]=${names[1] }<br>
</body>
</html>
**注意:**EL可以通过索引访问List,但无法访问Set.因为SetNo index concept
七、EL访问Map
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Map<String,Object> map=new HashMap<String,Object>();
map.put("age", 20);
map.put("name", "xlj");
pageContext.setAttribute("map", map);
%>
name=${map.name }<br>
age=${map.age }<br>
</body>
</html>
八、EL中的运算符(empty)
1、常用运算符
- 算术运算符:+、-、*、/、%(不支持++、–)
- 关系运算符:==、!=、>、>=、<、<=
- 逻辑运算符:!、&&、||、not、and、or
- 条件运算符:?:
- 取值运算符:[]、点号
2、empty运算符
用法为${empty 变量},结果为布尔值
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name1=null;
String name2="";
List<String> name3=new ArrayList<String>();
pageContext.setAttribute("name1", name1);
pageContext.setAttribute("name2", name2);
pageContext.setAttribute("name3", name3);
%>
emptyFor variable is not defined,运算结果为true:
empty namex=${empty namex }<br>
empty对于null的引用,运算结果为true:
empty name1=${empty name1 }<br>
empty对于为空串的String引用,运算结果为true:
empty name2=${empty name2 }<br>
emptyWith no element of an array or collection,运算结果为true:
empty name3=${empty name3 }<br>
</body>
</html>
九、自定义EL函数
(There are figure buttyporaDrawing storage limitations,ELFunction is the basic need not,Oneself read here is don't write,I can go to I turned to the original author of)
因为ELItself does not have ability to deal with string,所以可以自定义EL函数
- 定义函数(新建MyEL.java类)
- 注册:先找到jsp2-example-taglib.tld,Copies head and registered function to create your own.tld文件中(.tld放在WEB-INF下)
- 在index.jsp中使用,使用时需要<%@ taglib uri=”http://tomcat.apache.org/jsp2-example-taglib” prefix=”MyEL” %>
1、定义函数 MyEL.java
package linjie.com;
//自定义函数
//该类及其函数,需要在扩展名为.tld的xml文件中注册
//tld:tag library definition(标签库定义)
//xml文件是需要约束的,The need to configure file head.The head restraint can be copy from the file
//在Tomcat安装目录下:webapps\examples\WEB-INF\jsp2
//文件为:jsp2-example-taglib.tld
//这个.tld的xml文件,需要定义在当前web项目的WEB-INF目录下,Created under the directory to.tld结尾的xml文件
//将jsp2-example-taglib.tldIn the head copied to createxml文件中
//The function registered again,还是在jsp2-example-taglib.tldThe bottom copy
public class MyEL {
private static MyEL instance;
public static MyEL getInstance() {
if(instance==null)
{
instance=new MyEL();
}
return instance;
}
//Lowercase string variable capital
public static String LowerToUpper(String str) {
return str.toUpperCase();
}
}
2、将jsp2-example-taglib.tldIn the head part and the bottom of the registration function part copy to create your owntld(在WEB-INF下)文件中
十、EL总结
- ELExpressions can't appear inJava代码块、表达式块等JSP动态代码部分
- EL只能从四大域属性空间中获取数据(pageContext、request、session、application)
- EL不会抛出空指针异常,只会不显示
- EL不会抛出数组越界异常,只会不显示
- EL不具有对字符串进行处理的能力(可以使用JSTL的EL或者自定义EL函数)
边栏推荐
猜你喜欢
随机推荐
[开发杂项][VS Code]remote-ssd retry failed
LeetCode刷题
Unity Day01
Janus转发丢包导致音视频不同步原因分析
[开发杂项][编辑器][代码阅读]ctags&vim
JVM三大常量池与方法区
file permission management ugo
【HIT-SC-LAB2】哈工大2022软件构造 实验2
[English learning][sentence] good sentence
【HIT-SC-MEMO6】哈工大2022软件构造 复习笔记6
LeetCode_Nov_5th_Week
MySQL批量修改时间字段
第一章 绪论
counting cycle
Treating as key frame since WebRTC-SpsPpsIdrIsH264Keyframe is disabled 解决
集合---ArrayList的底层
2020-03-27
JUC并发容器——阻塞队列
C语言无符号整型运算
LeetCode_22_Apr_4th_Week