2.EL表达式&JSTL标签库常用方法
1.EL表达式
Expression Language表达式语言,主要是代替jsp页面中的表达式脚本在jsp页面中进行数据的输出。
格式为${表达式}
- EL表达式输出Bean的普通属性、数组属性、List集合属性、map集合属性。
//Person测试类 注意到Person类中没有age属性,但是写了一个public int getAge()方法
package bean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author Forrestyu
* @create 2021-04-18 20:29
*/
public class Person {
private String name;
private String[] phones;
private List<String> cities;
private Map<String,Object> map;
public int getAge() {
return 18;
}
public Person() {
}
public Person(String name, String[] phones, List<String> cities, Map<String, Object> map) {
this.name = name;
this.phones = phones;
this.cities = cities;
this.map = map;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getPhones() {
return phones;
}
public void setPhones(String[] phones) {
this.phones = phones;
}
public List<String> getCities() {
return cities;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", phones=" + Arrays.toString(phones) +
", cities=" + cities +
", map=" + map +
'}';
}
}
//el.jsp
<%@ page import="bean.Person" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
Created by IntelliJ IDEA.
User: Forrestyu
Date: 2021/4/18
Time: 20:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
//new一个Person类对象,用于测试
Person person = new Person();
person.setName("kkk");
person.setPhones(new String[]{"0000000","1111111","2222222"});
ArrayList<String> array = new ArrayList<>();
array.add("beijing");
array.add("shanghai");
array.add("shenzhen");
array.add("guangzhou");
person.setCities(array);
Map<String,Object> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
person.setMap(map);
pageContext.setAttribute("p",person);
%>
输出Person:${ p }<br/>
输出Person的name属性:${ p.name }<br/>
输出Person的phones数组属性值:${ p.phones[0] }<br/>
输出Person的cities集合中的元素值:${ p.cities }<br/>
输出Person的List集合中的个别元素值:${ p.cities[1] }<br/>
输出Person的Map集合:${ p.map }<br/>
输出Person的Map集合个别元素值:${ p.map[3] }<br/>
输出Person的age属性:${ p.age }<br/>
</body>
</html>
注意到这里p.属性其实是在类中找到相应的getXxx方法,并不是单纯的找该属性的值。由最后一个age属性可以看出来
- EL表达式的运算问题
除了和java相同的关系运算、逻辑运算、算数运算、三元运算之外。还提供了:
- empty运算,使用方法为:
&{ empty variable }
来判断该变量是否为空。 - "."运算和"[]"运算
比如属性值key中含有.-+等特殊字符,就需要使用[]
${ map.a.a.a }其实是属性key是a.a.a,但此时并不能解析出来,需要${ map.['a.a.a'] }
- EL表达式的11个隐含对象
这是EL表达式中自己定义的,可以直接使用
| 变量 | 类型 | 作用 |
|---|---|---|
| pageContext | PageContextImpl | 它可以获取 jsp 中的九大内置对象 |
| pageScope | Map<String,Object> | 它可以获取 pageContext 域中的数 |
| requestScope | Map<String,Object> | 它可以获取 requestContext 域中的数 |
| sessionScope | Map<String,Object> | 它可以获取 sessionContext 域中的数 |
| applicationScope | Map<String,Object> | 它可以获取 applicationContext 域中的数 |
| param | Map<String,String> | 它可以获取请求参数的值 |
| paramValues | Map<String,String[]> | 它也可以获取请求参数的值,获取多个值的时候使用。 |
| header | Map<String,String> | 它可以获取请求头的信息 |
| headerValues | Map<String,String[]> | 它可以获取请求头的信息,它可以获取多个值的情况 |
| cookie | Map<String,Cookie> | 它可以获取当前请求的 Cookie 信息 |
| initParam | Map<String,String> | 它可以获取在 web.xml 中配置的 |
- EL获取四个特定域中的属性
<%
pageContext.setAttribute("key1", "pageContext1");
request.setAttribute("key1", "request");
session.setAttribute("key1", "session");
application.setAttribute("key1", "application");
%>
${ pageScope.key1 }
${ requestScope.key1 }
${ sessionScope.key1 }
${ applicationScope.key1 }
- pageContext的作用
一般用来获取协议、服务器IP、服务器端口、工程路径、请求方法等等;操作jsp的九大内置对象
<%=request.getScheme() %> <br>//原本需要使用getXxx()
<%
pageContext.setAttribute("req", request);
%>
1.协议: ${ req.scheme }<br>
2.服务器 ip:${ pageContext.request.serverName }<br>
3.服务器端口:${ pageContext.request.serverPort }<br>
4.获取工程路径:${ pageContext.request.contextPath }<br>
5.获取请求方法:${ pageContext.request.method }<br>
6.获取客户端 ip 地址:${ pageContext.request.remoteHost }<br>
7.获取会话的 id 编号:${ pageContext.session.id }<br>
- 其他隐含对象的使用
//这里测试用的请求参数为http://localhost:8080/09_EL_JSTL/other_el_obj.jsp?username=kkk&password=666666&hobby=java&hobby=cpp
输出请求参数 username 的值:${ param.username } <br>
输出请求参数 password 的值:${ param.password } <br>
输出请求参数 username 的值:${ paramValues.username[0] } <br>
输出请求参数 hobby 的值:${ paramValues.hobby[0] } <br>
输出请求参数 hobby 的值:${ paramValues.hobby[1] } <br>
输出请求头【User-Agent】的值:${ header['User-Agent'] } <br>
输出请求头【Connection】的值:${ header.Connection } <br>
输出请求头【User-Agent】的值:${ headerValues['User-Agent'][0] } <br>
获取 Cookie 的名称:${ cookie.JSESSIONID.name } <br>
获取 Cookie 的值:${ cookie.JSESSIONID.value } <br>
2.JSTL标签库
EL表达式主要是为了替换 jsp 中的表达式脚本,而标签库则是为了替换代码脚本。
JSTL的使用步骤如下:
- 先导入 jstl 标签库的 jar 包。
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar - 、第二步,使用 taglib 指令引入标签库。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
使用:
<c:set/>:往域中保存数据
scope 属性设置保存到哪个域
page 表示 PageContext 域(默认值)
request 表示 Request 域
session 表示 Session 域
application 表示 ServletContext 域
var 属性设置 key 是多少
value 属性设置值
<c:set scope="" var="" value="" />
<c:if/>
if 标签用来做 if 判断。
test 属性表示判断的条件(使用EL表达式输出)
<c:if test=""/>
<%--
<c:choose> <c:when> <c:otherwise>标签
作用:多路判断。跟 switch ... case .... default 非常接近
choose 标签开始选择判断
when 标签表示每一种判断情况
test 属性表示当前这种判断情况的值
otherwise 标签表示剩下的情况
<c:choose> <c:when> <c:otherwise>标签使用时需要注意的点:
1、标签里不能使用 html 注释,要使用 jsp 注释
2、when 标签的父标签一定要是 choose 标签
--%>
<c:foreach/>
items 表示遍历的集合
var 表示遍历到的数据
begin 表示遍历的开始索引值
end 表示结束的索引值
step 属性表示遍历的步长值
varStatus 属性表示当前遍历到的数据的状态
滴水穿石、燕子衔泥,点点滴滴都是添补

浙公网安备 33010602011771号