1 EL入門
1、EL是JSP內置的表達式語言。
(1)JSP 2.0開始,不讓再使用java腳本,而是使用EL表達式和動態標籤來替代Java腳本。
(2)EL代替的是<%= ... %>
,也就是說EL只能做輸出。
2、EL表達式來讀取四大域
(1)${xxx}
:全域查找名爲xxx的屬性,如果不存在,則輸出空字符串,而不是null。
(2)${pageScope.xxx}
、${requestScope.xxx}
、${sessionScope.xxx}
、${applicationScope.xxx}
,指定域獲取屬性。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("page", "pageContext");
request.setAttribute("page", "request");
session.setAttribute("page", "session");
application.setAttribute("page", "application");
%>
${page }<br/> <!-- pageContext -->
${pageScope.page }<br/> <!-- pageContext -->
${requestScope.page }<br/> <!-- request -->
${sessionScope.page }<br/> <!-- session -->
${applicationScope.page }<br/> <!-- application -->
</body>
</html>
3、JavaBean導航
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.lks.domain.Person,org.lks.domain.Address" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Person per = new Person("lks", 23, new Address("xinyang","luoshan"));
session.setAttribute("personA", per);
%>
${sessionScope.personA.address.city } <!-- xinyang -->
</body>
</html>
2 EL的11個內置對象
EL可以輸出的東西都在11個內置對象中,其中10個是Map。pageContext不是Map,它就是一個PageContext類型。
(1)param:Map<String,String>類型,對應參數,其中key是參數名,value是參數值,適用於單值的參數。
(2)paramValues:Map<String,String[]>類型,對應參數,其中key是參數名,value是多個參數值,適用於多值的參數。
(3)header:對應請求頭,它是一個Map,其中Key表示頭名稱,value是單個頭值,適用於單值請求頭。
(4)headerValues:對應請求頭,它是一個Map,其中Key表示頭名稱,value是多個頭值,適用於多值請求頭。
(5)initParam:獲取<context-param>
內的參數。
<context-param>
<param-name>info</param-name>
<param-value>hello</param-value>
</context-param>
(6)cookie:Map<String,Cookie>類型,其中key是Cookie的name,value是Cookie對象。
(7)pageContext:它是PageContext類型。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- http://localhost:8080/jsplearn/jsp03/a.jsp?info=hello -->
${param.info }<br/> <!-- hello -->
<!-- http://localhost:8080/jsplearn/jsp03/a.jsp?hobby=swimming&hobby=play computer game -->
${paramValues.hobby[0] } <br/> <!-- swimming -->
${paramValues.hobby[1] } <br/> <!-- play computer game -->
${header['User-Agent'] } <br/> <!-- Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0 -->
${initParam.info }<br/> <!-- hello -->
${cookie.JSESSIONID.value }<br/> <!-- 8EE8FA6AC2A02B1879AB13349A3BB63B -->
</body>
</html>
EL操作Map有兩種語法:
(1)map.key
(2)map[‘key’]
3 EL函數庫
EL函數庫是由JSTL提供的,但是JSTL並不能直接使用,使用JSTL需要先導入標籤庫。導入標籤庫的語法如下:
<%@ taglib prefix="fn" uri="http:java.sun.com/jsp/jstl/functions" %>
fn:contains():測試輸入的字符串是否包含指定的子串
fn:containsIgnoreCase():測試輸入的字符串是否包含指定的子串,大小寫不敏感
fn:endsWith():測試輸入的字符串是否以指定的後綴結尾
fn:escapeXml():跳過可以作爲XML標記的字符
fn:indexOf():返回指定字符串在輸入字符串中出現的位置
fn:join():將數組中的元素合成一個字符串然後輸出
fn:length():返回字符串長度
fn:replace():將輸入字符串中指定的位置替換爲指定的字符串然後返回
fn:split():將字符串用指定的分隔符分隔然後組成一個子字符串數組並返回
fn:startsWith():測試輸入字符串是否以指定的前綴開始
fn:substring():返回字符串的子集
fn:substringAfter():返回字符串在指定子串之後的子集
fn:substringBefore():返回字符串在指定子串之前的子集
fn:toLowerCase():將字符串中的字符轉爲小寫
fn:toUpperCase():將字符串中的字符轉爲大寫
fn:trim():移除首尾的空白符
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${fn.toUpperCase("love") }<br/> <!-- LOVE -->
${fn.toLowerCase("LOVE") }<br/> <!-- love -->
${fn.contains("hello", "lo") }<br/> <!-- true -->
${fn.containsIgnoreCase("HELLO", "lo") }<br/> <!-- true -->
${fn.endsWith("Hello", "lo") }<br/> <!-- true -->
${fn.startsWith("HEllo", "HE") }<br/> <!-- true -->
</body>
</html>
4 EL自定義函數庫
使用自定義函數庫的步驟:
(1)寫一個Java類,類中可以定義0-N個方法,但必須是static的,而且必須有返回值。
public static String fun() {
return "hello";
}
(2)在WEB-INF目錄下創建一個tld文件
<?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">
<description>lks</description>
<display-name>lks</display-name>
<tlib-version>1.0</tlib-version>
<short-name>hy</short-name>
<uri>http://lks.org/hello/kls</uri>
<function>
<name>fun</name>
<function-class>org.lks.test.Test</function-class>
<function-signature>java.lang.String fun()</function-signature>
</function>
</taglib>
(3)在JSP頁面導入標籤庫
(4)在JSP頁面中使用自定義的函數
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="hy" uri="/WEB-INF/tlds/myfun.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${hy.fun()}
</body>
</html>