三、EL表达式语言
1、EL简介
EL(Expression Language)
EL是一种表达式语言(访问作用域对象、获取对象属性的值、执行简单的运算)
表达式语言是一种简单的语法结构,没有复杂的结构,像循环、switch等等
JSP2.0后作为JSP规范的一部分(早期包含在JSTL中)
语法${expr}。如:${user.name}、${user['name']}、${2 + 3}
保留字:
and、or、not(逻辑运算)
eq、ne、gt、ge、lt、le(条件运算)
true、false(布尔类型值)
Instanceof、empty、null(关键字)
mod、div(数学运算)
EL中的运算符
算术运算符
| 算术运算符 | 说明 | 示例 | 结果 |
| + | 加 | ${ 2+4 } | 6 |
| - | 减 | ${ 4-2 } | 2 |
| * | 乘 | ${ 2*4 } | 8 |
| /(div) | 除 | ${ 4/2 } | 2 |
| %(mod) | 取模(求余) | ${ 4%3 } | 1 |
关系运算符
| 关系运算符 | 说明 | 示例 | 结果 |
| ==(eq) | 等于 | ${ 2==4 } | false |
| !=(ne) | 不等于 | ${ 4!=2 } | true |
| >(gt) | 大于 | ${ 2>4 } | false |
| >=(ge) | 大于等于 | ${ 4>=2 } | true |
| <(lt) | 小于 | ${ 4<3 } | false |
| <=(le) | 小于等于 | ${ 3<=4 } | true |
逻辑运算符
| 逻辑运算符 | 说明 | 示例 | 结果 |
| &&(and) | 逻辑与 | ${ 2>1 && 5>6 } | false |
| ||(or) | 逻辑或 | ${ 2>1 || 5>6 } | true |
| !(not) | 逻辑非 | ${ !(2>1) } | false |
empty判断为null或者为空(A不存在则${empty A}为true)
条件运算符(?:)
小括号()
优先等级
例子:
创建动态网站工程el
创建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL表达式</title>
</head>
<body>
2 + 3 = ${2 + 3}<br/>
8 % 3 = ${8 % 3}<br/>
<hr/>
3 == 4 = ${3 == 4}<br/>
3 > 4 = ${3 > 4}<br/>
<hr/>
3>4 || 4==4 = ${3>4 || 4==4}<br/>
! true = ${!true}<br/>
<hr/>
user = ${user}|
<hr/>
empty user = ${empty user}<br/>
</body>
</html>
执行结果:
2、EL中访问隐式对象
pageContext
通过pageContext可以获取servletContext、request、response、session等对象
格式:${pageContext.request.locale}
获取request中用户请求的值:${param.name}
获取请求头的信息:${headers['Content-type']}
获取cookie值:${cookie.cookieName.value}
获取context-param:${initParam.name}
例子:
在index.jsp中添加
直接获取request:${request }<br/>
通过pageContext获取request:${pageContext.request.locale }<br/>
通过pageContext获取servletContext:${pageContext.servletContext.contextPath }<br/>
<hr/>
user-agent1:<%=request.getHeader("user-agent") %><br/>
user-agent2:${header['user-agent'] }<br/>
<hr/>
执行结果:

获取隐式作用域对象中保存的属性
格式:${对象名.属性}
说明:按照pageScope、requestScope、sessionScope、applicationScope的顺序查找
${pageScope.user.name}只在pageContext中查找
3、EL中调用函数(很少用到)
定义函数(就是java类的方法)
1)定义一个类,创建公共的静态方法(函数)
EL表达式只能调用公共的静态方法
package com.test.functions;
public class MyFun {
public static String hi(String name) {
return "Hi " + name;
}
}
2)在WEB-INF/tlds下创建myfun.tld文件
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>mfun</short-name>
<uri>http://com.test.functions/myfun</uri>
<function>
<name>hi</name>
<function-class>com.test.functions.MyFun</function-class>
<function-signature>
java.lang.String hi(java.lang.String)
</function-signature>
</function>
</taglib>
3)创建tlds.jsp,引用并调用
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="mfun" uri="http://com.test.functions/myfun"%>
<html>
<head>
<title>JSP</title>
</head>
<body>
${mfun:hi("admin")}
</body>
</html>
4)执行结果
页面显示:Hi admin
974

被折叠的 条评论
为什么被折叠?



