用途:
只能用于输出,用来替换<%= %>的
1. EL表达式的基本形式为:${var}。
如果在jsp模板文件中使用EL表达式,那么表达式的值回直接输出到网页上。
例如:Java表达式:<%=request.getParameter("usernamae") %> --> ${param.username} (form)
2. 从四大域中获取数据
- pageContext
- request
- session
- application
搜索范围:pageContext -> request -> session -> application,如果检索不到,则输出""
指定在某个与中检索:pageScope , requestScope, sessionScope, applicationScope, e.g ${requestScope.name}
既可以直接插入到jsp文件的模板文件中,也可以作为jsp标签的属性的值。
e.g. <input type="text" name="username" value="${param.username}" />
3. 直接访问对象的属性--JavaBean导航
自动调用getXxx方法。
e.g. ${request.user.username} <==> ${request.user['username']}
4. EL运算符:
算数运算符:
+ - * /(不是整除)%
关系运算符:
== < >..
逻辑运算符
! || &&
empty运算符
${empty var} :如果var为null或者集合【数组】长度为0或者字符串为空,就返回true
条件运算符
a?b:c
5. EL 语言中定义了11个隐含对象
他们都是java.util.Map类型【除了pageContext】,网页制作者通过他们便捷地访问web应用的特定数据。
隐含对象名 说明
- applicationScope: web应用范围内的属性名和属性值进行映射 【application属性】
- cookie: 客户请求中的Cookie名和Cookie对象进行映射
- header: 请求头部的项目名和项目值进行映射,例如:
${header.host} 等价于<%=request.getHeader("host") %?
- headerValues: 把Http请求头部的项目名和所有匹配你的项目值得数组进行映射,例如:
${headerValues["accept-language"]} 等价于<% request.getHeaders("accept-language") %>
- initParam :把web应用的初始化参数名和参数值进行映射 【应用程序的全局初始化参数】
- pageContext:标识PageContext对象,把页面范围内的属性名和属性值进行映射 【一个顶九个】 e.g ${pageContext.request.contextPath} 获取项目根路径
- pageScope :
- param: 把客户请求的参数名和参数值进行映射【请求参数】
- paramValues: 请求参数名对应参数数组
- requestScope: 请求范围内的属性名和属性值【请求属性,请求传值】
- sessionScope: 会话的范围内的属性名和属性值的映射 【session范围的属性】
分类:
(1)请求范围内的特定数据:header,headerValues,param,paramValues,cookie
${header["user-agent"]} (必须使用[]访问,因为不符合变量命名规则) <==> <%=requesst.getHeader("user-agent") %>
${cookie.username.value} <==> 调用名为username的Cookie对象的getValue()方法
与前端cookie的关系
(2)特定范围的属性值:pageScope,requestScope,sessionScope和applicationScope
${sessionScope.cart.total} 属性
${applicationScope.counter.count}
(3)pageContext,表示PageContext对象
${pageContext.request.contentPath}
${pageContext.servletContext.serverInfo} <==> <%=application.getServerInfo %>
${pageContext.request.requestURL} <==> <%=request.getRequestURL() %>
${pageContext.response.characterEncoding} <==> <%= response.getCharacterEncoding() %>
(4)表示web应用的初始化参数集合,为initParam
在web.xml中配置:
<context-param> <param-name>abc</param-name> <param-value>hello</param-value> </context-param>
${initParam.abc} <==> <%=appliction.getInitParameter("abc") %>
6.命名变量
EL表达式中的变量成为命名变量,他不是jsp文件的局部变量或实例变量,而是存放在特定范围内的属性,
命名变量的名字和属性名对应。
${username} 找的是特定范围的属性(不是参数)
${user.username} 实际上找的不是User的username属性,而是调用了User的getUsername方法,返回的结果
7. EL函数
7.1 jstl提供的函数库
基于EL表达式:
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
e.g.
<% request.setAttribute("str", "hello"); request.setAttribute("arr", new int[]{1,2,3}); %> ${fn:length(str)} ${fn:length(arr)}
7.2 自定义函数
创建静态函数:
package cn.getword.function; public class MyElFunction { public static String toUpperCase(String str){ System.out.println("Myfunction ..."); return str.toUpperCase(); } }
编写tld【标签库描述】文件,放在WEB-INF下面:
<?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>myfn</short-name> <!-- 使用短名称,对应jsp中taglib指令中的prefix属性值 --> <!-- uri:把当前的方法库绑定到一个uri地址上,在该网址上不一定存在方法库 随便指定 --> <uri>http://www.cn.getword.function.Myfunction</uri> <function> <description>Reverses the characters in the given String</description> <name>toUpperCase</name> <!-- 方法的名称,对应jsp中的方法名 --> <function-class>cn.getword.function.MyElFunction</function-class> <!-- 执行方法的类 --> <!-- 方法名,对应自定义类中的方法 数据类型必须写全称 --> <function-signature>java.lang.String toUpperCase( java.lang.String )</function-signature> </function> </taglib>
使用自定义EL函数:
${myfn:toUpperCase("hello world")}
end