jsp EL表达式详解

2022-07-26,,,

一、el表达式介绍

  • expression language表达式语言
  • 是一种在jsp页面获取数据的简单方式(只能获取数据,不能设置数据)
  • 在jsp2.0开始引入概念

语法格式

在jsp页面的任何静态部分均可通过:${expression}来获取到指定表达式的值

二、el获取数据(从四大域中获取属性)
el只能从四大域中获取属性
1、如果没有使用el的内置对象,则查找数据顺序是依次按照由小到大范围从四大域中查找指定名称的属性值

二、el获取数据(从四大域中获取属性)

el只能从四大域中获取属性

1、如果没有使用el的内置对象,则查找数据顺序是依次按照由小到大范围从四大域中查找指定名称的属性值

- pagecontext<request<session<application

  <%@ 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>

可以看出没有使用el内置对象时查找顺序是由小到大,所以最先显示的是pagecontext域中的

2、使用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=${applicationscope.name }
</body>
</html>

可以看出,使用applicationscope即可指定application域中的name输出,当然其他域也是类似,下文会说这四大域属性相关的内置对象

三、el中的内置对象

el有11个内置对象,这里主要讲域属性相关的4个和其他4个
el的11个内置对象,除了pagecontext以外,其他10个内置对象的类型都是java.util.map类型

1、域属性相关(4个)

  • pagescope:从page范围域属性空间中查找指定的key
  • requestscope:从request范围域属性空间中查找指定的key
  • 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、其他重要内置对象(4个)

1、pagecontext

该pagecontext与jsp内置对象pagecontext是同一个对象。通过该对象,可以获取到request、response、session、servletcontext、servletconfig等对象注意:这些对象在el里不是内置对象,这些对象只能通过pagecontext获取

在el中直接${pagecontext.request}即可获取request对象,其底层调用的是pagecontext.getrequest()方法。同理,也可以通过类似方法获取其他对象

重点:其中最常用的:${pagecontext.request.contextpath },代表web应用下的根,可以看出下面action中的路径可读性更强了

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、param(获取请求中的指定参数)

其底层实际调用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>

客户浏览器访问结果

 

3、paramvalues

获取请求中的指定参数的所以值,其底层实际调用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>

客户浏览器显示结果

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大写。
  • 需要序列化。这个是框架,工具跨平台反映状态必须的

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>

客户浏览器显示结果

五、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>

 <!-- 若访问的数组元素下标超出了数组下标上限,el不会抛出越界异常,只是不显示 -->
 names[5]=${names[5] }<br>
</body>
</html>

下面是访问类的数组

stu.java

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>

客户浏览器显示结果

六、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。因为set中没有索引概念

七、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);
 %>
 empty对于没有定义的变量,运算结果为true:
 empty namex=${empty namex }<br>

 empty对于null的引用,运算结果为true:
 empty name1=${empty name1 }<br>

 empty对于为空串的string引用,运算结果为true:
 empty name2=${empty name2 }<br>

 empty对于没有元素的数组或集合,运算结果为true:
 empty name3=${empty name3 }<br>
</body>
</html>

客户浏览器显示结果

九、自定义el函数

因为el本身不具有处理字符串能力,所以可以自定义el函数
- 定义函数(新建myel.java类)
- 注册:先找到jsp2-example-taglib.tld,将头部以及注册函数复制到自己创建的.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文件是需要约束的,即需要配置文件头部。这个头部约束可以从一下文件中进行复制
//在tomcat安装目录下:webapps\examples\web-inf\jsp2
//文件为:jsp2-example-taglib.tld

//这个.tld的xml文件,需要定义在当前web项目的web-inf目录下,在此目录下创建以.tld结尾的xml文件
//将jsp2-example-taglib.tld中头部复制到创建的xml文件中

//再将函数注册,还是在jsp2-example-taglib.tld底部中复制
public class myel {
 private static myel instance;
 public static myel getinstance() {
  if(instance==null)
  {
   instance=new myel();
  }
  return instance;
 }

 //字符串小写变大写
 public static string lowertoupper(string str) {
  return str.touppercase();
 }
}

2、将jsp2-example-taglib.tld中头部部分以及底部的注册函数部分复制到自己创建的tld(在web-inf下)文件中

myel.tld

<?xml version="1.0" encoding="utf-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 version="2.0">

 <!-- 定义标签库信息 -->
 <description>a tag library exercising simpletag handlers.</description>
 <tlib-version>1.0</tlib-version>
 <short-name>myel</short-name><!-- 标签库名称,一般定义成和文件名一样 -->
 <uri>http://tomcat.apache.org/jsp2-example-taglib</uri>

 <!-- 注册函数 -->
  <function>
  <name>mylowertoupper</name>
  <function-class>linjie.com.myel</function-class><!-- 方法得类 -->
  <function-signature>java.lang.string lowertoupper( java.lang.string )</function-signature><!-- 方法签名 :需要返回值以及方法名、参数-->
 </function>
</taglib>

3、在index.jsp中使用,使用时需要<%@ taglib uri=”http://tomcat.apache.org/jsp2-example-taglib” prefix=”myel” %>

<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<%@ taglib uri="http://tomcat.apache.org/jsp2-example-taglib" prefix="myel" %><!-- tld中的uri和short-name -->
<!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>
 <!-- 这个方法名是在tld注册时的name -->
 ${myel:mylowertoupper("sasas") }<br>


 <!-- el函数只能处理四大域中的属性值及string常量 -->
 <%
  string name="xlj";
  pagecontext.setattribute("name", name);
 %>
 ${myel:mylowertoupper(name) }<br>

</body>
</html>

客户浏览器显示结果

十、el总结

  • el表达式不能出现在java代码块、表达式块等jsp动态代码部分
  • el只能从四大域属性空间中获取数据(pagecontext、request、session、application)
  • el不会抛出空指针异常,只会不显示
  • el不会抛出数组越界异常,只会不显示
  • el不具有对字符串进行处理的能力(可以使用jstl的el或者自定义el函数)

到此这篇关于jsp el表达式详解的文章就介绍到这了,更多相关jsp el表达式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《jsp EL表达式详解.doc》

下载本文的Word格式文档,以方便收藏与打印。