Analysis of jsp standard tag Library

Source: Internet
Author: User
Tags tld

1. Introduction to JSTL
Introduction: in JSP and javaBean, when we use loops on the Web page or use object methods to connect to the database, we need to use jsp script elements inevitably, there is a lot of java code embedded in it. Now developers want to avoid using jsp script to compile elements as much as possible, and further completely separate the display layer and business layer of the application, which is more conducive to the division of labor and collaboration of the application, jsp developers specify JSTL, provide a unified set of universal custom tag files, and combine these files to form a jsp standard tag library, that is, JSTL.
1) Definition: contains a set of standard labels for compiling and developing JSP pages
A) features: it provides developers with a script-free environment without the need to write JAVA scripts.
B) Include
1. Core tag Library
2. SQL tag Library
3. International (I18N) and formatted tag Library
4. XML tag Library
C) classification: Two Versions
1. Use EL
2. Request expression
2) Role
A) further simplify development, simplify pages, and remove pages from jsp script elements
B) make java code appear as few pages as possible
C) Advantage: it can be used with EL expressions to replace attribute values for further dynamic changes.
3) Use
A) import the tag Library (this step is required in Eclipse and other tools)
B) Steps for using tags in the tag Library
I. Add JSTL JAR files: jstl. jar and standard. jar to the WEB-INF/lib directory of the application (see the courseware directory "JSTL label library" for the jar files ")
Ii. Specify the tag library description on the compiled jsp page:
Syntax: <% @ taglib prefix = "c" uri = "http://java.sun.com/jstl/core_rt" %>
The following is an extension of the above Syntax:
<% @ Taglib prefix = "tag alias" uri = "tag library location">
1. Commonly Used label alias c
2. Location of the tag Library: (set the uri)
A) on the teaching material: "http://java.sun.com/jstl/core_rt" pointing to the library file address on the sun Company website, note that here is only an address pointing to, even if there is no Internet, the tool also maps to the path in the project (Note: first put the two jar files of the tag library under the "Project \ WEB-INF \ lib" directory .)
B) can also be used: "/WEB-INF/c. tld "(but note: in this case, you must first set a standard in the two jar files of the tag library. jar files decompressed directory META-INF all *. tld files are placed in the "Project name/WEB-INF/" directory, that is, ensure that the directory has c. tld files)
Note: The two ways to import the label library, the first is better, that is, you only need to put the two label library jar files under the "Project \ WEB-INF \ lib" directory.
C) use in JSP
3. <c: xxxxxx property name = "property value" property name = "property value">
4. xxxxx is a variety of functions provided in the tag library. The details are as follows:
Ii. core tag library ----- core or c. tld
A) category:
1. Common Tag: operate the range variable created on the JSP page
2. Condition Tag: Operation Condition Calculation judgment and Processing
3. Cyclic Tag: Operation cyclic Operation
B) General labels
1. Set, delete, and display the variable value or object value created on the JSP page.
2. The scope of the variable is determined by the scope attribute during the setting.
3. <c: set> is used to set the value of a variable or object attribute.
<C: set var = "variable name" value = "variable value" scope = "scope"/>
<C: set target = "Object Name" property = "property name" value = "value"/>
: Note: The "variable value" can be an EL expression.
4. <c: out> used to dynamically display data (similar to EL)
1: <c: out value = "expression" default = "default value" escapeXml = "true/false">
2: Note: The expression can be a normal constant value of 1.
2. output the set variable value: $ {variable name}
5. <c: remove> used to delete the created variable
A) <c: remove var = "variable name" scope = "scope">
(Case: demonstrate the labels above)
C) condition tag
1. provides conditional operations, two types: if choose
2. <c: if> syntax
A) <c: if test = "condition" var = "variable storing test condition results" scope = "scope">
B )....... Condition body
C) </c: if>
D) What var stores is: The result is true or false.
E) else cannot be implemented.
3. <c: choose> syntax
A) <c: choose>
I. <c: when test = "conditional expression 1">
Ii. Statement 1 to be processed
Iii. </c: when>
Iv. <c: when test = "conditional expression 2">
V. Statement 2 to be processed
Vi. </c: when>
Vii. <c: otherwise>
Viii. Statements to be processed
Ix. </c: otherwise>
B) </c: choose>
(Case study three. jsp)
D) iteration tag
1. There are two types: forEach forTokens
2. forEach
Simple Application
<C: forEach var = "cyclic control variable" test = "cyclic condition" begin = "cyclic start value" end = "cyclic end value" varStatus = "variable for saving cyclic status">
Loop body
</ForEach>
Used to access collection objects
<C: forEach var = "variable for storing the current record of the Set" items = "Name of the set to be rotated" begin = "Start index position of the Set" end = "end position of the Set">
</ForEach>
NOTE: If begin is not set, end will retrieve all elements. If step is not set, the default value is 1.
3. forTokens (different from <c: forEach>, used to round a string)
<C: forTokens items = "the string to be round-robin" delims = "the word delimiter" var = "the variable to store the word">
</C: forTokens>
(Case: demonstrate the labels above)
Iii. SQL tag library ---- SQL. tld
Generally, enterprise-level applications cannot do without database operations. In many cases, you need to access the database on the JSP page. Therefore, it is very important to provide support for database access, the SQL tag library in JSTL provides a set of tags for database access, query, update, and other operations, which greatly facilitates database access on JSP pages.
1. Role: used for database operations
2. Add the tag Library to the JSP page
<% @ Taglib uri = http://java.sun.com/jstl/ SQL _rt prefix = "SQL" %>
3. Use tags
A) set the data source <SQL: setDataSource>
Usage: <SQL: setDataSource driver = "driver path" url = "connected database Settings" user = "username" password = "password" var = "connection object name"/>
B) query data <SQL: query>
Usage:
① <SQL: query SQL = "query statement" var = "result set name" scope = "result set storage range" dataSource = "associated data source object" maxRows = "Maximum number of rows" startRow = "row Index "/>
② <SQL: query var = "result set name" scope = "Save range">
Select statement
</SQL: query>
Result set obtained by the Operation
<SQL: query var = "students" scope = "session" SQL = "select * from stu" dataSource = "conn"/>
$ {Students. rowCount} indicates the number of rows in the returned result set.
$ {Students. columnNames} returns a set of all fields.
$ {Students. rowByIndex} returns the data set of a row in the result set.
C) data update <SQL: update>
Usage:
① <SQL: update SQL = "SQL statement" dataSource = "data source object name"/>
② <SQL: update dataSource = "Name of the data source object">
SQL statement
</SQL: query>
D) transaction application <SQL: transaction>
Usage:
<SQL: transaction dataSource = "data source object name" isolation = "transaction isolation level">
<SQL: query> or <SQL: update>
</SQL: transaction>
The preceding isolation level:
Read_committed, read_uncommited, repeatable, serializable
Note: If the dataSource attribute is set in the transaction, you do not need to set it in the tag.
E) parameter settings <SQL: param>
① Purpose: to set parameters for queries or data operations
② Usage: it is usually used as a sub-tag of the <SQL: query> and <SQL: update> tags
Case:
<SQL: query dataSource = "conn" var = "stus" scope = "page">
Select * from student where stuage>?
<SQL: param value = "parameter value"/>
</SQL: query>
Iv. Internationalization and formatting tags ---- fmt or fmt. tld
1. Role: standardize the output of numbers and date and time to solve Chinese garbled characters
2. Add a tag Library to the jsp page
<% @ Taglib uri = "http://java.sun.com/jstl/fmt_rt" prefix =" fmt "%>
3. Use of specific labels
A) <fmt: setLocale>
① Role: used for JSP page Localization
② Usage: <fmt: setLocale value = "language and Region"/>
B) <fmt: formatNumber>
1 Role: used to format JSP page data
② Use: <fmt: formatNumber type = "number | currency | percent" value = "data to be formatted"/>
C) <fmt: formatDate>
1 Role: used for formatting JSP page dates
② Use: <fmt: formatDate type = "time | date | both" value = "date data to be formatted"/>
D) <fmt: requestEncoding>
① Purpose: Specify the encoding method of request data
② Use: <fmt: requestEncodding value = "encoding method"/>
Example: <fmt: requestEncoding value = "gb2312"/>
Generally, you can use the following method:
A) create an attribute file, such as len_zh. properties.
B) <fmt: setLocale value = "indicates the file name zh"> (zh is Chinese, en is English, fixed, and will be discussed in the Y2 course .)
C) <fmt: setBundle basename = "file name len"> (len is its own name)
D) <fmt: message key = "keyword in the property file"> (the property file is len_zh. properties)
Note:: When internationalization is enabled, the *. properties file should be placed under the classes directory.
Related Article

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.