JSTL Tag Library Detailed information

Source: Internet
Author: User
Tags control label i18n logical operators tagname variable scope

HTML clipboard STL Tag Library detailed information preface

starting with the JSP 1.1 specification, JSP supports the use of custom tags in jsp. The widespread use of custom labels has created a duplication of definitions for programmers, which has contributed to the birth of Jstl (JavaServer Pages Standard Tag Library). Author: Dingling (http:///www.DingL.com/)

jstl Introduction

Jstl is a constantly improving open source JSP tag library that 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 the
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:

Label 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 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 & Lt;%=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 Describe
= = or EQ Equality check
!= or NE Unequal check
< or LT Less than check
> or GT Greater than check
<= or Le Less than equals check
>= or GE Greater than equals 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 the JSP, which has the following properties
Property Description Whether you must Default value
Value The output information, which can be an El expression or a constant Is No
Default Display information when value is empty Whether No
EscapeXML True to avoid special XML character sets Whether 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>
<c:set> tags are used to save data, which has the following properties
Property Description Whether you must Default value
Value The information to be saved can be an El expression or a constant Whether
Target Variable name that needs to be modified for the property, typically an instance of JavaBean Whether No
Property JavaBean properties that need to be modified Whether No
Var Variables that require information to be saved Whether No
Scope The scope of the variable that holds the information Whether 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>
<c:remove> tags are used to delete data, which has the following properties
Property Description Whether you must Default value
Var The variable to delete Is No
Scope The scope of the variable being deleted Whether All ranges, including page, request, session, application, etc.

Example:
<c:remove var= "Test2" scope= "Session"/>

Deletes the TEST2 variable from the session.

2. Flow control label

<c:if>

The <c:if> label has the following properties
Property Description Whether you must Default value
Test Conditions to be evaluated, equivalent to if (...) The condition in the {} statement Is No
Var Variable name that requires the condition result to be saved Whether No
Scope To save a variable range of conditional results Whether Page


<c:choose>
This label does not accept any attributes

<c:when>
The <c:when> label has the following properties
Property Description Whether you must Default value
Test Conditions that need to be evaluated 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>
<c:forEach> tags are used for common data, which has the following properties
Property Description Whether you must Default value
Items The project to cycle through Whether No
Begin Start condition Whether 0
End End condition Whether The last item in the collection
Step Step Whether 1
Var Represents the variable name of the current project Whether No
Varstatus Variable showing the loop state Whether No


Example:
<c:foreach items= "${vectors}" var= "vector" >
<c:out value= "${vector}"/>
</c:forEach>

Equivalent to Java statements
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 properties
Property Description Whether you must Default value
Items The project to cycle through Is No
Delims Split character Is No
Begin Start condition Whether 0
End End condition Whether The last item in the collection
Step Step Whether 1
Var Represents the variable name of the current project Whether No
Varstatus Variable showing the loop state Whether No


Example

<c:fortokens items= "A:b:c:d" delims= ":" var= "token" >
<c:out value= "${token}"/>
</c:forTokens>

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 properties Td>scope
Property Description must default
URL the URL where the page needs to be imported Yes none
context /followed by the name of the local Web application no The current application
charencoding The character set used to import data no iso-8859-1
var accept the imported text variable name no page
The variable scope of the variable that accepts the imported text no 1
varreader java.io.Reader variable name to accept imported text no no
varstatus Variable that shows the loop state no no

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.