Use of JSTL and EL, and use of JSTLEL
Use of JSTL and ELPreparations before using JSTL
To use JSTL, first import the JSTL package (JSTL. jar and standard. jar) to the project ).
JSTL tag Library
JSTL consists of the following five labels:
When using different tags, You need to introduce different libraries in JSP.
Core tags ): Introduce tag Library
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<C: set>
<C: set> equivalent to the setAttribute () method of the session in JSP
Usage:
1. <c: set var = "name" value = "value"/>
2. <c: set var = "name"> value </c: set>
<C: out>
<C: out> equivalent to <% = %>
Usage:
<C: out value = "value to be output">
Here, the EL expression can be used to output Attrbute in the session.
Example:
<C: set var = "test" value = "this is a test"> <c: out value = "$ {test}">
Output result: this is a test.
<C: remove>
<C: remove> the c: set label is the opposite. The c: set label sets the setAttrbute label, and deletes the value set in Attrbute, which is equivalent to the removeAttrbute () method in the session.
Usage:
<C: remove var = "name"/>
<C: if>
<C: if> equivalent to the if statement in java
Usage:
<C: if test = "condition"> JSP code that meets the condition </c: if>
<C: catch>
<C: catch> similar to try in java
Usage:
<C: catch var = "name"> JSP code </c: catch>
If an error occurs in the JSP code during execution, it will be copied to the name. Therefore, we can determine whether an exception has occurred by determining whether the name is NULL.
Example:
<c:catch var=”testException”><% int i = 5/0; %></c:catch><c:if test=”${testException != null}”>
The above code has an error
</C: if>
Output result: An error occurred in the above Code.
If you change the value of 0 to 3, no output is returned.
<C: choose> and <c: when>
<C: choose> and <c: when> are similar to the switch and case
Usage: (c: choose can contain multiple c: when labels)
<C: choose> <c: when test = "condition"> processing </c: when> <c: when test = "condition"> processing </c: when> </c: choose>
Example:
<C: set var = "test" value = "10"/> <c: choose> <c: when test = "$ {test> 5}"> test is greater than 5 </c: when> <c: when test = "$ {test <5}"> test less than 5 </c: when> </c: choose>
Output result: test is greater than 5
<C: otherwise>
<C: otherwise> equivalent to default in java switch
Usage
<C: choose> <c: otherwise> processing </c: otherwise> </c: choose>
When the when in the choose does not meet the conditions, the content in otherwise will be executed.
Note: choose and when and otherwise are a group of when and otherwise which must be written in choose
<C: import>
Usage:
<c:import var=”date” url=”http://www.baidu.com” />
The above writing method will return Baidu's source code to the date variable, using
<c:out value=”${date}”>
Output
<C: forEach> and <c: forTokens>
Similar to the FOR loop and FOREACH loop in JAVA
Usage:
<C: forEach var = "Name (I)" begin = "Start number (1)" end = "end number (5)" >$ {I} </c: forEach>
The output result is 1 2 3 4 5.
Usage:
<c:forTokens items=”a,b,c,d,e” delims=”,” var=”name”>${name}</c:forTokens>
Output result a B c d e
ForEach is similar to the for loop in JAVA to set a start number begin to set an end number end, and then assign the value to var in each loop. In addition, there is also a setp parameter, which can be set to increase the number of values each time.
ForTokens is similar to the foreach in JAVA. Put a group in items, set the delims to what to split, and assign the value of the split loop to var. If the items stores a set, you do not need to write delims. one of the sets will be taken out and put in var for each loop.
<C: redirect>
Similar to redirection in service
Usage:
<c:redirect url=”http://www.baidu.com”/>
Redirect to Baidu
There are also two labels: <c: url> and <c: param>.
EL expression introduction:
EL expression definition rules: content starting with $ is written in {}. Example: $ {test}
Of course, because all the EL Expression Code is written in {}, we can also write operations in it.
For example:
${test+100}
The final output result is the value after the variable test is added with the number 100.
For example
${test != null}
The final output result is true. In this way, we can use JSTL in combination.
The operators in EL are divided into. And [].
If you want dynamic values, you can use [] For example:
${session.user[date]}
The date is a variable.
The code above indicates to retrieve the date element in the array session. user.
Note that the EL expression limits the JAVA method to be called. If you want to call this method, you can customize a TAG.
EL expression query Order (all for Attrbute ()):
If you use a range similar to $ {username} that does not search for username, it will use:
Search in order. If username is found in the middle, the return value is returned. If no value is found, null is returned.
Hidden variables of EL expressions:
Here we will introduce the hidden variables of EL expressions. These hidden variables allow us to easily complete many complex operations:
| PageContext |
Implicit object used to access JSP |
| Pagination |
MAP of the page Object |
| RequestScope |
MAP of the request object |
| RequestScope |
MAP of the request object |
| SessionScope |
MAP of the session Object |
| ApplicationScope |
MAP of the application Object |
| Param |
MAP containing the request parameter string |
| ParamValues |
MAP containing the string array of Request Parameters |
| Header |
MAP containing the request header string |
| HeaderValues |
MAP containing the request header string array |
| Cookie |
Store the MAP of the cookie contained in the request by name |
For example, if we want the name value in the session, we can use:
${sessionScope.name}
Another example: if we want to GET the name parameter passed by GET or POST, we can only use:
request.getParameter(name);
The EL expression can be used instead:
${param.name}
Similarly, if we want to get an array of parameters, we can use:
${paramValues.name}
Of course, an array object is returned.