1 EL表达式
1.1 EL的概述
- EL,全名为Expression Language。
- 主要作用:
- ①EL表达式主要用于替换jsp页面中的脚本表达式,以便于从各种类型的web域中检索java对象(某个web域中的对象),获取数据(访问javabean的属性、访问List集合、访问Map集合、访问数组)。
- ②执行运算。
- ③利用EL表达式可以在jsp页面中执行一些基本的关系运算、逻辑运算和算数运算,以便于在jsp页面完成一些简单的逻辑运算。
- ④获取web开发常用的对象。
- ⑤EL表达式定义了一些隐式的对象,利用这些隐式对象,web开发人员可以很轻松的获取web常用对象的引用,从而获取这些对象中的数据。
- ⑥调用案卷方法。
- ⑦EL表达式允许用户开发自定义的EL函数,以便于在jsp页面通过EL表达式调用java类的方法。
1.2 EL的基本语法
- ${标识符}
- EL表达式在执行的时候,会调用pageContext.findAttribute("")方法,用标识符作为关键字。分别从page、request、session、application四个域对象中查找对应的对象,如果找到了,就返回对应的对象;否则返回""。(不是null)
<%
List<User> users = (List<User>) request.getSession().getAttribute("users");
out.write(users.get(0).getUsername());
%>
1.3 获取数据
package com.domain;
import java.io.Serializable;
/**
* 2017/11/6
* 说明:
*/
public class Student implements Serializable {
private String name;
private int age;
private String gender;
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
<%@ page import="com.domain.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
Student student = new Student();
student.setName("刘谦");
student.setAge(29);
student.setGender("男");
pageContext.setAttribute("student",student);
%>
${student}
<%--
${student} 相等于
Student student = (Student)pageContext.findAttribute("student");
out.write(student)
--%>
</body>
</html>
<%@ page import="com.domain.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
Student student = new Student();
student.setName("刘谦");
student.setAge(29);
student.setGender("男");
pageContext.setAttribute("student",student);
%>
${student.name}
<%--
${student} 相等于
Student student = (Student)pageContext.findAttribute("student");
out.write(student.getName())
--%>
</body>
</html>
package com.domain;
/**
* 2017/11/6
* 说明:
*/
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package com.domain;
import java.io.Serializable;
/**
* 2017/11/6
* 说明:
*/
public class Student implements Serializable {
private String name;
private int age;
private String gender;
private Address address = new Address();
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
<%@ page import="com.domain.Student" %>
<%@ page import="com.domain.Address" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
Address address = new Address();
address.setProvince("江苏省");
address.setCity("泰州市");
Student student = new Student();
student.setName("刘谦");
student.setAge(29);
student.setGender("男");
student.setAddress(address);
pageContext.setAttribute("student",student);
%>
${student.address.province}
</body>
</html>
<%@ page import="com.domain.Student" %>
<%@ page import="com.domain.Address" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
String[] strs = {"aa","bb"};
pageContext.setAttribute("strs",strs);
%>
${strs[0]}
</body>
</html>
<%@ page import="com.domain.Student" %>
<%@ page import="com.domain.Address" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
Map<String,String> map = new HashMap<>();
map.put("aa","bb");
map.put("cc","dd");
pageContext.setAttribute("map",map);
%>
${map.aa}
</body>
</html>
<%@ page import="com.domain.Student" %>
<%@ page import="com.domain.Address" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
List<String> list = new ArrayList<>();
list.add("aa");
list.add("bb");
pageContext.setAttribute("list",list);
%>
${list[0]}
</body>
</html>
1.4 EL的运算
- empty运算符:判断一个字符串是否为""或其他对象是否为null,同时还能判断是集合中是否含有元素。
<%@ page import="com.domain.Student" %>
<%@ page import="com.domain.Address" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
List<String> list = new ArrayList<>();
pageContext.setAttribute("s","");
pageContext.setAttribute("s1",null);
pageContext.setAttribute("s2",list);
%>
${empty s1}<%--true--%>
${empty s1}<%--true--%>
${empty s2}<%--true--%>
</body>
</html>
<%@ page import="com.domain.Student" %>
<%@ page import="com.domain.Address" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
pageContext.setAttribute("gender","男");
%>
<input type="radio" name="gender" value="男" ${gender == "男"?"checked='true'":""}>男<br/>
<input type="radio" name="gender" value="女" ${gender == "女"?"checked='true'":""}>女<br/>
</body>
</html>
1.5 EL的隐式对象(11个)
EL隐式对象的名称 |
表示的类型 |
JSP的隐式对象 |
备注 |
pageContext |
javax.servlet.jsp.PageContext |
pageContext |
一样 |
pageScope |
java.utl.Map |
没有 |
页面域范围的数据 |
sessionScope |
java.utl.Map |
没有 |
reqeust域范围的数据 |
requestScope |
java.utl.Map |
没有 |
session域范围的数据 |
aplicattionScope |
java.utl.Map |
没有 |
application域范围的数据 |
header |
java.utl.Map |
没有 |
key:请求消息头 value:消息头对应的值 |
headerValues |
java.utl.Map |
没有 |
key:请求消息头 value:消息头对应的值数组 |
param |
java.utl.Map |
没有 |
key:请求参数名,value:请求参数值 |
paramValues |
java.utl.Map |
没有 |
key:请求参数名,value:请求参数值数组 |
cookie |
java.utl.Map |
没有 |
key:Cookie对象的name ,value是Cookie对象本身 |
initParam |
java.utl.Map |
没有 |
key:全局参数的name,value是全局参数的值 |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
pageContext.setAttribute("p","p");
request.setAttribute("p","rp");
session.setAttribute("p","sp");
application.setAttribute("p","ap");
%>
${p} <%=pageContext.findAttribute("p")%> <br/>
${pageScope.p} <%=pageContext.getAttribute("p")%> <br/>
${requestScope.p} <%=request.getAttribute("p")%> <br/>
${sessionScope.p} <%=session.getAttribute("p")%> <br/>
${applicationScope.p} <%=application.getAttribute("p")%> <br/>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
${header["Accept-Encoding"]}<br/>
<%=request.getHeader("Accept-Encoding")%>
</body>
</html>
1.6 定义和使用EL函数
1.6.1 定义EL函数的开发步骤
- ①定义一个普通类,提供实现功能的静态方法(EL函数只能调用静态方法)。
package com;
/**
* 2017/11/6
* 说明:
*/
public class ELFunction {
public static String toUpperCase(String str) {
return str.toUpperCase();
}}
- ②EL函数的配置:在WEB-INF目录下建立一个扩展名为*.tld的xml文件,此时新建的*.tld的名称为elfunction.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>elfunction</short-name>
<uri>http://www.xuweiwei.com/jsp/functions</uri>
<function>
<name>toUpperCase</name>
<function-class>com.ELFunction</function-class>
<function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>
</function>
</taglib>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="elfunction" uri="http://www.xuweiwei.com/jsp/functions" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
pageContext.setAttribute("s","abcde");
%>
${elfunction:toUpperCase(s)}
</body>
</html>
2 JSTL核心标签库
- 导入standard.jar和jstl-api.jar包
- 在jsp的头部添加如下的代码
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="elfunction" uri="http://www.xuweiwei.com/jsp/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
pageContext.setAttribute("s","abcde");
%>
<c:if test="${not empty s}">
${s}
</c:if>
</body>
</html>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="elfunction" uri="http://www.xuweiwei.com/jsp/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
List<String> lists = new ArrayList<>();
lists.add("aa");
lists.add("bb");
lists.add("cc");
pageContext.setAttribute("lists",lists);
%>
<c:if test="${not empty lists}">
<c:forEach items="${lists}" var="list">
${list}
</c:forEach>
</c:if>
</body>
</html>