A brief analysis of JSP standard TAG Library _jsp programming

Source: Internet
Author: User
Tags tld
introduction of Jstl
Introduction: In JSP and JavaBean, when we use loops in a Web page, or when we use object methods to connect to a database, we inevitably need to use the scripting elements of the JSP, embedded with a lot of Java code, now developers want to avoid using JSP scripting elements as much as possible, Further separating the application's display layer from the business layer, which is more beneficial to the application's division of labor, the JSP developer specifies JSTL, provides a unified set of common custom label files, and combines these files together to form a JSP standard tag library, the JSTL.
1) Definition: A set of standard tags that contain the authoring and development of JSP pages
A) Features: Provides a script-free environment for developers without the need to write Java scripts
b) contains
1. Core Tag Library
2. SQL Tag Library
3. Internationalization (i18n) and Format tag Library
4. XML Tag Library
c) Category: two versions
1. Use of EL
2. Using the request-time expression
2) function
A to further simplify development, simplify the page, the implementation of the page out of JSP script elements
b make the page appear as little Java code as possible
c) Advantages: Used with El expressions, you can use El instead of property values for further dynamic change
3) Use
A) Import tag libraries (this step is required in tools such as Eclipse)
(b) Implementation steps for using labels in tag libraries
I. Add the jstl corresponding jar file: Jstl.jar, Standard.jar to the application's Web-inf/lib directory (these two jar files are shown in the courseware catalog "Jstl Tag Library")
Ii. Specify the description information for the tag library in the JSP page that is written:
Syntax: <%@ taglib prefix= "C" uri= "Http://java.sun.com/jstl/core_rt"%>
Extensions for the use of the above syntax:
<%@ taglib prefix = "label alias" uri = "Location of Tag Library" >
1. Label alias commonly used C
2. Location of the tag library: (That is, the setting of the URI)
A The textbook: "Http://java.sun.com/jstl/core_rt" points to the address of the library file on Sun's website, note that this is just an address point, even if there is no internet access, The path in the project has also been mapped to in the tool (note: The two jar files in the tag library are now placed under the "Engineering \web-inf\lib" directory.) )
b) can also be used: "/web-inf/c.tld" (but note: At this point, the tag library two jar files in a Standard.jar file after the unpacked directory Meta-inf all *.tld files into the "project name/web-inf/" directory, That is to ensure that there are c.tld files in the directory)
Note: The above two ways to import the tag library, the first is better, that is, only two tag library jar file into the "Engineering \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, detailed below:
Second, core tag library-----cores or c.tld
A) Categories:
1. Generic Tags: manipulating the range variables created by JSP pages
2. Condition Label: Operation condition operation judgment and processing
3. Cyclic labeling: Operation Loop Operations
b) Generic label
1. Set, delete, display variable value or object value created in JSP page
2. The scope of the variable is determined by the scope property at the time of setting
3. <c:set> to set the value of a variable or object property
<c:set var= "variable name" value= "Variable Value" scope= "Scope range"/>
<c:set target= "Object Name" property= "property name" value= "value"/>
: NOTE: "Variable value" can be an El expression
4. <c:out> for dynamic display of data (function similar to EL)
1:<c:out value= "expression" default= "Default value" Escapexml= "True/false" >
2: Note: The expression can be 1 of the normal constant value
2 Output The variable value already set: ${variable's name}
5. <c:remove> to delete the created variable
A) <c:remove var= "variable's name" scope = scope >
(Case: Demo above several tags)
c) Condition label
1. Provide conditional operation, two kinds: if choose
2. <c:if> Grammar
A) <c:if test= "condition" var= "the variable that holds the result of the test condition" scope= scope >
b) .... Conditional body
c) </c:if>
D) var stores is: The result is true or false
e) can not implement else by the following implementation
3. <c:choose> Grammar
A) <c:choose>
I. <c:when test= "conditional expression 1" >
Ii. statements to be processed 1
Iii. </c:when>
Iv. <c:when test= "conditional expression 2" >
V. Statements to be processed 2
VI. </c:when>
Vii. <c:otherwise>
Viii. statements to be processed
Ix. </c:otherwise>
b) </c:choose>
(Case three.jsp)
D) Iterative Labels
1. Two types: ForEach fortokens
2. ForEach
Simple to use
<c:foreach var= "loop control variable" test= "cyclic condition" begin= "cyclic start value" end= "Loop End Value" varstatus= "Save loop state variable" >
Circulation body
</forEach>
Access to the collection object
<c:foreach var= "The start index position of the collection name" Begin= "collection where the current record of the collection" items= "is to be followed" end= "the end position of the collection" >
</forEach>
Note: If not set begin,end will retrieve all elements, step does not set default to 1
3. Fortokens (differs from <c:forEach> for round-robin a string)
<c:fortokens items= "to round the string" delims= "the word" var= "," the variable that holds the word ">
</c:forTokens>
(Case: Demo above several tags)
three, SQL tag library----sql.tld
The General Enterprise class application is inseparable from the database operation, there are many times when you need to access a database in a JSP page, so providing support for database access becomes very important, and the SQL tag library in Jstl provides a set of tags that can access, query, update, and so on. Great convenience in the JSP page database access
1, role: For the operation of the database
2. Add tag Library to JSP page
<%@ taglib uri= "http://java.sun.com/jstl/sql_rt" prefix= "SQL"%>
3, the specific use of the mark
A) setting up a data source <sql:setDataSource>
Specific usage: <sql:setdatasource driver= "Driver Classpath" url= "Connected Database Settings" user= "username" password= "password" var= "Connection object name"/>
b) Query Data <sql:query>
Specific usage:
①<sql:query sql= "query statement" var= result set name scope= result set the range "datasource=" associated with the data source object "maxrows=" Maximum number of rows "startrow=" The index of the start row/>
②<sql:query var= "Result set name" scope= "Save Scope" >
SELECT statement
</sql:query>
Result set obtained by operation
<sql:query var= "Students" scope= "session" sql= "select * from Stu" datasource= "conn"/>
${students.rowcount} can return the number of data rows in the result set
${students.columnnames} to return a collection of all fields
${students.rowbyindex} returns a collection of data for a row in the result set
c) Data Update <sql:update>
Specific usage:
①<sql:update sql= "SQL statement" datasource= "Data source object name"/>
②<sql:update datasource= "Data source object Name" >
SQL statement
</sql:query>
d) Transaction Applications <sql:transaction>
Specific usage:
<sql:transaction datasource= "Data source object Name" isolation= "Transaction isolation Level" >
<sql:query> or <sql:update>
</sql:transaction>
The isolation level above:
Read_committed,read_uncommited,repeatable,serializable
Note: If you set the DataSource property in a transaction, you do not need to set it inside the tag.
e) Parameter Settings <sql:param>
① function: The setting of parameters when making queries or data manipulation
② use: Usually as a child tag for <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, the role: the number and date time output standardization, solve the Chinese garbled problem
2. Add tag Library to JSP page
<%@ taglib uri= "Http://java.sun.com/jstl/fmt_rt" prefix= "FMT"%>
3, the use of specific labels
A) <fmt:setLocale>
① function: for JSP page localization
② use: <fmt:setlocale value= "language and area"/>
b) <fmt:formatNumber>
① function: The format of JSP page data
② use: <fmt:formatnumber type= "number|currency|percent" value= "data to be formatted"/>
c) <fmt:formatDate>
① function: Format for JSP page dates
② use: <fmt:formatdate type= "Time|date|both" value= "date data to be formatted"/>
D) <fmt:requestEncoding>
① function: Specifies how the requested data is encoded
② use: <fmt:requestencodding value= "Encoding"/>
For example: <fmt:requestencoding value= "gb2312"/>
In general, you can also use this:
A to create a good property file, such as Len_zh. Properties
b <fmt:setlocale value= "to the name of the file en" > (en for Chinese, en for English, fixed, Y2 course will be mentioned.) )
c) <fmt:setbundle basename= "file name len" > (len is from name)
D <fmt:message key= "keywords in the properties file" > (property File is Len_zh. properties)
Attention: When internationalized, *.properties files 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.