Java Web 1 -- Chapter 8 EL expressions and JSTL tag libraries

1, EL expression

1. Definition and function

  • The full name of EL expression is Expression Language. Is an Expression Language.
  • What is the function of EL expression: EL expression mainly replaces the expression script in jsp page to output data in jsp page. Because EL expressions are much more concise than jsp expression scripts when outputting data.
	Expression script output key The values are: <%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%><br/> 
	EL Expression output key The values are: ${key1} 
  • The format of EL expression is ${expression}
  • When an EL expression outputs a null value, it outputs an empty string. When the jsp expression script outputs a null value, it outputs a null string.

2. The order in which EL expressions search for domain data

  • EL expressions mainly output data in jsp pages.
  • It mainly outputs the data in the domain object.

When there is the same key data in the four fields, the EL expression will search in the order of the four fields from small to large, and output it when found.

	//The data of the same key is saved in all four fields. 
	request.setAttribute("key", "request");
	session.setAttribute("key", "session"); 		
	application.setAttribute("key", "application"); 
	pageContext.setAttribute("key", "pageContext"); 
	${ key } 

3. EL expression output properties

Bean's common attribute, array attribute. List collection attribute, map collection attribute

i. Requirements - output common attributes and array attributes in the Person class. list collection attribute and map collection attribute.

Person class

public class Person { 
// i. Requirements - output common attributes and array attributes in the Person class. list collection attribute and map collection attribute. 
	private String name; 
	private String[] phones; 
	private List<String> cities; 
	private Map<String,Object> map; 
	public int getAge() { 
		return 18; 

Output code:

		Person person = new Person(); 
		person.setName("Brother Guo is so handsome!"); 
		person.setPhones(new String[]{"18610541354","18688886666","18699998888"}); 

		List<String> cities = new ArrayList<String>(); 
		Map<String,Object>map = new HashMap<>(); 
		pageContext.setAttribute("p", person); 
	output Person: ${ p }<br/> 
	output Person of name Properties: ${} <br> 
	output Person of pnones Array property value: ${p.phones[2]} <br> 
	output Person of cities Element values in collection: ${p.cities} <br> 
	output Person of List Individual element values in the collection: ${p.cities[2]} <br> 
	output Person of Map aggregate: ${} <br> 
	output Person of Map A in the collection key Value of: ${} <br> 
	output Person of age Properties: ${p.age} <br> 


4. EL expression - operation

Syntax: ${operation expression}, EL expression supports the following operators:

4.1 relation operation

4.2 logic operation

4.3 arithmetic operation

4.3.1 empty operation

Empty operation can judge whether a data is empty. If it is empty, it will output true, and if not, it will output false.
The following cases are empty:

1. When the value is null, it is empty
2. When the value is an empty string, it is empty
3. The value is an array of Object type. When the length is zero
4. list collection, the number of elements is zero
5. map collection, the number of elements is zero

	// 1. When the value is null, it is empty 
	request.setAttribute("emptyNull", null); 
	// 2. When the value is an empty string, it is empty 
	request.setAttribute("emptyStr", ""); 
	// 3. The value is an array of Object type. When the length is zero 
	request.setAttribute("emptyArr", new Object[]{}); 
	// 4. list collection, the number of elements is zero 
	List<String> list = new ArrayList<>(); 
	// list.add("abc"); request.setAttribute("emptyList", list); 
	// 5. map collection, the number of elements is zero 
	Map<String,Object> map = new HashMap<String, Object>(); 
	// map.put("key1", "value1"); 
	request.setAttribute("emptyMap", map); 
${ empty emptyNull } <br/> 
${ empty emptyStr } <br/> 
${ empty emptyArr } <br/> 
${ empty emptyList } <br/> 
${ empty emptyMap } <br/> 

4.3.2 ternary operation

Format: expression 1? Expression 2: expression 3
If the value of expression 1 is true, the value of expression 2 is returned. If the value of expression 1 is false, the value of expression 3 is returned.

${ 12 != 12 ? "Guoge Shuai Dai":"Brother Guo lied again" }

4.3.3. "" dot operation and [] bracket operator

. point operation, you can output the value of an attribute in the Bean object.
[] bracket operation, which can output the value of an element in an ordered set. And [] bracket operation can also output the value of the key containing special characters in the key in the map set.

	Map<String,Object> map = new HashMap<String, Object>(); 
	map.put("a.a.a", "aaaValue"); 
	map.put("b+b+b", "bbbValue"); 
	map.put("c-c-c", "cccValue"); 
	request.setAttribute("map", map); 
${ map['a.a.a'] } <br> 
${ map["b+b+b"] } <br>
${ map['c-c-c'] } <br> 

5. 11 implicit objects of EL expression

The 11 implicit objects in EL expressions are defined in EL expressions and can be used directly.

5.1. EL obtains attributes in four specific domains

	pageContext.setAttribute("key1", "pageContext1"); 
	pageContext.setAttribute("key2", "pageContext2"); 
	request.setAttribute("key2", "request"); 
	session.setAttribute("key2", "session"); 
	application.setAttribute("key2", "application"); 
${ applicationScope.key2 } 

5.2 use of pageContext object

  1. agreement:
  2. Server ip:
  3. Server port:
  4. Get project path:
  5. Get request method:
  6. Get client ip address:
  7. Get the id number of the session:
	request.getScheme() It can get the requested protocol 
	request.getServerName() Get the requested server ip Or domain name 
	request.getServerPort() Gets the requested server port number 
	getContextPath() Get current project path
	request.getMethod() How to get the request( GET or POST) 
	request.getRemoteHost() Get client's ip address 
	session.getId() Gets the unique identity of the session 
	pageContext.setAttribute("req", request); 
<%=request.getScheme() %> <br>
1.agreement: ${ req.scheme }<br> 
2.The server ip: ${ pageContext.request.serverName }<br> 
3.Server port: ${ pageContext.request.serverPort }<br> 
4.Get project path: ${ pageContext.request.contextPath }<br> 
5.Get request method: ${ pageContext.request.method }<br> 
6.Get client ip Address: ${ pageContext.request.remoteHost }<br> 
7.Gets the of the session id No.: ${ }<br> 

5.3 use of EL expression and other implied objects

Example code:

Output request parameters username Value of: ${ param.username } <br> 
Output request parameters password Value of: ${ param.password } <br> 

Output request parameters username Value of: ${ paramValues.username[0] } <br> 
Output request parameters hobby Value of: ${ paramValues.hobby[0] } <br> 
Output request parameters hobby Value of: ${ paramValues.hobby[1] } <br>

Example code:

Output request header[ User-Agent]Value of: ${ header['User-Agent'] } <br> 
Output request header[ Connection]Value of: ${ header.Connection } <br> 
Output request header[ User-Agent]Value of: ${ headerValues['User-Agent'][0] } <br>

Example code:

obtain Cookie Name of: ${ } <br> 
obtain Cookie Value of: ${ cookie.JSESSIONID.value } <br>

Configuration in web.xml:



Example code:

output&lt;Context-param&gt;username Value of: ${ initParam.username } <br> 
output&lt;Context-param&gt;url Value of: ${ initParam.url } <br>

2, JSTL tag library

The full name of JSTL tag library refers to JSP standard tag library. It is a continuously improved open source JSP tag library.
EL expression is mainly used to replace the expression script in jsp, while tag library is used to replace the code script, which makes the whole jsp page more concise.

JSTL consists of five tag libraries with different functions.

Use the taglib instruction in the jsp tag library to introduce the tag library

CORE Tag library 
	<%@ taglib prefix="c" uri="" %> 
XML Tag library
	<%@ taglib prefix="x" uri="" %> 
FMT Tag library 
	<%@ taglib prefix="fmt" uri="" %> 
SQL Tag library 
	<%@ taglib prefix="sql" uri="" %> 
FUNCTIONS Tag library 
	<%@ taglib prefix="fn" uri="" %>

1. Steps for using JSTL tag library

1. First import the jar package of jstl tag library.

2. The second step is to use the taglib instruction to introduce the tag library.

<%@ taglib prefix="c" uri="" %>

2. core library usage

2.1. < C: set / > (rarely used)

Function: the set tag can save data to the domain

	<c:set /> 
	effect: set Tags can save data to fields 

	Domain object.setAttribute(key,value); 
	scope Property settings are saved to which domain 
		page express PageContext Field (default) 
		request express Request field 
		session express Session field 
		application express ServletContext field 
	var Property settings key How much is it 
	value Property setting value 
Before saving: ${ } <br> 
<c:set scope="session" var="abc" value="abcValue"/> 
After saving: ${ } <br>

2.2,<c:if />

The if tag is used for if judgment.

	<c:if /> 
		if Labels are used for if Judgment. 
		test Property represents the condition of judgment (using EL Expression output) 
<c:if test="${ 12 == 12 }"> 
	<h1>12 Equal to 12</h1> 
<c:if test="${ 12 != 12 }"> 
	<h1>12 Not equal to 12</h1> 

2.3. < C: choose > < C: when > < C: otherwise > label

Function: multi-channel judgment. Very close to switch... case... default

	<c:choose> <c:when> <c:otherwise>label 
	Function: multi-channel judgment switch ... case .... default Very close 

	choose Tag start selection judgment 
	when The label represents each judgment 
		test Property represents the value of the current judgment 
	otherwise The label indicates the rest 

	<c:choose> <c:when> <c:otherwise>Points to note when using labels: 
		1,It cannot be used in the label html Notes, to use jsp notes 
		2,when The parent label of the label must be choose label 
	request.setAttribute("height", 180); 
	<%-- This is html notes --%> 
	<c:when test="${ requestScope.height > 190 }"> 
		<h2>Little giant</h2> 
	<c:when test="${ requestScope.height > 180 }"> 
		<h2>Very high</h2> 
	<c:when test="${ requestScope.height > 170 }"> 
		<h2>just so so</h2> 
		<c:when test="${requestScope.height > 160}"> \
			<h3>Greater than 160</h3> 
		<c:when test="${requestScope.height > 150}"> 
			<h3>Greater than 150</h3> 
		<c:when test="${requestScope.height > 140}"> 
			<h3>Greater than 140</h3> 
			Others less than 140 

2.4,<c:forEach />

Function: used to traverse the output.

Example code: 1. Traverse 1 to 10 and output

	1.Traverse 1 to 10, output 
	begin Property to set the start index 
	end Property sets the index of the end 
	var Property represents the variable of the loop(It is also the data currently being traversed) 
	for (int i = 1; i < 10; i++) 
<table border="1"> 
	<c:forEach begin="1" end="10" var="i"> 
			<td>The first ${i}that 's ok</td> 

Example code: 2. Traversing the Object array

	2.ergodic Object array 
	for (Object item: arr) 
	items Represents the traversed data source (traversed Collection) 
	var Represents the data currently traversed 
	request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"}); 
<c:forEach items="${ requestScope.arr }" var="item"> 
	${ item } <br> 

Example code: 3. Traverse the Map set

	Map<String,Object> map = new HashMap<String, Object>(); 
	map.put("key1", "value1"); 
	map.put("key2", "value2"); 
	map.put("key3", "value3"); 
// for ( Map.Entry<String,Object> entry : map.entrySet()) { 
// } 
	request.setAttribute("map", map); 
<c:forEach items="${ }" var="entry"> 
	<h1>${entry.key} = ${entry.value}</h1> 

Example code: 4. Traverse the list set - the Student class is stored in the list, with attributes: number, user name, password, age and telephone information
Student class:

public class Student { 
	//4. Number, user name, password, age, telephone information 
	private Integer id; 
	private String username; 
	private String password; 
	private Integer age; 
	private String phone;
<%--4.ergodic List aggregate---list Storage in Student Class, with attributes: number, user name, password, age, telephone information--%> 
	List<Student> studentList = new ArrayList<Student>(); 
	for (int i = 1; i <= 10; i++) { 
		studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i)); 
	request.setAttribute("stus", studentList); 
		<th>user name</th> 
	items Represents a collection of traversals 
	var Represents the traversed data 
	begin Indicates the start index value of the traversal 
	end Index value indicating end 
	step Property represents the step value of the traversal 
	varStatus Property represents the state of the data currently traversed 
	for(int i = 1; i < 10; i+=2) 
<c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu"> 

Tags: Java

Posted on Fri, 15 Oct 2021 21:16:06 -0400 by klaibert26