1. EL表达式

1.1 概念

Expression Language表达式语言

1.1 作用

简化JSP中书写Java代码的代码量

1.3 语法

${表达式}

1.4 注意

jsp默认是支持el表达式,如果要忽略el表达式,则可以使用以下两种方法:

  1. 设置jsp中page指令,isELIgnored=“true”,忽略当前页面中所有的el表达式
  2. \${表达式} 忽略当前这个el表达式

1.5 使用

  1. 运算:
    1. 算数运算符:+ - * /(div) %(mod)

    2. 比较运算符:> < >= <= != ==

    3. 逻辑运算符:&& (and) || (or) ! (not)

    4. 空运算符:empty

      1. 功能:用于判断字符串,集合,数组对象是否为null或长度是否为0
      2. ${empty list}
      <%
          List list = new ArrayList();
          list.add("aaa");
          request.setAttribute("list",list);
      %>
      
      <%
          String str = "";
          request.setAttribute("str",str);
      %>
      ${
             empty list}<br>//如果list为空时输出true,反之false
      ${
             not empty list}<br>//如果list不为空时输出true,反之false
      ${
             not empty str}<br> //“”属于长度=0,但是不为空
      
      <%--
        Created by IntelliJ IDEA.
        User: lzy
        Date: 2020/3/6
        Time: 17:48
        To change this template use File | Settings | File Templates.
      --%>
      <%@ page contentType="text/html;charset=UTF-8" language="java"  %>
      <html>
      <head>
          <title>Title</title>
      </head>
      <body>
      <h3>算术运算符</h3>
      ${
             3+4}<br>
      ${
             3%4}<br>
      ${
             3 div 4}<br>	
      <h3>比较运算符</h3>
      ${
             5 > 6}<br>
      ${
             5 == 6}<br>
      <h3>逻辑运算符</h3>
      ${
             3 > 5 && 3>2}<br>
      ${
             3 > 5 or 3>2}<br>
      </body>
      </html>
      

1.6 获取值

  1. EL表达式只能从域对象中获取值

  2. 语法:

    1. ${域名称.键名}:从指定域名称中获取指定键的值
      域名称(由上至下 域的范围变大):

      1. pageScope - - - - ->pageContext
      2. requestScope - - - - >request
      3. sessionScope - - - - ->session
      4. applicationScope - - - - >application(ServletContext)
    2. ${键名}:表示依次从最小域中查找是否又该键对应的值,知道找到为止

      举例1:在request域中存储了	name = liuzeyu
      获取:${
             requstScope.name}
      
      举例2:在application域,request域中,pageContext域中分别存储了name 的值
      <%
          application.setAttribute("name","lisi");
          request.setAttribute("name","zhangsan");
          pageContext.setAttribute("name","wangwu");
      %>
      ${
             name}
      最后输出:wangwu
      验证了依次从最小域中查找是否又该键对应的值,知道找到为止
      
    3. 获取实体类对象,List集合,Map的值

      1. 对象:${域名称.键名.属性名},本质上会去调用getter方法
      2. List集合:${域名称.键名[索引]}
      3. Map集合:
        ${域名称.键名.key名称}
        ${域名称.键名[“key名称”]}

    User.java

    public class User {
         
    		
    		    private String username;
    		    private String password;
    		    private Date birthday;
    		    public String getUsername() {
         
    		        return username;
    		    }
    		
    		    public void setUsername(String username) {
         
    		        this.username = username;
    		    }
    		
    		    public String getPassword() {
         
    		        return password;
    		    }
    		
    		    public void setPassword(String password) {
         
    		        this.password = password;
    		    }
    		
    		    public Date getBirthday() {
         
    		        return birthday;
    		    }
    		
    		    public String getBirthdayFormat() {
         
    		        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    		        String format = sdf.format(birthday);
    		        return format;
    		    }
    		
    		    public void setBirthday(Date birthday) {
         
    		        this.birthday = birthday;
    		    }
    		}
    

    el3.jsp

    <%@ page import="domain.User" %>
    <%@ page import="java.util.*" %><%--
      Created by IntelliJ IDEA.
      User: lzy
      Date: 2020/3/6
      Time: 17:48
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java"  %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
    <%--1.测试el表达式获取对象得值--%>
    <%
        User user = new User();
        user.setUsername("liuzeyu");
        user.setPassword("809080");
        user.setBirthday(new Date());
        request.setAttribute("u",user);
    %>
    <%--2.测试el表达式获取List对象得值--%>
    <%
        List list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add(user);
        request.setAttribute("list",list);
    %>
    <%--2.测试el表达式获取Map对象的值--%>
    <%
        Map map = new HashMap();
        map.put("sname","liuzeyu");
        map.put("gender",32);
        map.put("user",user);
        request.setAttribute("map",map);
    %>
    <%-- ##############################################--%>
    <h3>el表达式获取对象的属性</h3>
    <%--通过对象的【属性】来获取
        如:获取username,找到getUsername,找到不到为空--%>
    ${
         requestScope.u.username}<br>
    ${
         requestScope.u.password}<br>
    ${
         requestScope.u.birthday}<br>
    ${
         requestScope.u.birthday.year}<br>
    ${
         requestScope.u.birthday.month}<br>
    ${
         requestScope.u.birthdayFormat}<br>
    
    <h3>el表达式获取List对象的内容</h3>
    ${
         requestScope.list}<br>
    ${
         requestScope.list[0]}<br>
    ${
         requestScope.list[2].username}<br>
    
    <h3>el表达式获取Map对象的内容</h3>
    ${
         requestScope.map}<br>
    ${
         requestScope.map["gender"]}<br>
    ${
         requestScope.map.sname}<br>
    ${
         requestScope.map.user.username}<br>
    </body>
    </html>
    

1.7 隐式对象

  • el表达式中有11个隐式对象
    • pageContext:
      • 获取jsp其他八个内置对象
        • ${pageContext.request.contextPath}:动态获取虚拟目录,常用于表单的action

    el表达式隐式对象:https://blog.csdn.net/hjl815/article/details/71123545

2. JSTL标签

2.1 概念

Java Server Tag Library
是由appach组织提供的免费开源jsp标签

2.2 作用

用于简化和替换jsp页面上的Java代码

2.3 使用步骤

  1. 导入jstl相关的jar包
  2. 引入标签库:taglib指令:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. 使用标签
  4. 常用的jstl标签
    1. if:相对于java代码的if语句

      1. 属性:
        test必须属性,接受boolean值
        1. 如果表达式为true,则显示if标签体内容,反之则不显示
        2. 一般情况下,test属性会和el表达式配合使用

      注意:c:if标签没有else情况,想要else情况,则可以再定义一个c:if标签

      <%--
          需求:
          1.判断集合是否为空,如果不为空,则显示if标签体内容
          2.结合el表达式判断是否奇数
      --%>
      <%
          List list = new ArrayList();
          list.add("liuzeyu");
          request.setAttribute("list",list);
      
          request.setAttribute("num",31);
      %>
      
      <c:if test="${not empty list}">list不为空  可以遍历</c:if><br>
      <c:if test="${requestScope.num mod 2 == 0}">${
             requestScope.num}是偶数</c:if><br>
      <c:if test="${requestScope.num mod 2 != 0}">${
             requestScope.num}是奇数</c:if><br>
      
    2. chose:相当于Java代码的switch语句

      1. 使用chose标签取出数字 相当于switch
      2. 使用when标签做数字判断 相当于case
      3. 使用otherwise 做出其他情况声明 相当于default
      <%--
          需求:
          完成数字编号对应星期几的案例
          1.域中存储一数字
          2.使用chose标签取出数字     相当于switch
          3.使用when标签做数字判断    相当于case
          4.使用otherwise 做出其他情况声明  相当于default
      --%>
      
      <%
          request.setAttribute("number",33);
      %>
      
      <c:choose>
          <c:when test="${requestScope.number == 1}">星期一</c:when>
          <c:when test="${requestScope.number == 2}">星期二</c:when>
          <c:when test="${requestScope.number == 3}">星期三</c:when>
          <c:when test="${requestScope.number == 4}">星期四</c:when>
          <c:when test="${requestScope.number == 5}">星期五</c:when>
          <c:when test="${requestScope.number == 6}">星期六</c:when>
          <c:when test="${requestScope.number == 7}">星期日</c:when>
      
          <c:otherwise>请输入正确的数字!!</c:otherwise>
      </c:choose>
      	```
      
    3. foreach:相当于Java代码的for语句

      1. 完成重复操作
        for(int i = 0;i < 10; i++){
        //操作
        }
        *属性:
        begin:开始值
        end:结束值
        step:步长
        var:临时变量
        varStatus:循环状态对象
        index:容器中的元素索引,从0开始
        count:循环次数,从1开始

      2. 遍历容器
        List users;
        for(User user : users){
        //操作
        }
        *属性
        item:容器对象
        var:容器元素的临时变量
        varStatus:循环状态对象
        index:容器中的元素索引,从0开始
        count:循环次数,从1开始

      <%@ page import="java.util.List" %>
      <%@ page import="java.util.ArrayList" %><%--
        Created by IntelliJ IDEA.
        User: lzy
        Date: 2020/3/6
        Time: 21:55
        To change this template use File | Settings | File Templates.
      --%>
      <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <html>
      <head>
          <title>测试jstl的if</title>
      </head>
      <body>
      
      <%-- 1.完成重复操作--%>
      <c:forEach begin="0" end="5" step="1" var="i" varStatus="s">
          ${
             i}<h1>${
             s.index}</h1><h2>${
             s.count}</h2><br>
      </c:forEach>
      
      <hr>
      <%-- 2.遍历容器--%>
      <%
          List list = new ArrayList();
          list.add("aaa");
          list.add("bbb");
          list.add("ccc");
          list.add("ddd");
          request.setAttribute("list",list);
      %>
      <c:forEach items="${list}" var="str" varStatus="s">
          ${
             str}<h1>${
             s.index}</h1><h2>${
             s.count}</h2><br>
      </c:forEach>
      </body>
      </html>
      
      

2.4 案例

需求:在request域中存有一个User对象的List集合,需要使用jstl+el将List集合数据展示到jsp页面的table表格中

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Date" %>
<%@ page import="domain.User" %><%--
  Created by IntelliJ IDEA.
  User: lzy
  Date: 2020/3/6
  Time: 21:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>案例</title>
</head>
<body>
<%
    List list = new ArrayList();
    list.add(new User("zhangsan","1998",new Date()));
    list.add(new User("lisi","2001",new Date()));
    list.add(new User("wangwu","2520",new Date()));
    list.add(new User("xiaohua","1530",new Date()));
    list.add(new User("taizi","5540",new Date()));

    request.setAttribute("list",list);
%>

<table border="1" width="500" align="center">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>出生年份</th>
        <th>生日</th>
    </tr>
    <c:forEach items="${requestScope.list}" var="user" varStatus="s">

        <c:if test="${s.count % 2 != 0}">
        <tr bgcolor="red">
            <td>${
   s.count}</td>
            <td>${
   user.username}</td>
            <td>${
   user.password}</td>
            <td>${
   user.birthdayFormat}</td>
        </tr>
        </c:if>

        <c:if test="${s.count % 2 == 0}">
            <tr bgcolor="green">
                <td>${
   s.count}</td>
                <td>${
   user.username}</td>
                <td>${
   user.password}</td>
                <td>${
   user.birthdayFormat}</td>
            </tr>
        </c:if>
    </c:forEach>
</table>
</body>
</html>

实现结果:

注意:

test="${s.count % 2 == 0}"不可以写成test="${s.count % 2 == 0} "
多出一个空格,
老眼昏花,一个空格排错半天。