EL (Expression language)
Expression Language,el is a new feature in JSP2.0. With the expression language, you can easily access the contents of attributes in the flags (page,request,session and application flag bits) to avoid many scriptlet code. The syntax is as follows:
${Property Name}
Note: scriptlet: In JSP, Scriptlet is called a script applet, and all Java programs embedded in HTML code must be marked with Scriptlet. A total of three kinds:
The first kind: <%%> the second kind of:<%!%>: <%=%>
El expressions make it easy to access properties in an object, submitted parameters, or perform various mathematical operations, and the most important feature of El expressions is that if the output is null, an empty string ("") is automatically used. the El Expression language can output content in 4 ranges, such as setting the same property name in different attribute scopes, and will look in the following order:
Page--Request--session-application
<% pagecontext.setattribute ("info", "page Range Properties"); Request.setattribute ("info", "Request range attribute"); Session.setattribute ("Info", "Session range attribute"); Application.setattribute ("Info", "Application range attribute"); %>${info}
The result of the above code is: Page Range property.
Of course, to specify a range to remove attributes, you can add tags
Attribute Range |
Example |
Description |
Page |
${pagescpe. Properties} |
Remove the property contents of the page range |
Request |
${requestscpe. Properties} |
Remove the property contents of the request scope |
Session |
${sessionscpe. Properties} |
Remove the property contents of the session range |
Application |
${applicationscpe. Properties} |
Remove the property contents of the application range |
Accept Request Parameters
The expression language allows you to display the received request parameters, similar to Request.getparameter (), with the following syntax:
Receive a parameter ${param. Parameter name}
Receives a set of parameter ${paramvalues. Parameter name}
After receiving a set of parameters, if you want to remove, you need to specify the subscript.
Operations on the collection (Collection, contents of the map collection) saves the collection in the request scope, and is accessed directly through the collection's subscript when using the EL expression output.
<% new Arraylist (); List.add ("a"); List.add ("B"); List.add ("C"); Request.setattribute ("list", list); %> First element: ${list[0]} second element: ${list[1]} third element: ${list[2]}
The result of the above code is:
First element: A
Second element: b
A third element: C
operator
arithmetic operator |
Description |
example |
result |
+ |
Addition |
${4+6} |
10 |
- |
Subtraction |
${4-6} |
-2 |
* |
Multiplication |
${4*6 } |
|
/or div |
Division |
${4/6 } or ${4 Div 6} |
0.666 |
% or mod |
Modulo (remainder) |
${4%6} or ${4 mod 6} |
4 |
Special attention
"+" in El only represents addition, does not indicate a connection;
${a+ ""} ///// exception ${10/0} //Infinity// true to compare the position of the character Unicode code // returns a Boolean, which is whether the ulist is empty.
JSTL (JSP standard Tag Library)
JSP Standard Tag Library
using a tag library avoids too much scriptlet code, but the practice of using a custom tag library is cumbersome and non-generic, so some open source tools can be used to develop code using some common tags. JSTL (JSP standard tag library,jsp Standards Tag Library) is an open source label component. the Core tag library is the most important part of JSTL, and it is also the most common part in the development, which is the process control, iteration output and so on in the core tag library.
key tags in the core tag library
Functional classification |
Label name |
Describe |
Basic label |
<c:out> |
Output attribute Content |
<c:set> |
Setting Property Content |
<c:remove> |
Delete the specified property |
<c:catch> |
Exception handling |
Process Control |
<c:if> |
Conditional judgment |
<c:choose> |
Multi-conditional judgment, can set <c:when> and <c:otherwise> tag |
Iteration Labels |
<c:forEach> |
Output Array, collection |
<c:forTokens> |
String splitting and output operations |
Include tags |
<c:import> |
Include the specified path to the current page |
Generate URL Tags |
<c:url> |
Generate a new URL based on path and parameters |
Client Jump |
<c:redirect> |
Client Jump |
Before using the core tag library, the tag library profile must be introduced in the JSP page.
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%><%@ taglib uri= "http://java.sun.com/jsp/ Jstl/fmt "prefix=" FMT "%>
<c:if> Tags
Grammar:
<c:if test= "judging condition" var= "storage Judgment result" ></C:if>
Example
<c:ifnull} "> Welcome, ${username}</c:if>
<c:choose> tags
<c:if> 's function is only for one condition judgment, if you want to judge multiple conditions, you can use <c:choose> tag,<c:choose> can only be used as <c:when> and <c:o Therwise> 's parent tag appears and cannot be used alone.
Example:
Request.setattribute ("num",N);
%><c:choose> <c:when test= "${num = = Ten}" > num property has a value of ten</c:when> < C:when test= "${num = =" > num property has a value of</c:when> <c:otherwise> Not one condition to meet </c:otherwise></c:choose>
The above code output is: no one condition satisfies
<c:otherwise> tags are executed when all <c:when> is not satisfied, so be sure to put them at the end of all <c:when> tags when writing.
<c:forEach> tags
The main function of the <c:forEach> tag is loop control, which can iterate the output of the members of the collection, similar to the iterator interface.
<c:foreach [var=] The property name of each object "] items=" collection "[varstatus=" holds member information of the current object "] [begin=" The Start output location of the collection "] [end=" The end output position of the collection] [step=] Step]> for each growth
Label body
</c:forEach>
Example :
<% = {"A", "B", "C", "D", "E", "F", "G"}; Request.setattribute ("info", info); %> Example one:<c:foreach var = "s" items= "${info}" > ${s},</c:forEach> example two: <c:foreach var = "s" items= "${info}" step= "2" > ${s},</c:forEach> example three:<c: ForEach var = "s" items= "${info}" begin= "1" end= "2" > ${s},</c:forEach>
The above code outputs the result:
Example one: A, B, C, D, E, F, H,
Example two: A, C, E, H,
Example three: B, C,
when using <c:forEach> output, you can output not only the list, but also the set, that is, as long as the sub-interface or class of the collection interface can be output. <c:forEach> can also export the map collection, but the elements in the map collection are stored in key-value pairs, and for key and value, the Getkey () and GetValue () methods provided by the element are required.
Example:
<% new haspmap (); M.put ("1", "first"); M.put ("2", "second"); Request.setattribute ("M", m); %> <c:foreach var = "s" items= "${m}" > -- ${m.value}</c:forEach>
The above code outputs the result:
1--First
2--second
El and Jstl