1. 程式人生 > >javaweb:EL表示式

javaweb:EL表示式

EL表示式簡介:

EL 全名為Expression Language

EL主要作用:

獲取資料:

EL表示式主要用於替換JSP頁面中的指令碼表示式,以從各種型別的web域 中檢索java物件、獲取資料。(某個web域 中的物件,訪問javabean的屬性、訪問list集合、訪問map集合、訪問陣列)

執行運算:

利用EL表示式可以在JSP頁面中執行一些基本的關係運算、邏輯運算和算術運算,以在JSP頁面中完成一些簡單的邏輯運算。${user==null}

獲取web開發常用物件

EL 表示式定義了一些隱式物件,利用這些隱式物件,web開發人員可以很輕鬆獲得對

web常用物件的引用,從而獲得這些物件中的資料。

呼叫Java方法

EL表示式允許使用者開發自定義EL函式,以在JSP頁面中通過EL表示式呼叫Java類的方法。

EL注意事項:

EL表示式是JSP 2.0(JavaEE1.4)規範中的一門技術 。因此,若想正確解析EL表示式,需使用支援Servlet2.4/JSP2.0技術的WEB伺服器。

l注意:有些Tomcat伺服器如不能使用EL表示式

  1)升級成tomcat6

  2)在JSP中加入<%@ page isELIgnored="false" %>

獲取資料(1):

l使用

EL表示式獲取資料語法:“${識別符號}”

lEL表示式語句在執行時,會呼叫pageContext.findAttribute方法,用識別符號為關鍵字,分別從pagerequestsessionapplication四個域中查詢相應的物件,找到則返回相應物件,找不到則返回”” (注意,不是null,而是空字串)。

l示例:${user}

獲取資料(2):

lEL表示式也可以很輕鬆獲取JavaBean的屬性,或獲取陣列、CollectionMap型別集合的資料,例如:

${user.address.city}

${user.list[0]}:訪問有序集合某個位置

的元素

${map.key: 獲得map集合中指定key的值

l. [ ] 區別

l結合JSTLforeach標籤,使用EL表示式也可以很輕鬆迭代各種型別的陣列或集合,示例:

迭代陣列

迭代collection型別集合

迭代map型別集合

執行運算:

l語法:

 

EL語法

${  EL表示式 } : 注意${間不要有空格。

EL存取變數:${ a },取出某一範圍中名稱為a的變數,預設會按pagerequestsessionapplication的順序查詢。

<%

  request.setAttribute("str", str);

%>

${ str }

${ RequestScope.str }

${運算表示式}EL表示式支援如下運算子:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el支援運算</title>
</head>
<body>

<%
	request.setAttribute("n1", 10);
	request.setAttribute("n2", 20);
	request.setAttribute("n3", 30);
	request.setAttribute("n4", 40);
%>
<h4>EL表示式支援運算</h4>
${ n1 + n2 }

<h4>是否相等</h4>
${ n1 == n2 }  ${ n1 eq n2 }

<h4>是否不等</h4>
${ n1 != n2 } ${ n1 ne n2 }

<h4>大於或者小於</h4>
${ n1 > n2 } ${ n1 gt n2 }  ${ n1 < n2 } ${ n1 lt n2 }

<h4>大於等於或者小於等於</h4>
${ n1 >= n2 } ${ n1 ge n2 }  ${ n1 <= n2 } ${ n1 le n2 }

<h4>與或非</h4>
${ n1 > n2 && n3 > n4 } ${ n1>n2 and n3>n4 }
${ n1 > n2 || n3 > n4 } ${ n1>n2 or n3>n4 }
${ !(n1>n2) }	${ not(n1>n2) }

</body>
</html>


EL表示式保留關鍵字:

 l所謂保留字的意思是指變數在命名時,應該避開上述的名字,以免程式編譯時發生錯誤

獲得web開發常用物件:

lEL表示式語言中定義了11個隱含物件,使用這些隱含物件可以很方便地獲取web開發中的一些常見物件,並讀取這些物件的資料。

l語法:${隱式物件名稱:獲得物件的引用

隱含物件名稱

描       述

pageContext

對應於JSP頁面中的pageContext物件(注意:取的是pageContext物件。)

pageScope

代表page域中用於儲存屬性的Map物件

requestScope

代表request域中用於儲存屬性的Map物件

sessionScope

代表session域中用於儲存屬性的Map物件

applicationScope

代表application域中用於儲存屬性的Map物件

隱含物件名稱

描       述

param

表示一個儲存了所有請求引數的Map物件

paramValues

 

表示一個儲存了所有請求引數的Map物件,它對於某個請求引數,返回的是一個string[]

header

表示一個儲存了所有http請求頭欄位的Map物件

headerValues

 

同上,返回string[]陣列。注意:如果頭裡面有- ,例Accept-Encoding,則要headerValues["Accept-Encodings"]

cookie

表示一個儲存了所有cookieMap物件

initParam

表示一個儲存了所有web應用初始化引數的map物件

 

測試各個隱式物件

demo1.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="/day13/jsp/eldemo3.jsp" method="post">
	使用者名稱:<input type="text" name="username" /><br/>
	<input type="submit" value="提交"/>
</form>

</body>
</html>


demo2.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h4>傳統方式</h4>
<%= request.getParameter("username") %>

<h4>使用EL表示式內建物件獲取請求引數</h4>
${ param.username }
${ paramValues.username[0] }


<h4>使用EL表示式的內建物件獲取請求頭的資訊</h4>
${ header.referer }
${ headerValues.referer[0] }
${ header["user-agent"] }

<h4>獲取全域性初始化引數的值</h4>
${ initParam.encoding }


<h4>使用傳統方式獲取cookie中的值</h4>
<%
	Cookie [] cookies = request.getCookies();
	for(Cookie c : cookies){
		if(c.getName().equals("last")){
			String value = c.getValue();
%>			
		<font color="blue"><%= value %></font>
<% 		
		}	
	}
%>

<h4>使用EL表示式來獲取內容</h4>
${ cookie.last.name }  -- ${ cookie.last.value }
<font color="red">${ cookie.last.value }</font>

<form action="">
	<input type="text" name="username" value="${ cookie.last.value }" />
</form>


<h4>EL表示式中的pageContext物件</h4>
${ pageContext.request.contextPath }
${ pageContext.request.remoteAddr }

<form action="${ pageContext.request.contextPath }/xxx" method="post">
	<input type="text" name="username"  />
</form>

</body>
</html>


 

注意事項

測試headerValues時,如果頭裡面有- ,例Accept-Encoding,則要headerValues[“Accept-Encoding”]

測試cookie時,例${cookie.key}取的是cookie物件,如訪問cookie的名稱和值,須${cookie.key.name}${cookie.key.value}

EL表示式獲取集合的值:

el1.jsp

<%@page import="cn.itcast.domain.Person"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	pageContext.setAttribute("username", "美美");
	request.setAttribute("username", "張默");
	session.setAttribute("username", "烏鴉");
	application.setAttribute("username", "李代沫");
%>

<h4>EL獲取域物件中的值</h4>
${ pageScope.username }
${ requestScope.username }
${ sesssionScope.username }
${ applicationScope.username }


<h4>從陣列中取值(把陣列存到域物件中)</h4>
<%
	String [] arrs = new String[]{"美美","張默","房祖名"};
	// 先把陣列存到域中
	pageContext.setAttribute("arrs", arrs);
%>
${ arrs[2] }

<h4>從List集合中取值(域物件中)</h4>
<%
	List<String> list = new ArrayList<String>();
	list.add("美美");
	list.add("張默");
	list.add("房祖名");
	pageContext.setAttribute("list", list);
%>
${ list[1] }

<h4>從Map集合中取值</h4>
<%
	Map<String,String> map = new HashMap<String,String>();
	map.put("aaa", "美美");
	map.put("bbb", "張默");
	map.put("ccc-ddd", "房祖名");
	pageContext.setAttribute("map", map);
%>
${ map["ccc-ddd"] }


<h4>從List存入物件集合中取值(域物件中)</h4>
<%
	List<Person> ulist = new ArrayList<Person>();
	ulist.add(new Person("美美","123"));
	ulist.add(new Person("張默","456"));
	ulist.add(new Person("祖明","789"));
	pageContext.setAttribute("ulist", ulist);
%>
${ ulist[2].username }


</body>
</html>

Person.java:

package cn.itcast.domain;

public class Person {
	
	private String username;
	private String password;
	
	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 Person(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public Person() {
		super();
	}
	public String toString() {
		return "Person [username=" + username + ", password=" + password + "]";
	}

}