use of JSTL and EL
is to simplify the code if you used to embed code in Java.
<%names = Request.getattribute ("name");%>
JSP code ...
<%for (int i=0;i<names.length;i++) {
String Name=names.get (i);
%>
<tr>
<td>
<%=name%>
</td>
</tr>
<%}%>
If you use Jstl+el words will not be so troublesome, and the enterprise will not like the above code operation, mainly the code is huge, if you use Jstl+el words will be much better
<c:foreach var= ' name ' items= ' ${names} ' >//This sentence is jstl expression
<tr>
<td>
${name}//This sentence is an El expression
</td>
</tr>
To be able to watch sex is certainly jstl+el easy to understand, and he moves to get the value from the server, highly coupled with the JSP implementation.
The above is a comparison
The following examples illustrate
Preparation before use of Jstl
To use JSTL, you first need to import JSTL packages (Jstl.jar and standard.jar) to your project.
JSTL Tag Library
The JSTL is divided into the following five tags
- Core tags
- Formatting labels
- SQL Tags
- XML Tags
- JSTL function
Different libraries need to be introduced into the JSP when using different tags
core tags (cores):
Introducing Tag Libraries
1 |
<% @taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> |
<c:set>
<c:set> setAttribute () method equivalent to the session in the JSP
How to use:
1.<c:set var= "name" value= "value"/>
2.<c:set var= "Name" > value </c:set>
<c:out>
<c:out> is equivalent to <%=%> in JSP
How to use:
<c:out value= "value required for Output" >
Here you can use the EL expression to output the attrbute in the session
Cases:
<c:set var= "Test" value= "This is a test" ><c:out value= "${test}" >
Output result: This is a test
<c:remove>
<c:remove> and c:set label just the opposite , C:set tag is set Setattrbute This tag is deleted Attrbute Set the value equal to the removeattrbute () method in the session
How to use:
<c:remove var= "Name"/>
<c:if>
<c:if> equivalent to the if statement in java
How to use:
<c:if test= "condition" > JSP code that satisfies the condition execution </c:if>
<c:catch>
<c:catch> similar to try in Java
How to use:
<c:catch var= "First name" >jsp code </c:catch>
If there is an error in the JSP code during the execution, it will be copied to the name, so we can determine if there is an exception by judging whether the name is NULL.
Cases:
<c:int i = 5/0;%></C:catch><c:null} ">
The above code has an error occurred
</c:if>
Output: Error occurred in the above code
If you change the above 0 to 3, there is no output .
<c:choose> and <c:when>
<c:choose> and <c:when> are similar to switch and case in Java
How to use: ( can have multiple c:when tags in c:choose )
<c:choose><c:when test= "conditions" > Handling </c:when><c:when test= "conditions" > Handling </c:when></c:choose >
Cases:
<c:set var= "Test" value= "ten"/><c:choose><c:when test= "${test > 5}" >test greater than 5</c:when><c : When test= "${test < 5}" >test less than 5</c:when></c:choose>
Output result: Test greater than 5
<c:otherwise>
<c:otherwise> is equivalent to default in java switch
How to use
<c:choose><c:otherwise> processing </c:otherwise></c:choose>
When the When in choose does not meet the criteria, it executes the contents of the otherwise
Note: Choose and when and otherwise are a set of when and otherwise must be written in choose
<c:import>
How to use:
<c:import var= "date" url= "http://www.baidu.com"/>
This will return the source code of Baidu to the date variable , using the
<c:out value= "${date}" >
Output
<c:forEach> and <c:forTokens>
Similar to for loops and FOREACH loops in Java
How to use:
<c:foreach var= "First name (i)" begin= "Start number (1)" end= "End number (5)" >${i}</c:foreach>
The output is 1 2 3 4 5
How to use:
<c:fortokens items= "A,b,c,d,e" delims= "," var= "name" >${name}</c:fortokens>
Output result a b c d E
Foreach similar to java for cycle set a start number Span style= "Font-family:calibri" >begin Set an end number Then each cycle will assign values to var There is also a setp parameters can be set value meaning is to add a few numbers at a time
Fortokens similar to foreach in JAVA put a group in items and then delims set the partition and assign the value of the split loop to var if the items are stored in a collection then you do not have to write delims so that each cycle will be taken out of the set of one in var
<c:redirect>
Similar to redirection in service
How to use:
<c:redirect url= "http://www.baidu.com"/>
Redirect to Baidu to go
There are also <c:url> and <c:param> two labels
An EL expression describes :
EL expression Definition rule : write in {} In the beginning of the $ example : ${test}
Of course , because all the code in the EL expression is written in {} , We can also write operations in it
Like what:
${TEST+100}
The result of the final output is the value of the test variable plus the number.
Another example
The result of the final output is a true in this way we can use it well together with JSTL .
The operators in EL are divided into . and [] two kinds
If you want to dynamically fetch values you can use [] for example :
${session.user[date]}
where the date is a variable
The above code means to take out the first date element in the session.user array.
It is important to note that the EL expression restricts our call to the JAVA method, and if you want to invoke it, you can customize a TAG
EL Expression Lookup order ( all for Attrbute ()):
If you do not use a range similar to ${username} to find the username, then it will be :
- Page
- Request
- Session
- Application
In order to find, join Midway found username then return the value if not found return null
The recessive variable of the EL expression :
Starting from here is the introduction of the hidden variables of El expressions, which allow us to do many complex operations with ease :
pagecontext |
hidden object used to access JSP |
pagescope |
Page Object ma P |
requestscope |
Request object's map |
requestscope |
request object's map |
sessionscope |
Session object's map |
applicationscope |
Application object's map |
param |
contains the request parameter string for the map |
paramvalues |
contains a map of the request parameter string array |
header |
contains the request header string map |
headervalues |
contains a map of the request header string array |
Cookies |
Store a map of the cookie that came with the request by name |
Example: Let 's say we want the name value in the session so we can use :
${sessionscope.name}
Again: we want to get The name parameter passed by the get or POST, which we can only use in the past :
Request.getparameter (name);
Use an El expression instead of :
${param.name}
Similarly, if we want to get an array of parameters , we can use :
${paramvalues.name}
Of course, an array object is returned .
Jstl+el finishing from other bloggers ' analysis