1. 程式人生 > >EL(表示式語言)

EL(表示式語言)

1EL概述

1.1EL的作用

JSP2.0要把htmlcss分離、要把htmljavascript分離、要把Java指令碼替換成標籤。標籤的好處是非Java人員都可以使用。

JSP2.0 – 純標籤頁面,即:不包含<% … %><%! … %>,以及<%= … %>

ELExpression Language)是一門表示式語言,它對應<%=…%>。我們知道在JSP中,表示式會被輸出,所以EL表示式也會被輸出。

1.2EL的格式

格式:${…}

例如:${1 + 2}

1.3 關閉EL

如果希望整個JSP忽略EL表示式,需要在page指令中指定

isELIgnored=”true”。

如果希望忽略某個EL表示式,可以在EL表示式之前新增“\”,例如:\${1 + 2}

1.4EL運算子

運算子

說明

範例

結果

+

${17+5}

22

-

${17-5}

12

*

${17*5}

85

/div

${17/5}${17 div 5}

3

%mod

取餘

${17%5}${17 mod 5}

2

==eq

等於

${5==5}${5 eq 5}

true

!=ne

不等於

${5!=5}

${5 ne 5}

false

<lt

小於

${3<5}${3 lt 5}

true

>gt

大於

${3>5}${3 gt 5}

false 

<=le

小於等於

${3<=5}${3 le 5}

true 

>=ge

大於等於

${3>=5}${3 ge 5}

false 

&&and

並且

${true&&false}${true and false}

false 

!not

${!true}

${not true}

false

||or

或者

${true||false}${true or false}

true

empty

是否為空

${empty “”},可以判斷字串、資料、集合的長度是否為0,為0返回trueempty還可以與not!一起使用。${not empty “”}

true

1.5EL不顯示null

  當EL表示式的值為null時,會在頁面上顯示空白,即什麼都不顯示。

2EL表示式格式

先來了解一下EL表示式的格式!現在還不能演示它,因為需要學習了EL11個內建物件後才方便顯示它。

l 操作List和陣列:${list[0]}${arr[0]}

l 操作bean的屬性:${person.name}${person[‘name’]},對應person.getName()方法;

l 操作Map的值:${map.key}${map[‘key’]},對應map.get(key)

3EL內建物件

EL一共11個內建物件,無需建立即可以使用。這11個內建物件中有10個是Map型別的,最後一個是pageContext物件。

l pageScope

l requestScope

l sessionScope

l applicationScope

l param

l paramValues

l header

l headerValues

l initParam

l cookie

l pageContext

3.1 域相關內建物件(重點)

域內建物件一共有四個:

l pageScope${pageScope.name}等同與pageContext.getAttribute(“name”)

l requestScope${requestScope.name}等同與request.getAttribute(“name”)

l sessionScoep: ${sessionScope.name}等同與session.getAttribute(“name”)

l applicationScope${applicationScope.name}等同與application.getAttribute(“name”)

如果在域中儲存的是JavaBean物件,那麼可以使用EL來訪問JavaBean屬性。因為EL只做讀取操作,所以JavaBean一定要提供get方法,而set方法沒有要求。

Person.java

public class Person {
private String name;
private int age;
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}

 

  全域查詢:${person}表示依次在pageScoperequesScopetsessionScopeappliationScope四個域中查詢名字為person的屬性。

3.2 請求引數相關內建物件

  paramparamValues這兩個內建物件是用來獲取請求引數的。

l paramMap<String,String>型別,param物件可以用來獲取引數,與request.getParameter()方法相同。

注意,在使用EL獲取引數時,如果引數不存在,返回的是空字串,而不是null。這一點與使用request.getParameter()方法是不同的。

l paramValuesparamValuesMap<String, String[]>型別,當一個引數名,對應多個引數值時可以使用它。

3.3 請求頭相關內建物件

headerheaderValues是與請求頭相關的內建物件:

l header: Map<String,String>型別,用來獲取請求頭。

l headerValuesheaderValuesMap<String,String[]>型別。當一個請求頭名稱,對應多個值時,使用該物件,這裡就不在贅述。

3.4 應用初始化引數相關內建物件

l initParaminitParamMap<String,String>型別。它對應web.xml檔案中的<context-param>引數。

3.5Cookie相關內建物件

l cookiecookieMap<String,Cookie>型別,其中keyCookie的名字,而值是Cookie物件本身。

3.6pageContext物件

pageContextpageContextPageContext型別!可以使用pageContext物件呼叫getXXX()方法,例如pageContext.getRequest(),可以${pageContext.request}。也就是讀取JavaBean屬性!!!

EL表示式

說明

${pageContext.request.queryString}

pageContext.getRequest().getQueryString();

${pageContext.request.requestURL}

pageContext.getRequest().getRequestURL();

${pageContext.request.contextPath}

pageContext.getRequest().getContextPath();

${pageContext.request.method}

pageContext.getRequest().getMethod();

${pageContext.request.protocol}

pageContext.getRequest().getProtocol();

${pageContext.request.remoteUser}

pageContext.getRequest().getRemoteUser();

${pageContext.request.remoteAddr}

pageContext.getRequest().getRemoteAddr();

${pageContext.session.new}

pageContext.getSession().isNew();

${pageContext.session.id}

pageContext.getSession().getId();

${pageContext.servletContext.serverInfo}

pageContext.getServletContext().getServerInfo();

EL函式庫

1 什麼EL函式庫

  EL函式庫是由第三方對EL的擴充套件,我們現在學習的EL函式庫是由JSTL新增的。JSTL明天再學!

EL函式庫就是定義一些有返回值的靜態方法。然後通過EL語言來呼叫它們!當然,不只是JSTL可以定義EL函式庫,我們也可以自定義EL函式庫。

  EL函式庫中包含了很多對字串的操作方法,以及對集合物件的操作。例如:${fn:length(“abc”)}會輸出3,即字串的長度。

2 匯入函式庫

  因為是第三方的東西,所以需要匯入。匯入需要使用taglib指令!

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

3EL函式庫介紹

l String toUpperCase(String input):

l String toLowerCase(String input):

l int indexOf(String input, String substring):

l boolean contains(String input, String substring):

l boolean containsIgnoreCase(String input, String substring):

l boolean startsWith(String input, String substring):

l boolean endsWith(String input, String substring):

l String substring(String input, int beginIndex, int endIndex):

l String substringAfter(String input, String substring):hello-world, “-“

l substringBefore(String input, String substring):hello-world, “-“

l String escapeXml(String input):把字串的“>”、“<”。。。轉義了!

l String trim(String input):

l String replace(String input, String substringBefore, String substringAfter):

l String[] split(String input, String delimiters):

l int length(Object obj):可以獲取字串、陣列、各種集合的長度!

l String join(String array[], String separator):

<%@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> -->

 
4 自定義EL函式庫

l 寫一個類,寫一個有返回值的靜態方法;

l 編寫itcast.tld檔案,可以引數fn.tld檔案來寫,把itcast.tld檔案放到/WEB-INF目錄下;

l 在頁面中新增taglib指令,匯入自定義標籤庫。

ItcastFuncations.java

package cn.itcast.el.funcations;
 
public class ItcastFuncations {
public static String test() {
return "傳智播客自定義EL函式庫測試";
}
}

 

itcast.tld(放到classes下)

<?xml version="1.0" encoding="UTF-8" ?>
 
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
    
  <tlib-version>1.0</tlib-version>
  <short-name>itcast</short-name>
  <uri>http://www.itcast.cn/jsp/functions</uri>
 
  <function>
    <name>test</name>
    <function-class>cn.itcast.el.funcations.ItcastFuncations</function-class>
    <function-signature>String test()</function-signature>
  </function>
</taglib>

 

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="itcast" uri="/WEB-INF/itcast.tld" %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body> 
  	<h1>${itcast:test() }</h1>
  </body>
</html>