zl程序教程

您现在的位置是:首页 >  其他

当前栏目

EL表达式&JSTL标签库笔记

amp笔记 标签 表达式 el JSTL
2023-06-13 09:13:09 时间

文章目录

一、EL表达式

1、什么是EL表达式,EL表达式的作用?

EL表达式的全称是:Expression Language。是表达式语言。 EL表达式主要是代替jsp页面中的表达式脚本在jsp页面中进行数据输出。 因为EL表达式在输出数据的时候,要比jsp的表达式脚本要简洁很多。

<body>
  <%
    request.setAttribute("key","value");
  %>
    表达式脚本输出key的值是:
<%=request.getAttribute("key")%> <br>
    EL表达式输出key的值是:${key} <br>
  表达式脚本输出key1的值是:
  <%=request.getAttribute("key1")%> <br>
  表达式脚本输出key1的值是:
  <%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%> <br>
  EL表达式输出key的值是:${key1}
</body>

EL表达式的格式是:${表达式} EL表达式在输出null值的时候,输出的是空串。jsp表达式脚本输出null值的时候,输出的是null字符串。

2、EL表达式搜索域数据的顺序

EL表达式主要是在jsp页面输出数据。 主要是输出域对象中的数据。 当四个域对象中都有相同的key的数据的时候,EL表达式会按照四个域的从小到大的顺序去进行搜索,找到就输出。

<body>
<%
    //往四个域对象中保存相同的key数据
    request.setAttribute("key","request");
    session.setAttribute("key","session");
    application.setAttribute("key","application");
    pageContext.setAttribute("key","pageContext");
%>
${key}
</body>

3、EL表达式输出Bean的普通属性,数组属性、List集合属性、map集合属性

Person类:

package com.javaweb.bean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Person {
    private String name;
    private String[] phone;
    private List<String> cities;
    private Map<String,Object> map;

    public Person() {
    }

    public Person(String name, String[] phone, List<String> cities, Map<String, Object> map) {
        this.name = name;
        this.phone = phone;
        this.cities = cities;
        this.map = map;
    }
    public int getAge() {
        return 20;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getPhone() {
        return phone;
    }

    public void setPhone(String[] phone) {
        this.phone = phone;
    }

    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 + '\'' +
                ", phone=" + Arrays.toString(phone) +
                ", cities=" + cities +
                ", map=" + map +
                '}';
    }
}

jsp代码:

<body>
<%
    Person person = new Person();
    person.setName("Java程序员");
    person.setPhone(new String[]{"18865895412","15268451145"});
    List<String> cities = new ArrayList<String>();
    cities.add("西安");
    cities.add("渭南");
    cities.add("深圳");
    person.setCities(cities);
    HashMap<String,Object> hashmap = new HashMap<>();
    hashmap.put("key1","value1");
    hashmap.put("key2","value2");
    hashmap.put("key3","value3");
    person.setMap(hashmap);
    pageContext.setAttribute("person",person);
%>
输出person:${person}<br>
输出person的name属性:${person.name}<br>
输出person的phone数组属性值:${person.phone[0]}<br>
输出person的cities属性:${person.cities}<br>
输出perso的List的个别元素值:${person.cities[2]}<br>
输出person的Map集合:${person.map}<br>
输出person的Map集合中某个key值:${person.map.key2}<br>
输出person的age属性:${person.age}
</body>

在EL表达式里面,输出一个对象的属性时,不是直接找这个属性,而是找这个属性对应的get方法。

4、EL表达式–运算

语法: ${运算表达式},EL表达式支持如下运算符: (1)、关系运算

(2)、逻辑运算

(3)、算术运算

(4)、empty运算 empty运算可以判断一个数据是否为空,如果为空,则输出true,不为空输出false。 以下几种情况为空: 1、值为null值的时候为空 2、值为空串的时候为空 3、值是Object类型数组,长度为0的时候 4、list集合元素个数为0 5、map集合元素个数为0的时候

<body>
<%
//  1、值为null值的时候为空
  request.setAttribute("emptyNull",null);
//  2、值为空串的时候为空
  request.setAttribute("emptyStr","");
//  3、值是Object类型数组,长度为0的时候
  request.setAttribute("emptyArr",new Object[]{});
//  4、list集合元素个数为0
  List list = new ArrayList();
  request.setAttribute("emptyList",list);
//  5、map集合元素个数为0的时候
  HashMap map = new HashMap();
  map.put("key","value");
  request.setAttribute("emptyMap",map);
%>
${empty emptyNull}<br>
${empty emptyStr}<br>
${empty emptyArr}<br>
${empty emptyList}<br>
${empty emptyMap}<br>
</body>

(5)、三元运算 表达式1?表达式2:表达式3 如果表达式1的值为真,则返回表达式2的值,如果表达式1的值为假,则返回表达式3的值。 (6)、”.“点运算和 [] 中括号运算符 .点运输,可以输出Bean对象中某个属性的值。 []中括号运算,还可以输出map集合中key里含有特殊字符的key的值

<body>
<%
  HashMap<String,Object> map = new HashMap();
  map.put("a.a.a","aValue");
  map.put("b+b+b","bValue");
  map.put("c-c-c","cValue");
  request.setAttribute("map",map);
%>
${map.a.a.a}<br>
${map.b+b+b}<br>
${map['a.a.a']}<br>
${map['b+b+b']}<br>
${map['c-c-c']}<br>
</body>

5、EL表达式的11个隐含对象

EL表达式中11个隐含对象,是EL表达式中自己定义的,可以直接使用。

1、EL获取四个特定域中的属性

pageScope:pageContext域 requestScope:Request域 sessionScope:Session域 applicationScope:ServletContext域

<body>
<%
  pageContext.setAttribute("key1","pageContext1");
  request.setAttribute("key2","request");
  session.setAttribute("key2","session");
  application.setAttribute("key2","application");
  pageContext.setAttribute("key2","pageContext2");

%>
${applicationScope.key2}
</body>

2、pageContext对象的使用

1、协议: 2、服务器ip: 3、服务器端口: 4、获取工程路径: 5、获取请求方法: 6、获取客户端ip地址: 7、获取会话的id编号:

<body>
<%--
request.getScheme() 它可以获取请求的协议
request.getServerName() 获取请求的服务器ip或域名
request.getServerPort() 获取请求的服务器端口号
getContextPath() 获取当前工程路径
request.getMethod() 获取请求的方式(GET或POST)
request.getRemoteHost()  获取客户端的ip 地址
session.getId() 获取会话的唯一标识
--%>
<%pageContext.setAttribute("req",request);%>
<%=request.getScheme()%><br>
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>
</body>

3、EL表达式其他隐含对象的使用

param:Map<String,String>它可以获取请求参数值 paramValues:Map<String,String[]>它也可以获取请求参数值,获取多个值的时候使用

输出请求参数username的值:${param.username}<br>
输出请求参数password的值:${param.password}<br>

输出请求参数username的值:${paramValues.username[0]}<br>
输出请求参数hobby的值:${paramValues.hobby[0]}<br>
输出请求参数hobby的值:${paramValues.hobby[1]}<br>
</body>

访问地址:http://localhost:8080/EL_JSTL/other_EL_obj.jsp?username=wzl168&password=1234&hobby=java&hobby=python

header:Map<String,String>它可以获取请求头的信息 headerValues:Map<String,String[]>它可以获取请求头的信息,它可以获取多个值的i情况

输出请求头User-Agent的值:${header['User-Agent']}<br>
输出请求头Connection的值:${header.Connection}<br>
输出请求头Host的值:${header.Host}<br>
输出请求头User-Agent的值:${headerValues['User-Agent'][0]}<br>

cookie:Map<String,Cookie>它可以获取当前请求的cookie信息

获取cookie的名称:${cookie.JSESSIONID.name}<br>
获取cookie的值:${cookie.JSESSIONID.value}<br>

initParam:Map<String,String>它可以获取在web.xml中配置的上下文参数

    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql///test</param-value>
    </context-param>
输出&lt;context-param&gt;username的值:${initParam.username}<br>
输出&lt;context-param&gt;url的值:${initParam.url}

二、JSTL标签库

JSTL标签库全称是指 JSP Standard Tag Library JSP标准标签库。是一个不断完善的开放源代码的JSP标签库。 EL表达式主要是为了替换jsp中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个jsp页面变得更加简洁。 JSTL由五个不同功能的标签库组成。

在jsp标签库中使用taglib指令引入标签库。 IDEA会自动导入。

1.JSTL标签库的使用步骤

(1)、先导入jstl标签库的jar包。

(2)、第二步,使用taglib指令引入标签库。

2.core核心库的使用

1、<c:set />(使用很少) 作用:set标签可以往域中保存数据

<%--<c:set />
作用:set标签可以往域中保存数据
域对象.setAttribute(key,value)
Scope属性设置保存到哪个域
    page表示PageContext域(默认值)
    request表示Request域
    session表示Session域
    application表示ServletContext域
var属性设置key是多少
value属性设置值是多少--%>
<body>
保存之前的值:${sessionScope.abc}<br>
<c:set scope="session" var="abc" value="abcValue"/>
保存之后的值:${sessionScope.abc}<br>
</body>

2、<c:if/> if标签用来做if判断

<%--if标签用来做if判断,test属性表示判断的条件,(用EL表达式进行输出)--%>
<c:if test="${12==12}">
    <h4>12等于12</h4>
</c:if>

3、<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标签
    --%>
<%
    request.setAttribute("height",130);
%>
<c:choose>
    <c:when test="${requestScope.height > 180}">
        <h2>小巨人</h2>
    </c:when>
    <c:when test="${requestScope.height > 170}">
        <h2>很高</h2>
    </c:when>
    <c:when test="${requestScope.height > 160}">
        <h2>还可以</h2>
    </c:when>
    <c:otherwise>
        <c:choose>
            <c:when test="${requestScope.height > 140}">
                <h2>大于140</h2>
            </c:when>
            <c:otherwise>
                其他小于140
            </c:otherwise>
        </c:choose>
    </c:otherwise>
</c:choose>

4、<c:forEach /> 作用:遍历输出使用 (1)、遍历1到10,输出 示例代码:

<%--遍历1到10输出
begin属性设置开始的索引
end属性设置结束的索引
var属性表示循环的变量(也是当前正在遍历到的数据)--%>
<table border="2">
<c:forEach begin="1" end="10" var="i">
  <tr>
    <td>第${i}行</td>
  </tr>
</c:forEach>
</table>

(2)、遍历Object数组 示例代码:

<%--遍历Object数组
for(Object item : arr)
item表示遍历的数据源(遍历的集合)
var表示当前遍历到的数据--%>
<%
  request.setAttribute("arr",new String[]{"18884544444","15229855465","18988884444"});
%>
<c:forEach items="${requestScope.arr}" var="item">
  ${item}<br>
</c:forEach>
<hr>

(3)、遍历Map集合 示例代码:

<%
  Map<String,Object> map = new HashMap<String,Object>();
  map.put("key1","value1");
  map.put("key2","value2");
  map.put("key3","value3");
  map.put("key4","value4");
  /*for(Map.Entry<String,Object> entry : map.entrySet()) {
  }*/
  request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
  <h1>${entry.key}=${entry.value}</h1>
</c:forEach>
<hr>

(4)、遍历List集合 Student类

public class Student {
    //编号,用户名,密码,年龄,电话信息
    private int no;
    private String name;
    private String password;
    private int age;
    private String phone;
    public Student() {}
    还有相应的get、set、构造方法、toString方法
    ....
<%--遍历List集合--list中存放Student类,
有属性:编号,用户名,密码,年龄,电话信息--%>
<style type="text/css">
  table{
    width: 500px;
    border: 1px solid red;
    border-collapse: collapse;
  }
  th,td{
    border: 1px solid red;
  }
</style>
<%
  List<Student> studentList = new ArrayList<Student>();
  for (int i = 1; i <= 10; i++) {
    studentList.add(new Student(i,"username"+i,"password"+i,18+i,"phone"+i));
  }
  request.setAttribute("student",studentList);
%>
<table>
  <tr>
    <th>编号</th>
    <th>用户名</th>
    <th>密码</th>
    <th>年龄</th>
    <th>电话</th>
    <th>操作</th>
  </tr>
<%--  items表示遍历的集合
  var表示遍历到的数据
  begin表示遍历的开始索引
  end表示结束的索引值
  step属性表示遍历的步长值
  varStatus属性表示当前遍历到的数据的状态--%>
  <c:forEach begin="0" end="10" step="1" varStatus="status" items="${requestScope.student}" var="stu">
    <tr>
      <td>${stu.no}</td>
      <td>${stu.name}</td>
      <td>${stu.password}</td>
      <td>${stu.age}</td>
      <td>${stu.phone}</td>
      <td>${status.step}</td>
    </tr>
  </c:forEach>
</table>

Status类实现了LoopTagStatus接口,以下是这个接口里面的方法