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. 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 description
= Or EQ equal check
! = Or NE check
<Or lt less than check
> Or GT is greater than check
<= Or Le is less than or equal to check
> = Or GE is greater than or equal to the 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> the label is used to display data in JSP. It has the following attribute Attributions to indicate whether the default value is required.
The information output by value. It can be an El expression or a constant that does not exist.
If the default value is null, no information is displayed.
If escapexml is true, the special XML character set is not true.
Example: your user name 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 attribute Attributions to indicate whether the default value is required.
The information to be saved by the value, which can be an El expression or a constant no
Target: The variable name of the attribute to be modified. Generally, the variable name is not available for the JavaBean instance.
The JavaBean attribute to be modified by the property does not exist
None of the variables var needs to save information
Scope: the range of variables in which information is saved is 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 attribute attributes to describe whether the default value is required.
The variable to be deleted by VAR is none.
Scope: The scope of the deleted variable. Optional values: 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> the tag has the following attribute attributes to describe whether the default value is required.
The condition to be evaluated by test is equivalent to that in the IF (...) {} statement
VaR requires that the variable name that saves the condition result be none
Scope: variable range of the stored condition Result No page
<C: Choose>
This tag does not accept any attributes.
<C: When>
<C: When> the tag has the following attribute attributes to describe whether the default value is required.
Test must be evaluated on the condition that 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. It has the following attribute Attributions to indicate whether the default value is required.
No items in the items Loop
Begin start condition no 0
End end condition no last project in the Set
Step No 1
VaR indicates whether the variable name of the current project is none
Varstatus: whether the variable that shows the cyclic status is none
Example: <C: foreach items = "$ {vectors}" Var = "vector">
<C: Out value = "$ {vector}"/>
</C: foreach>
Equivalent to the 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> A tag has the following attributes to describe whether the default value is required.
Items that are cyclic by items are none
Delims Delimiter is none
Begin start condition no 0
End end condition no last project in the Set
Step No 1
VaR indicates whether the variable name of the current project is none
Varstatus: whether the variable that shows the cyclic status is 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 attribute Attributions to indicate whether the default value is required.
The URL that needs to be imported to the page is none
Context/followed by the name of the Local Web Application No current application
Charencoding character set no ISO-8859-1 for Data Import
VaR accepts the imported text variable name no page
Scope: The variable range of the variable that accepts the imported text is No 1.
Varreader: the java. Io. Reader variable used to accept the imported text; no
Varstatus: whether the variable that shows the cyclic status is none
<C: URL>
<C: URL> the tag outputs a URL address, which has the following attribute Attributions to indicate whether the default value is required.
The URL address is none.
Context/followed by the name of the Local Web Application No current application
Charencoding character set no ISO-8859-1 for Data Import
VaR accepts the processed URL variable name. This variable stores whether the URL is output to the page.
Scope: The variable range of the variable name storing the URL is 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 attribute Attributions to indicate whether the default value is required.
The URL address is 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 page. It has the following attribute Attributions to indicate whether the default value is required.
The variable name set in the request parameter is none.
Whether the variable value set by value in the request parameter is null
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.
References
1. http://java.sun.com/products/jsp/jstl/
Sun's jstl site
2. http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html
Jstl site of the Jakarta team
3. http://www.manning.com/bayern/appendixA.pdf
Jstl references. Many of the content in this article is translated from this PDF file.
4. <J2EE Programming Guide (version 1.3)>
Introduced the prototype of jstl. wrox books are excellent products.