0%

EL表达式+JSTL

2017年9月11日 下午5:40

概述

  1. 目的:其实就是取代jsp,为了让页面看起来更加整齐
  2. 这里的内容基本不用记,把下面说的重点记住就行了,其他不用记
  3. 注意
    1. 区分大小写
    2. el表达式.txt
      jstl.txt

EL表达式

核心重点:

  1. EL用来代替<%=…%>
  2. EL操作四大域的内置对象:它们是Map类型,用来代替jsp的内置对象
    1. pageScope
    2. requestScope
    3. sessionScope
    4. applicationScope
  3. EL函数库
    1. <%@taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions" %>
    2. 处理字符串用
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      EL:Expression Language,它是可以在JSP页面中直接使用的语言!
      JSP页面也可以忽略EL:<@page isELIgnored="true"%>
      EL用来代替<%=...%>


      --------------------

      格式:${...},例如:${1 + 2},会在页面上输出3

      --------------------

      运算符:+ - * / % == != < > <= >= && ! || empty
      ${empty ""} --> 输出true,判断集合、数据、字符串长度是否为0

      ${null},不会输出!如果是null不输出!

      --------------------

      EL内置对象

      EL一共11个内置对象

      EL操作四大域的内置对象:它们是Map类型
      pageScope
      requestScope
      sessionScope
      applicationScope



      ${pageScope.user}:输出pageContext.getAttribute("user")
      ${requestScope.user}:输出request.getAttribute("user");
      ${sessionScope.user}:输出session.getAttribute("user");
      ${applicationScope.user}:输出application.getAttribute("user");

      ${user}
      依次在pageScope、requestScope、sessionScope、applicationScope中查找user
      如果查找到,那么立刻停止查找。

      el表达式的默认取值查找方式是全域查找 按作用域范围从小到大依次查找 找到一个对应的键值对之后立即停止查找并显示
      如果加上对应的域范围查找 那么是指定域查找 只在指定作用域查找对应的键值对 有则显示 无则显示null

      -----------


      操作JavaBean

      ${pageScope.user.username}
      ${pageScope.user.password}

      -----------

      操作List


      ${pageScope.list[0].username}
      ${pageScope.list[0].password}

      -----------

      操作Map



      ${pageScope.map.u1.username}
      ${pageScope.map.u1.password}


      ------------------

      应用初始化参数内置对象:Map类型
      initParam:Map<String,String>

      用来获取web.xml文件中的<context-param>参数,例如:

      web.xml
      <context-param>
      <param-name>p1</param-name>
      <param-value>v1</param-value>
      </context-param>

      ${initParam.p1}

      ------------------

      与Cookie相关的内置对象:Map类型
      cookie:Map<String,Cookie>,其中key是Cookie的名称,而值是Cookie对象

      ${cookie.jsessionid.value}:获取sessionid


      ------------------

      EL中最重要的就是操作四大域!


      ==========================
      ==========================
      ==========================

      EL函数库

      EL函数库,当前就是一些函数了。

      使用EL函数库需要在JSP页面中导入标签库:


      <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

      String[] strs = {"a", "b","c"};
      List list = new ArrayList();
      list.add("a");
      pageContext.setAttribute("arr", strs);
      pageContext.setAttribute("list", list);
      %>
      ${fn:length(arr) }<br/><!--3-->
      ${fn:length(list) }<br/><!--1-->
      ${fn:toLowerCase("Hello") }<br/> <!-- hello -->
      ${fn:toUpperCase("Hello") }<br/> <!-- HELLO -->
      ${fn:contains("abc", "a")}<br/><!-- true -->
      ${fn:containsIgnoreCase("abc", "Ab")}<br/><!-- true -->
      ${fn:contains(arr, "a")}<br/><!-- true -->
      ${fn:containsIgnoreCase(list, "A")}<br/><!-- true -->
      ${fn:endsWith("Hello.java", ".java")}<br/><!-- true -->
      ${fn:startsWith("Hello.java", "Hell")}<br/><!-- true -->
      ${fn:indexOf("Hello-World", "-")}<br/><!-- 5 -->
      ${fn:join(arr, ";")}<br/><!-- a;b;c -->
      ${fn:replace("Hello-World", "-", "+")}<br/><!-- Hello+World -->
      ${fn:join(fn:split("a;b;c;", ";"), "-")}<br/><!-- a-b-c -->

      ${fn:substring("0123456789", 6, 9)}<br/><!-- 678 -->
      ${fn:substring("0123456789", 5, -1)}<br/><!-- 56789 -->
      ${fn:substringAfter("Hello-World", "-")}<br/><!-- World -->
      ${fn:substringBefore("Hello-World", "-")}<br/><!-- Hello -->
      ${fn:trim(" a b c ")}<br/><!-- a b c -->
      ${fn:escapeXml("<html></html>")}<br/> <!-- <html></html> -->


      ====================

      String toUpperCase(String input):把参数转换成大写
      String toLowerCase(String input):把参数转换成小写
      int indexOf(String input, String substring):从大串,输出小串的位置!
      boolean contains(String input, String substring):查看大串中是否包含小串
      boolean containsIgnoreCase(String input, String substring):忽略大小写的,是否包含
      boolean startsWith(String input, String substring):是否以小串为前缀
      boolean endsWith(String input, String substring):是否以小串为后缀
      String substring(String input, int beginIndex, int endIndex):截取子串
      String substringAfter(String input, String substring):获取大串中,小串所在位置后面的字符串
      substringBefore(String input, String substring):获取大串中,小串所在位置前面的字符串
      String escapeXml(String input):把input中“<”、">"、"&"、"'"、""",进行转义
      String trim(String input):去除前后空格
      String replace(String input, String substringBefore, String substringAfter):替换
      String[] split(String input, String delimiters):分割字符串,得到字符串数组
      int length(Object obj):可以获取字符串、数组、各种集合的长度!
      String join(String array[], String separator):联合字符串数组

JSTL (JavaServer Pages Standard Tag Library)

重点

  1. 逻辑控制语句
    1. <%taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core"%>
    2. 这里一定更要c字母表示
  2. 格式刷format
    1. <%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt" %>
    2. 一定用fmt
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
      163
      164
      165
      166
      167
      168
      169
      170
      171
      172
      173
      174
      Apache提供的标签库,
      jar包:jstl-1.2.jar,如果MyEclipse,它会在我们导入jar包,无需自己导入,如果没有使用MyEclipse那么需要自行导入。

      ------------------

      导入JSTL核心标签库
      <%taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


      <c:set>
      * <c:set var="a" value="hello"/> 创建名为a,值为hello的域属性,范围:page
      * <c:set var="a" value="hello" scope="session"/> 范围为session

      <c:out>
      * <c:out value="aaa"/> 输出字符串aaa
      * <c:out value="${aaa}"/> 输出域属性aaa,其中与${aaa}相同
      * <c:out value="${aaa}" default="xxx"/>如果${aaa}不存在,那么输出xxx字符串
      * <c:out value="${aaa}" escapeXml="true"/>如果${aaa}中包含特殊字符,那么转义它。这可以防止javascript攻击

      <c:remove>
      * <c:remove var="a"/> 删除名为a的域属性
      * <c:remove var="a" scope="page"/> 删除page域中名为a的域属性

      <c:url>
      * <c:url value="/AServlet"/> 输出URL:/项目名/AServlet
      * <c:url value="/AServlet" var="url" scope="page"/> 把生成的url保存到page域中,而不会输出
      * <c:url value="/AServlet">:输出URL:/项目名/AServlet?username=%xx%xx%xx%xx%xx%xx,其中张三会被URL编码
      <c:param name="username" value="张三"/>
      </c:url/>

      <c:if>
      * <c:if test="${条件}"> 当条件为true时执行标签体内容
      hello
      </c:if>

      <c:choose>
      * <c:choose>
      <c:when test="${条件1}">a</c:when>
      <c:when test="${条件2}">b</c:when>
      <c:when test="${条件3}">c</c:when>
      <c:otherwise>d</c:otherwise>
      </c:choose>

      等同与:
      if() {
      } esle if() {
      } esle if() {
      } else if() {
      } else {
      }

      -------------

      <c:forEach>

      可以用来遍历数组、List、Map、

      1. 计数循环

      <c:forEach begin="1" end="10" var="i">
      ${i}
      </c:forEach>
      等同于
      for(int i = 1; i <= 10; i++) {
      out.println(i);
      }


      <c:forEach begin="1" end="10" var="i" step="2">
      ${i}
      </c:forEach>
      等同于
      for(int i = 1; i <= 10; i+=2) {
      out.println(i);
      }

      -------------

      2. 遍历数组

      <%
      String[] names = {"zhangSan", "liSi", "wangWu", "zhaoLiu"};
      pageContext.setAttribute("ns", names);
      %>
      <c:forEach var="item " items="${ns } ">
      <c:out value="name: ${item } "/><br/>
      </c:forEach>


      -------------

      3. 遍历List

      <%
      List<String> names = new ArrayList<String>();
      names.add("zhangSan");
      names.add("liSi");
      names.add("wangWu");
      names.add("zhaoLiu");
      pageContext.setAttribute("ns", names);
      %>
      <c:forEach var="item" items="${ns }">
      <c:out value="name: ${item }"/><br/>
      </c:forEach>

      -------------

      4. 遍历Map

      <%
      Map<String,String> stu = new LinkedHashMap<String,String>();
      stu.put("number", "N_1001");
      stu.put("name", "zhangSan");
      stu.put("age", "23");
      stu.put("sex", "male");
      pageContext.setAttribute("stu", stu);
      %>
      <c:forEach var="item " items="${stu }">
      <c:out value="${item.key }: ${item.value } "/><br/>
      </c:forEach>


      -------------

      5. 循环状态对象

      循环状态对象是用来说明循环的状态的,属性如下:
      count:int类型,当前已遍历元素的个数;
      index:int类型,当前元素的下标;
      first:boolean类型,是否为第一个元素;
      last:boolean类型,是否为最后一个元素;
      current:Object类型,表示当前元素。

      <c:forEach var="item" items="${ns }" varStatus="vs" >
      <c:if test="${vs.first } ">第一行:</c:if>
      <c:if test="${vs.last } ">最后一行:</c:if>
      <c:out value="第${vs.count } 行: "/>
      <c:out value="[${vs.index } ]: "/>
      <c:out value="name: ${vs.current } "/><br/>
      </c:forEach>


      ------------------

      导入JSTL格式化标签库

      format

      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

      <%
      Date date = new Date();
      pageContext.setAttribute("d", date);
      %>
      <fmt:formatDate value="${d }" pattern="yyyy-MM-dd HH:mm:ss "/>

      ---------

      <%
      double d1 = 3.5;
      double d2 = 4.4;
      pageContext.setAttribute("d1", d1);
      pageContext.setAttribute("d2", d2);
      %>
      <fmt:formatNumber value="${d1 }" pattern="0.00 "/><br/>
      <fmt:formatNumber value="${d2 }" pattern="#.## "/>

      * pattern:0.00,表示小数不足两位时,使用0补足两位
      * pattern:#.##,表示小数不足两位时,有几位显示几位,不会补足


      ============================
      ============================
      ============================