Starting with the JSP 1.1 specification, JSP supports the use of custom tags in the JSP, and the widespread use of custom tags has caused the programmer to repeat the definition, which contributed to the birth of Jstl (JavaServer Pages Standard Tag Library).
Because the work needs to use JSTL, but the internet is struggling to find the Chinese information about Jstl, so there is this article.
JSTL Introduction
Jstl is a constantly improving open source JSP tag Library, which is maintained by the Apache Jakarta team. Jstl can only be run on containers that support JSP1.2 and Servlet2.3 specifications, such as Tomcat 4.x. However, the upcoming JSP 2.0 is supported as a standard.
The latest version of Jstl is 1.02, and the final release is 1.0. The jstl contains two parts: the tag library and the El (Expression Language expression language) language. The tag library currently supports four kinds of tags: sample tag URI prefix
Core
Http://java.sun.com/jstl/coreC <c:tagname ...>
XML processing
Http://java.sun.com/jstl/xmlX <x:tagname ...>
i18n capable formatting
http://java.sun.com/jstl/fmtFMT <fmt:tagname ...>
Database Access (SQL)
Http://java.sun.com/jstl/sqlSQL <sql:tagname ...>
Core supports some basic operations in JSP;
XML processing supports the processing of XML documents;
I18N capable formatting supports the internationalization of JSP pages;
Database access (SQL) supports JSP operations on databases.
Because of my limited level, this article only introduces the core tag, if interested, can explore the other three kinds of labels for use and expansion.
El Language Introduction
The El language is the Jstl output (input) representation of a Java expression.
In Jstl, the El language can only be used in attribute values. The El language can only be invoked by establishing an expression ${EXP1}. There are three ways to use an expression in an attribute value.
1, the Value property contains an expression
<some:tag value= "${expr}"/>
In this case, the expression value is computed and assigned to the Value property based on the type conversion rule. For example: <c:out value= "${username}"/> ${username} is an EL, which is equivalent to the JSP statement <%=request.getattribute ("username")%> or <%=session.getattribute ("username")%>
2. The Value property contains one or more properties that are split by text or surround
<some:tag value= "some${expr}${expr}text${expr}"/>
In this case, the expression is evaluated from left to right and the result is converted to a string type (based on the type conversion rule) and the result is assigned to the Value property
3, the Value property contains only text
<some:tag value= "Sometext"/>
In this case, the string property value is converted to the type that the label expects based on the type conversion rule.
Operator for El language
Gets the value of an attribute in an object or collection
To get the properties in the collection, El supports the following two actions
1. Use the. operator to obtain a named attribute. For example, an expression ${user.username} indicates the Username property of the object user
2. Use the [] operator to obtain a name or numeric attribute.
Expression ${user["username"]} has the same meaning as expression ${user. Username}
Expression ${row[0]} indicates the first entry of the row collection.
Here the user is an object of a class whose attribute username must conform to the standard JavaBean specification, that is, the appropriate getter and setter method must be defined for the Username property.
Empty operator (null value check)
Use the empty operator to determine whether an object, collection, or string variable is null or NULL. For example:
${empty Param.username}
If the username value in the parameter list for the request is null, the value of the expression is true. El can also be compared directly with NULL using the comparison operator. such as ${param.firstname = = null}.
Comparison operator operator description
= = or EQ equality check
!= or NE unequal check
< or LT is less than check
> or GT is larger than check
<= or le is less than equal to check
>= or GE is greater than or equal to check
Numeric operators and logical operators are the same as the Java language and are no longer listed.
Core Tag Library
1. General Label
<c:out>
The <c:out> tab is used to display data in a JSP that has the following property properties that describe whether a default value is required
Value output information that can be an El expression or a constant is none
Displays no information if default value is empty
EscapeXML is true to avoid the special XML character set no true
Example: Your username is: <c:out value= "${user.username}" default= "Guest"/>
Displays the user's user name, if empty, displays the guest
<c:out value= "${sessionscope.username}"/>
Specifies that the value of the username is displayed from the session;
<c:out value= "${username}"/>
Displays the value of the username, by default, from request (page), if no object named username is taken from the session, and no session is from application (ServletContext) and is not displayed if no value is taken.
<c:set>
The <c:set> tab is used to save the data, which has the following property properties that describe whether a default value is required
Value the information to be saved can be an El expression or a constant no
Target needs to modify the variable name of the property, typically the JavaBean instance does not have
property that needs to be modified is not JavaBean
var needs to save information on variable No
Scope of the variable that holds information for scope 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"/>
Saves the value of the Test.testinfo to the test2 of the session, where test is an instance of JavaBean, and TestInfo is the property of the test object.
<c:set target= "${cust.address}" property= "City" value= "${city}"/>
To save the city property value of an object cust.address in the variable city
<c:remove>
The <c:remove> label is used to delete data, and it has the following property properties to describe whether a default value is required
var to delete the variable is None
Scope of the deleted variable is scoped to all scopes, including page, request, session, application, etc.
The <c:if> label has the following property properties to describe whether the default value must be
Test needs to evaluate the condition, equivalent to if (...) {} The condition in the statement is None
var requires that the variable name of the conditional result be saved without
Scope of the variable to hold the condition result is no page
<c:choose>
This label does not accept any attributes
<c:when>
The <c:when> label has the following property properties to describe whether the default value must be
Test needs to evaluate the condition is no
<c:otherwise>
This label also does not accept any attributes
Example: <c:if test= "${user.wealthy}" >
User.wealthy is true.
</c:if>
Displays User.wealthy is true if the User.wealthy value 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>
The user.generous is true only appears if the condition user.generous return value is true.
The user.stingy is true only appears if the condition User.stingy return value is true.
All other cases (that is, user.generous and user.stingy values are not true) show all user.generous and User.stingy are false.
Because Jstl has no shape like if () {...}} else {...} , so this form of statement can only be done in conjunction with the <c:choose>, <c:when> and <c:otherwise> tags.
3. Cyclic control label
<c:forEach>
The <c:forEach> label is used for common data, and it has the following property properties to describe whether a default value is required
Items that are recycled are not
Begin Condition No 0
End ending condition no last item in the collection
Step Steps No 1
var represents the variable name of the current project No
Varstatus show the loop state of the variable No
Equivalent to the Java statement for (int i=0;i<vectors.size (); i++) {
Out.println (Vectors.get (i));
}
Here vectors is a Java.util.Vector object that contains string data, and vector is the string object under the current loop condition. In fact, the vectors here can be any object that implements the Java.util. Collection interface.
<c:foreach begin= "0" end= "var=" "I" step= "1" >
Count=<c:out value= "${i}"/><br>
</c:forEach>
Output:
Count=0
...
count=100
<c:forTokens>
The <c:forTokens> label has the following property properties to describe whether the default value must be
Items to be recycled are none
Delims delimiters are None
Begin Condition No 0
End ending condition no last item in the collection
Step Steps No 1
var represents the variable name of the current project No
Varstatus show the loop state of the variable No
The use of this label is equivalent to the Java.util.StringTokenizer class. Here, a:b:c:d the string to: loop four times, token is looping to the currently split string.
4. import files and URLs
The JSTL core tag library supports using <c:import> to include files, using <c:url> to print and format URLs, and using <c:redirect> to redirect URLs.
<c:import>
The <c:import> tag contains another page code to the current page, which has the following property properties that describe whether the default value must be
URL is required to import the page URL is none
Context/followed by the name of the local Web application no current application
Charencoding the character set used to import data iso-8859-1
VAR accepts variable name of imported text no page
Scope of variable to accept imported text is No 1
Varreader the Java.io.Reader variable name used to accept imported text is not
Varstatus show the loop state of the variable No
<c:url>
The <c:url> label outputs a URL address that has the following attribute properties that describe whether the default value must be
URL URL address is None
Context/followed by the name of the local Web application no current application
Charencoding the character set used to import data iso-8859-1
var accepts the processed URL variable name, which stores the URL no output to the page
Scope of the variable name of the variable that stores the URL is no page
Include the URL http://www.url.com/edit.js to the current location of the current page and save the URL to the newsfeed variable
<a href= "<c:url url="/index.jsp "/>"/>
Output <a href= "http://www.yourname.com/index.jsp"/>,http://www.yourname.com is the current page location at the current page.
<c:redirect>
<c:redirect> tag redirects the request to another page, which has the following property properties that describe whether the default value must be
URL URL address is None
Context/followed by the name of the local Web application no current application
Redirect request to http://www.yourname.com/login.jsp page, equivalent to Response.setredirect ("http://www.yourname.com/login.jsp");
<c:param>
The <c:param> label is used to pass parameters to a redirect or include page that has the following property properties that describe whether the default value must be
Name the variable name that is set in the request parameter is None
The value of the variable set in the request parameter is not
Pass parameter 888 to the LOGIN.JSP page as ID, equivalent to login.jsp?id=888
Advantages of Jstl
1. Provides a consistent interface between application servers, maximizing the porting of Web applications between application servers.
2. Simplifies the development of JSP and Web applications.
3, in a unified way to reduce the number of scriptlet code in the JSP, you can reach the program without any scriptlet code. In our company's project, no scriptlet code is allowed to appear in the JSP.
4. Allows further integration of JSP design tools with Web application development. It is believed that there will soon be an IDE development tool that supports JSTL.
Summarize
The above is only part of the JSTL, and if I have time I will continue to write the other parts to share with you. If you want to use JSTL, you must put Jstl.jar and Standard.jar files in Classpath, and if you also need to use XML processing and database access (SQL) tags, Also put the relevant jar files into Classpath, all of which are in the downloaded zip file. This zip file can be downloaded from the Http://jakarta.apache.org/builds/jakarta-taglibs/releases/standard/jakarta-taglibs-standard-1.0.zip.
Resources
1, http://java.sun.com/products/jsp/jstl/
Sun Company's Jstl site
2, http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html
Jakarta Group's Jstl site
3, Http://www.manning.com/bayern/appendixA.pdf
Jstl's reference documentation, many of which are translated from this PDF file.
4, <<J2EE Programming Guide (1.3 Edition) > >
Introduced the embryonic form of Jstl, Wrox's books are fine.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.