JSP/Servlet/JSF: JSTL (JSP standard tag Library) Introduction

Source: Internet
Author: User
Tags control label

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
XML processing http://java.sun.com/jstl/xml x
I18N capable formatting http://java.sun.com/jstl/fmt (fmt)
Database access http://java.sun.com/jstl/ SQL SQL


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.

In this case, the expression value is calculated and assigned to the value attribute according to the type conversion rule. For example: $ {Username} In is an EL, which is equivalent to the 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

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

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


The label is used to display data in JSP. It has the following attribute attributes to describe 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:  

Displays the user name. If it is null, guest is displayed.
 

Display the username value obtained from the session;
 

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.


A tag is used to save data. It has the following attribute attributes to describe 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:  

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.
 

Save the city property value of the object cust. address to the variable city.


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:  

Delete the test2 variable from the session.

2. Flow Control label



The tag has the following attribute attributes to indicate 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



This tag does not accept any attributes.


The tag has the following attribute Attributions to describe whether the default value is required
Test must be evaluated on the condition that none



This tag does not accept any attributes.

Example:
User. wealthy is true.
 

If the user. wealthy value is true, the user. wealthy is true.



User. generous is true.
 

User. stingy is true.


User. generous and user. stingy are false.

 

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 {...} So this form of statements can only use , And Tag.

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.