Preface
Since the JSP 1.1 specification, JSP supports the use of custom tags in JSP. The wide use of custom tags has resulted in repeated definitions by programmers, this has led to the birth of jstl (JavaServer Pages standard tag library.
Jstl is used in work, but the Chinese documents about jstl cannot be found on the Internet, so this article is available.
Jstl Introduction
Jstl is an open-source JSP tag library that is constantly improved and maintained by Apache's Jakarta team. Jstl can only run on containers that support jsp1.2 and servlet2.3 specifications, such as Tomcat 4.x. However, JSP 2.0 will be supported as a standard.
The latest version of jstl is 1.02 and the final version is 1.0. Jstl consists of two parts: the tag library and El (Expression Language) language. The tag library currently supports four types of tags:
Tag |
Uri |
Prefix |
Example |
Core |
Http://java.sun.com/jstl/core |
C |
<C: tagname...> |
XML Processing |
Http://java.sun.com/jstl/xml |
X |
<X: tagname...> |
I18n capable formatting |
Http://java.sun.com/jstl/fmt |
FMT |
<FMT: tagname...> |
Database Access (SQL) |
Http://java.sun.com/jstl/ SQL |
SQL |
<SQL: tagname...> |
Core supports some basic operations in JSP;
XML processing supports processing XML documents;
I18n capable formatting supports internationalization of JSP pages;
Database Access (SQL) supports JSP database operations.
Due to my limited level, this article only introduces core tags. If you are interested, you can discuss the use and expansion of the other three tags.
El
The El language is the representation of a Java expression from the jstl output (input.
In jstl, El can only be used in attribute values. The El language can only be called by creating an expression ${exp1. There are three ways to use expressions in attribute values.
1. The value attribute contains an expression.
<Some: Tag value = "$ {expr}"/>
In this case, the expression value is calculated and assigned to the value attribute according to the type conversion rule. For example, $ {username} in <C: Out value = "$ {username}"/> is an El, which is equivalent to a JSP statement <% = request. getattribute ("username") %> or <% = session. getattribute ("username") %>
2. The value attribute contains one or more attributes, which are separated by text or centered around
<Some: Tag value = "some $ {expr }$ {expr} text $ {expr}"/>
In this case, the expression is calculated from left to right, the result is converted to a string type (according to the type conversion rules), and the result is assigned to the value attribute.
3. The value attribute only contains text
<Some: Tag value = "sometext"/>
In this case, the value of the string type property is converted to the desired type of the tag according to the type conversion rules.
El Operators
Obtains the attribute values of an object or set.
To obtain attributes in a set, El supports the following two operations:
1. Use the. Operator to obtain attributes with names. For example, the expression $ {user. Username} indicates the username attribute of the Object User.
2. Use the [] operator to obtain attributes with names or numbers.
The expression $ {user ["username"]} has the same meaning as the expression $ {user. Username }.
Expression $ {row [0]} indicates the first entry of the row set.
Here, user is a class object, and its property username must comply with the standard JavaBean specification, that is, the corresponding getter and setter methods must be defined for the username attribute.
Empty operator (Null Value Check)
Use the empty operator to determine whether an object, set, or string variable is null or null. For example:
$ {Empty Param. Username}
If the username value in the request parameter list is null, the expression value is true. El can also directly use the comparison operator to compare with null. For example, $ {Param. firstname = NULL }.
Comparison operator
Operator |
Description |
= Or EQ |
Equality check |
! = Or ne |
Unequal check |
<Or lt |
Less than check |
> Or GT |
Greater than check |
<= Or Le |
Less than or equal to check |
> = Or Ge |
Greater than or equal to check |
The numeric and logical operators are the same as those in Java and are not listed.
Core tag Library
1. General labels
<C: Out>
<C: Out> A tag is used to display data in JSP. It has the following attributes:
Genus |
Description |
Required? |
Default Value |
Value |
The output information, which can be an El expression or constant. |
Yes |
None |
Default |
Information displayed when value is null |
No |
None |
Escapexml |
If the value is true, special XML character sets are avoided. |
No |
True |
Example:
Your username is: <C: Out value = "$ {user. Username}" default = "guest"/> |
Displays the user name. If it is null, guest is displayed.
<C: Out value = "$ {sessionscope. Username}"/> |
Display the username value obtained from the session;
<C: Out value = "$ {username}"/> |
Display the username value. The value is retrieved from the request (PAGE) by default. If the request does not contain an object named username, It is retrieved from the session. If the value is not in the session, it is retrieved from the application (servletcontext) if no value is obtained, it is not displayed.
<C: Set>
<C: Set> A tag is used to save data. It has the following attributes:
Genus |
Description |
Required? |
Default Value |
Value |
The information to save. It can be an El expression or a constant. |
No |
|
Target |
The variable name of the attribute to be modified, generally the instance of the JavaBean |
No |
None |
Property |
JavaBean attribute to be modified |
No |
None |
VaR |
Variable for saving information |
No |
None |
Scope |
Range of variables for saving information |
No |
Page |
If the target attribute is specified, the property attribute must also be specified.
Example:
<C: Set Value = "$ {test. testinfo}" Var = "Test2" Scope = "session"/> |
Save the value of test. testinfo to Test2 of the session. test is a JavaBean instance and testinfo is the property of the test object.
<C: set target = "$ {Cust. Address}" property = "city" value = "$ {city}"/> |
Save the city property value of the object Cust. Address to the variable city.
<C: Remove>
<C: Remove> A tag is used to delete data. It has the following attributes:
Genus |
Description |
Required? |
Default Value |
VaR |
Variable to be deleted |
Yes |
None |
Scope |
Scope of the deleted variable |
No |
All scopes, including page, request, session, and Application |
Example:
<C: Remove Var = "Test2" Scope = "session"/> |
Delete the Test2 variable from the session.
2. Flow Control label
<C: If>
<C: If> labels have the following attributes:
Genus |
Description |
Required? |
Default Value |
Test |
The condition to be evaluated is equivalent to the condition in the IF (...) {} statement. |
Yes |
None |
VaR |
Variable name for storing the condition Result |
No |
None |
Scope |
Variable range for saving condition results |
No |
Page |
<C: Choose>
This tag does not accept any attributes.
<C: When>
<C: When> labels have the following attributes:
Genus |
Description |
Required? |
Default Value |
Test |
Conditions for evaluation |
Yes |
None |
<C: otherwise>
This tag does not accept any attributes.
Example:
<C: If test = "$ {user. Wealthy}"> User. Wealthy is true. </C: If> |
If the user. Wealthy value is true, the user. Wealthy is true.
<C: Choose> <C: When test = "$ {user. Generous}"> User. Generous is true. </C: When> <C: When test = "$ {user. stingy}"> User. stingy is true. </C: When> <C: otherwise> User. Generous and user. stingy are false. </C: otherwise> </C: Choose> |
User. Generous is true is displayed only when the return value of condition user. Generous is true.
User. stingy is true only when the return value of condition user. stingy is true.
All other cases (that is, the values of user. Generous and user. stingy are not true) show user. Generous and user. stingy are false.
Because jstl is not like if (){...} Else {...} Therefore, the statements in this form can only be completed using the <C: Choose>, <C: When>, and <C: otherwise> labels.
3. cyclic control labels
<C: foreach>
<C: foreach> A tag is used for common data and has the following attributes:
Genus |
Description |
Required? |
Default Value |
Items |
Cyclic Project |
No |
None |
Begin |
Start Condition |
No |
0 |
End |
End Condition |
No |
The last project in the collection. |
Step |
Step Size |
No |
1 |
VaR |
Variable name of the current project |
No |
None |
Varstatus |
Variable that shows the loop status |
No |
None |
Example:
<C: foreach items = "$ {vectors}" Var = "vector"> <C: Out value = "$ {vector}"/> </C: foreach> |
Equivalent to a Java statement
For (INT I = 0; I <vectors. Size (); I ++ ){ Out. println (vectors. Get (I )); } |
Vectors is a Java. util. Vector object that stores string data and vector is a string object in the current loop. In fact, vectors can be any object that implements the java. util. Collection interface.
<C: foreach begin = "0" End = "100" Var = "I" step = "1"> Count = <C: Out value = "$ {I}"/> <br> </C: foreach>
|
Output:
Count = 0
...
Count = 100
<C: fortokens>
<C: fortokens> tags have the following attributes:
Genus |
Description |
Required? |
Default Value |
Items |
Cyclic Project |
Yes |
None |
Delims |
Delimiter |
Yes |
None |
Begin |
Start Condition |
No |
0 |
End |
End Condition |
No |
The last project in the collection. |
Step |
Step Size |
No |
1 |
VaR |
Variable name of the current project |
No |
None |
Varstatus |
Variable that shows the loop status |
No |
None |
Example
<C: fortokens items = "A: B: C: D" delims = ":" Var = "token"> <C: Out value = "$ {token}"/> </C: fortokens>
|
This label is equivalent to the java. util. stringtokenizer class. Here, the string A: B: C: D is separated for four times and the token is the string to be split to the current one.
4. Import files and URLs
Jstl core tag Library supports <C: Import> to contain files, <C: URL> to print and format URLs, and <C: Redirect> to redirect URLs.
<C: Import>
<C: Import> the tag contains the code of another page to the current page. It has the following attributes:
Genus |
Description |
Required? |
Default Value |
URL |
URL of the page to be imported |
Yes |
None |
Context |
/Followed by the name of the Local Web Application |
No |
Current Application |
Charencoding |
Character set used to import data |
No |
ISO-8859-1 |
VaR |
Variable name that accepts the imported text |
No |
Page |
Scope |
Variable range of the input text |
No |
1 |
Varreader |
Java. Io. Reader variable name used to accept the imported text |
No |
None |
Varstatus |
Variable that shows the loop status |
No |
None |
<C: URL>
<C: URL> the tag outputs a URL address, which has the following attributes:
Genus |
Description |
Required? |
Default Value |
URL |
URL |
Yes |
None |
Context |
/Followed by the name of the Local Web Application |
No |
Current Application |
Charencoding |
Character set used to import data |
No |
ISO-8859-1 |
VaR |
The name of the URL variable that has been processed. The variable stores the URL |
No |
Output to page |
Scope |
Variable range of the variable name storing the URL |
No |
Page |
Example:
<C: Import url = "http://www.url.com/edit.js" Var = "newsfeed"/>
|
Add the URL http://www.url.com/edit.js to the front page and save the URL to the newsfeed.
<A href = "<C: URL url ="/index. jsp "/>"/>
|
Output <a href = "http://www.yourname.com/index.jsp"/> at the current location of the current page, where the http://www.yourname.com is the location of the current page.
<C: Redirect>
<C: Redirect> the tag redirects the request to another page. It has the following attributes:
Genus |
Description |
Required? |
Default Value |
URL |
URL |
Yes |
None |
Context |
/Followed by the name of the Local Web Application |
No |
Current Application |
Example:
<C: Redirect url = "http://www.yourname.com/login.jsp"/>
|
Redirect requests to http://www.yourname.com/login.jsppage, equivalent to response.setredirect ("http://www.yourname.com/login.jsp ");
<C: param>
<C: param> A tag is used to pass a parameter to a redirection or contain page. It has the following attributes:
Genus |
Description |
Required? |
Default Value |
Name |
Variable name set in the Request Parameter |
Yes |
None |
Value |
Variable value set in the Request Parameter |
No |
None |
Example:
<C: Redirect url = "login. jsp"> <C: Param name = "ID" value = "888"/> </C: Redirect>
|
Pass Parameter 888 with ID as the name to the login. jsp page, which is equivalent to login. jsp? Id = 888
Advantages of jstl
1. the same interface is provided between application servers, which increases the porting of Web applications between application servers.
2. Simplified JSP and web application development.
3. Reduce the number of scriptlet code in JSP in a unified way, and achieve programs without any scriptlet code. In our company's projects, no scriptlet code is allowed to appear in JSP.
4. Further integration of JSP design tools with Web application development is allowed. I believe there will soon be an IDE development tool supporting jstl.
Summary
The above is only a part of jstl. If I have time, I will continue to write other parts and share them with you. To use jstl, you must. jar and standard. put the JAR file in classpath. If you still need to use the XML processing and database access (SQL) Labels, you also need to put the relevant jar files in classpath, these jar files are all stored in the downloaded ZIP file. This zip file can be uploaded from.