El表達式

EL隱含對象:html

一、與範圍有關的隱含對象java

applicationScopeweb

sessionScopecookie

requestScopesession

pageScopeapp

二、與輸入有關的隱含對象ui

paramcode

paramValueshtm

三、其它隱含對象對象

cookie

header

headerValues

initParam

pageContext

 

使用方法:

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.atguigu.javaweb.Customer"%>
<%@ 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>

	<!-- 7. EL 的運算符 -->
	${param.score > 60 ? "及格" : "不及格" }
	<br>
	
	<% 
		List<String> names = new ArrayList<String>();
		names.add("abc");
		request.setAttribute("names", names);
	%>
	<!-- empty 能夠做用於一個集合, 若該集合不存在或集合中沒有元素, 其結果都爲 true -->
	names is empty: ${empty requestScope.names }
	<br>
	
	<!-- 6. 其餘隱含對象: pageContext 等(cookie, header, initParam 只需瞭解.) -->
	pageContext: pageContext 即爲 PageContext 類型, 但只能讀取屬性就能夠一直的 . 下去。 
	<br>
	contextPath: ${pageContext.request.contextPath }
	
	<br>
	sessionId: ${pageContext.session.id }
	
	<br>
	sessionAttributeNames: ${pageContext.session.attributeNames }
	
	<br>
	
	
	initParam: ${initParam.initName }
	<br>
	
	Accept-Language: ${header["Accept-Language"] }
	<br>

	JSESSIONID: ${cookie.JSESSIONID.name } -- ${cookie.JSESSIONID.value }
	<br>

	<!-- 5. 與輸入有關的隱含對象: param, paramValues -->
	score: ${param.score }
	<%-- 
	<%= request.getParameter("score") %>
	--%>
	<br>
	names: ${paramValues.name[0].class.name }
	<%-- 
	<%= 
		request.getParameterValues("name")[0].getClass().getName()
	%>
	--%>
	<br>
	
	<!-- 4. 隱含對象之與範圍相關的: pageScope, requestScope, sessionScope, applicationScope -->
	time: ${applicationScope.time.time }
	<%-- 
	<%= application.getAttribute("time") %>
	--%>
	<br>
	
	<!-- 3. EL 能夠進行自動的類型轉換 -->
	score: ${param.score + 11}
	<br>
	score: <%= request.getParameter("score") + 11 %>
	<br>
	
	<!-- 2. EL 中的隱含對象 -->
	<% 
		Customer cust2 = new Customer();
		cust2.setAge(28);
		request.setAttribute("customer", cust2);
	%>
	
	age: ${customer.age } 

	<br>
	<!-- 1. EL 的 . 或 [] 運算符 -->
	age: ${sessionScope.customer["age"] }
	
	<%-- 
		Customer customer = (Customer)session.getAttribute("customer");
		out.print(customer.getAge());
	--%>
	
	<% 
		Customer customer = new Customer();
		customer.setName("com.qimeng");	
	
		session.setAttribute("com.qimeng", customer);
	%>
	
	<br>
	<!--  
		若是域對象中的屬性名帶有特殊字符, 則使用 [] 運算符會很方便. 
	-->
	name: ${sessionScope["com.atguigu.customer"].name }
	
</body>
</html>
相關文章
相關標籤/搜索