在JSP页面的任何静态部分均可通过:${expression}来获取到指定表达式的值
二、EL获取数据(从四大域中获取属性)
EL只能从四大域中获取属性
1、如果没有使用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有11个内置对象,这里主要讲域属性相关的4个和其他4个
EL的11个内置对象,除了pageContext以外,其他10个内置对象的类型都是java.util.Map类型
<%@ 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>
该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>
其底层实际调用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.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>
客户浏览器显示结果
获取初始化参数,其底层调用的是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>
客户浏览器显示结果
JavaBean是公共Java类,但是为了编辑工具识别
需要满足至少三个条件
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>
客户浏览器显示结果
<%@ 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>
客户浏览器显示结果
<%@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中没有索引概念
<%@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>
客户浏览器显示结果
用法为${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函数
- 定义函数(新建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>
客户浏览器显示结果
到此这篇关于jsp EL表达式详解的文章就介绍到这了,更多相关jsp EL表达式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章: