el expression and jstl tag library

el expressions are only used in jsp pages.

The role of the el expression when it was first proposed:

1. Data can be obtained from domain objects. easier.

${expression to get domain object content}
pageContext
request
session
application

2. The second usage is to write various operations in el expressions.

Arithmetic
comparison operation
logic operation
Ternary operation

1. Use the el expression to get the value in the domain object

application
session
request
pageContext page

(1) el expressions are used in jsp pages, not in html, or in servlets.
(2) Writing method: ${domain object name}
(3) Use el expressions to get the values in the four domain objects
(4) el expression

= code

<%
// We can set the value through the domain object.
request.setAttribute(“aaa”, “AAA”);
session.setAttribute(“bbb”, “BBB”);
pageContext.setAttribute(“ccc”, “CCC”);
application.setAttribute(“ddd”, “DDD”);

//The way to get the value before.
String value = (String) request. getAttribute(“aaa”);
%>

<-- Get the parameter value set by the domain object through the el expression. How to get it. -->
<-- The el expression can directly get the value through the name of the domain object, and does not involve the issue of forced type conversion. -->
${aaa}

${bbb}

${ccc}

${ddd}

(4) If the name obtained by using the el expression does not exist, an empty string “” will be returned, and a null pointer exception will not occur
el has already done coercion for us at the bottom of the expression, and we don’t need to do it manually.
It can improve the security of the code, and the display effect can be displayed in a friendly manner.

 If you use the traditional method getAttribute to obtain a name that does not exist, return null, which is prone to null pointer exceptions

(5) Use the el expression to obtain the domain object with the same name, and obtain the value inside the domain with the smallest scope

<%
// We can set the value through the domain object.
request.setAttribute(“andy”, “AAA”);
session.setAttribute(“andy”, “BBB”);
pageContext.setAttribute(“andy”, “CCC”);
application.setAttribute(“andy”, “DDD”);

//The way to get the value before.
String value = (String) request. getAttribute(“andy”);
out. println(value);
%>

<-- Get the parameter value set by the domain object through the el expression. How to get it. -->
<-- The el expression can directly get the value through the name of the domain object, and does not involve the issue of forced type conversion. -->
<-- After we set the names of all domain objects to the same name, we found that what we accessed was the value set by the page object -->

${andy}

${andy}

${andy}

${andy}

<-- How to solve this problem? It can be accessed through domain object + scope. --> ${requestScope. andy}

${sessionScope. andy}

<-- Note: To get the pageContext object in the el expression, use page -->

${pageScope. andy}

${applicationScope. andy}

Conclusion: The order of the four scopes from small to large is:
pageContext < request < session < application

2. Use el expressions to obtain the values of arrays, collections (list, map), and object attributes in domain objects

(1) Use the el expression to get the value of the array in the domain object

= Use the subscript of the array to get the value inside, the subscript of the array starts from 0
= ${domain object name[subscript of array]}

= code

<%
//Define an array of strings.
String[] arr = {“aaa”, “bbb”, “ccc”};
//Save the array object as a parameter of the request domain object.
request.setAttribute(“arr”, arr);

%>

<-- Get the element value of the stored array through the el expression. -->
${arr[0]}
${arr[1]}
${arr[2]}

= If the subscript of the obtained array does not exist, the return is an empty string “”
//When we accessed the element corresponding to the index of the non-existing array through java code, it would report: array corner mark out of bounds exception.
//out.println(arr[10]); // java.lang.ArrayIndexOutOfBoundsException: 10

<-- When accessing through the el expression, when the subscript of the array does not exist, no error will be reported, and an empty string "" will be displayed -->
${arr[10]}

(2) Use the el expression to get the value of the list collection in the domain object

= Use the subscript of the list collection to get the value inside, the list subscript starts from 0
= ${domain object name[subscript of list collection]}
= code

<%
//Create a list collection
List list = new ArrayList();
list.add(“AAA”);
list.add(“BBB”);
list.add(“CCC”);
list.add(“DDD”);

//Through the domain object, store the list collection in the domain object parameter.
request.setAttribute(“list100”, list);

%>

<-- el expression to get the value -->
<-- Use the traditional way to traverse the list, there are four ways
Ordinary for: Obtained according to the subscript, the subscript starts from 0
iterator
enhanced for
list iterator
–>
<-- Get the list collection through the el expression, and get the value inside. -->
${list100[0]}

${list100[1]}

${list100[2]}

${list100[3]}

= If the subscript to obtain the list collection does not exist, the return is an empty string “”

= Put the set collection in the domain object, use the el expression to get the value of the set collection in the domain object
== The difference between list and set: list is ordered, set is unordered

(3) Use the el expression to get the value of the map collection in the domain object
= Get the corresponding value according to the key name of the map
= Writing method:

domain name

.

k

e

the y

name

=

Another way to write:

{domain name.key name} = another way of writing:

domain name.key name = another way of writing: {domain name[“key name”]}
= code

<%
//Create a map collection, map is an ordered collection, you can use hashMap treeMap’
Map map = new HashMap();
map.put(“aaa”, “AAA”);
map. put(“bbb”, “BBB”);
map.put(“ccc”, “CCC”);
map. put(“ddd”, “DDD”);

// Put the map collection into the domain object.
request.setAttribute(“map100”, map);

%>

<-- Get the elements in the map collection through the el expression. Find value by element name, find value by key –>
${map100.aaa}

<-- If the el expression is used to directly access the name of the map collection we stored, it will print out the collection directly. -->
${map100}

= If the name of the key does not exist, the return is an empty string “”

= If the key type of the map collection is an int type, the value inside cannot be obtained using the el expression, and an error is reported
For example: ${map[3]} // If the key of the map collection is int type. Using .Get will report an error. [] Cannot be obtained.

(4) Use the el expression to get the attribute value of the object in the domain object

= Use the name.Name of the property inside the object to get the value
= ${domain object name.property name}
= code
<-- Use the el expression to get the value of the attribute of the object in the domain object -->
<%
//Create object
User user = new User();
user.setUsername(“Invincible in the East”);
user.setPassword(“999”);
// Put the user object into the domain object
request.setAttribute(“user100”, user);
%>
<-- el expression to get the value -->
${user100. username }

${user100.password}
= If the attribute name of the object in the obtained domain object does not exist, an error will be reported

3. Other operations of el expressions

3.1 Operation of operation

(1) Arithmetic operations + – …
(2) Operation of relation < > <= >=…
(3) Logical operations & amp; & amp; ||…

3.2 empty operator

(1) Determine whether the object is empty
= ${user == null}
= ${empty user}

= ${user != null}
= ${not empty user}

3.3 Ternary expressions

Ternary expressions are like, if(){}else{}

(1) ${user != null ? user.name : “”}
= If the user object is not empty, get the name value of the user, otherwise return an empty string
= ${ not empty user ? user.name : “”}

3.4 There are 11 implicit objects in the el expression

(2) pageContext implicit object: corresponding to the built-in object of pageContext in jsp, get other eight built-in objects

JSTL tag (It is a tag library.)

jstl tag library

1. Introduction to jstl tag library
1.1 jstl: JavaServerPages Standard Tag Library, the standard tag library of jsp
1.2 Use the jstl tag library, which can be used together with the jsp command taglib to replace <% %> in the page
(1) jstl version 1.0 1.1 1.2

1.3 To use the jstl tag library, import the jstl jar package (two jar packages)
(1) The jstl tag library is used in the jsp page, and the tag library needs to be introduced in the jsp page, using the taglib command to import
= <%@ taglib uri="path of jstl tag library" prefix="alias of jstl tag library" %>
= Find the imported jar package, find the c.tld file of META-INF, find
c
http://java.sun.com/jsp/jstl/core
= <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

(2) Writing method:

=

1.4 Introductory case of jstl
(1) Code
<-- Embed java code -->
<%
int m = 10;
if(m == 10) {
%>

m is equal to 10

<% } else { %>

m is not equal to 10

<% } %>

<hr/>
<!-- Use the jstl tag library to replace the java code with jstl to express it -->
<!-- Define a variable, use the set tag in jstl to set a value into the domain object -->
<c:set var="a" value="20"></c:set>
<!-- Conditional judgment, implemented using the if tag in jstl -->
<c:if test="${a==10 }">

a is equal to 10

a is not equal to 10

2. The if tag of jstl

2.1 Labels for Conditional Judgment
2.2 Writing
2.3 There is no tag in jstl
2.4 Code
<-- Conditional judgment, using the if tag in jstl to achieve -->

a is equal to 10

a is not equal to 10

3. The forEach tag of jstl

3.1 Labels of traversed operations
3.2 Use the forEach tag to get the attribute values of arrays, collections (list, set, map), and multiple objects in the domain object
3.3 Writing method:

3.4 Use the forEach tag to get the value of the array in the domain object
(1) Code
<-- Use jstl's foreach tag to get the value of the array in the domain object -->
<%
//Create an array and put the array into the domain object
String[] arr = {“lucy”, “mary”, “jack”};
request.setAttribute(“arr”, arr);
%>
<-- Use el expression to get -->
${arr[0]}

<hr/>
<!-- Use the foreach tag to get -->
<!--

Using the enhanced for loop
for(String a : arr) {}
–>

${a}

3.5 Use jstl’s forEach tag to get the value in the collection in the domain object
(1) Get the value in the list collection in the domain object
= code
<-- Use the forEach tag of jstl to get the value of the list collection in the domain object -->
<%
//Create list collection
List list = new ArrayList();
list.add(“aaaa”);
list.add(“bbbb”);
list.add(“cccc”);
// Put the list collection into the domain object
request.setAttribute(“list”, list);
%>
<-- Use the el expression to get the value of the list inside -->
${list[0]}

<hr/>
<!-- Use the forEach tag to get the value -->
<!--

for(String l : list) {}
–>

${l}

(2) Get the value in the set collection in the domain object
= Using the el expression cannot get the value in the set collection in the domain object, because the set collection is unordered and cannot be obtained by subscript
= code
<-- Use the forEach tag in jstl to get the value of the set collection in the domain object -->
<%
//Create a set collection and put the set collection in the domain object
Set set = new HashSet();
set.add(“WWW”);
set.add(“QQQ”);
set.add(“TTT”);
request.setAttribute(“set”, set);
%>
<-- Use el expression to get -->
<%-- the s e t [ 0 ] ? ? < ? ? use f o r E. a c h label acquisition ? ? >

< c : f o r E. a c h v a r = " the s e t " i t e m the s = " {set[0] } --%> <-- use the forEach tag to get -->
${set}

(3) Get the value in the map collection in the domain object
= Use the el expression to get the map collection value in the domain object, and get the value according to the key of the map
= code
<-- Use jstl's foreach tag to get the map collection value in the domain object -->
<%
//Create a map and put the map collection into the domain object
Map map = new HashMap();
map.put(“aaa”, “AAA”);
map. put(“bbb”, “BBB”);
map.put(“ccc”, “CCC”);
request.setAttribute(“map”, map);
%>
<-- Use the el expression to get the value -->
${map.aaa}

<hr/>
<!-- Use the foreach tag to get the value -->
<c:forEach var="m" items="${map }">

${m.key} , ${m.value}

3.6 Use the forEach tag of jstl to get the values of the attributes of multiple objects in the domain object

(1) How to put multiple objects into domain objects
= You can put multiple objects into the collection (list) first, and then put the list collection into the domain object
= code
<-- put multiple objects into the domain object -->
<%
//Create multiple objects
User user1 = new User();
user1.setUsername(“Invincible in the East”);
user1.setPassword(“999”);

User user2 = new User();
user2.setUsername(“Yue Buqun”);
user2.setPassword(“444”);

User user3 = new User();
user3.setUsername(“Lin Pingzhi”);
user3.setPassword(“666”);

// Put multiple objects into the list collection
List list = new ArrayList();
list. add(user1);
list. add(user2);
list. add(user3);
// Put the list collection into the domain object
request.setAttribute(“list”, list);
%>

= Code to get property values of multiple objects
<-- Use the foreach tag of jstl to get the values of the attributes of multiple objects in the domain object -->
<-- Use the foreach tag to get the value -->

${user.username} :: ${user.password}