Analysis of JSP standard tag library


1、 Introduction to JSTL
Introduction: in JSP and JavaBean, when we use loops in web pages or use object methods to connect databases, we inevitably need to use JSP scripting elements, which are embedded with a lot of Java code. Now developers want to avoid using JSP scripting elements as much as possible, and further separate the display layer and business layer of the application, It is more conducive to the division and cooperation of applications. JSP developers specify JSTL, provide a set of unified and common custom tag files, and combine these files to form a JSP standard tag library, namely JSTL.
1) Definition: contains a set of standard tags for writing and developing JSP pages
a) Features: it provides a script free environment for developers and does not need to write java scripts
b) Include
1. Core tag library
2. SQL tag library
3. International (I18N) and formatted label Library
4. XML tag library
c) Classification: two versions
1. Use el
2. Use request time expression
2) Function
a) Further simplify the development, simplify the page, realize the page from JSP script elements
b) Make the page appear as few java code as possible
c) Advantage: with El expression, you can use El instead of attribute value to further change dynamically
3) Use
a) Import tag library (need to do this step in eclipse and other tools)
b) Implementation steps of using tags in tag library
i. Add the jar files corresponding to JSTL: jstl.jar and standard.jar to the WEB-INF / lib directory of the application (see the courseware directory “JSTL tag library” for these two jar files)
II. Specify the description information of the tag library in the JSP page:
Syntax: <% @ taglib prefix = “C” URI = ” rt” %>
On the extension of the above syntax usage:
<% @ taglib prefix = “tag alias” URI = “location of tag library” >
1. Label alias C
2. Location of tag library: (setting of URI)
a) In the textbook: RT “point to the address of the library file on Sun company’s website. Note that this is only an address. Even if you don’t surf the Internet, you have already mapped to the path in the project in the tool. (Note: at this time, you should first put the two jar files of the tag library into the” project web-inf-lib “directory.)
b) You can also use / WEB-INF / c.tld (Note: at this time, all *. TLD files in meta-inf after decompressing a standard.jar file in two jar files of the tag library should be put into the directory of “project name / WEB-INF /”, that is, to ensure that there is a c.tld file in this directory)
Note: the first of the two ways to import the tag library is better, that is, you only need to put the two tag library jar files into the directory of “project / WEB-INF / lib”.
c) Using in JSP
3. < C: XXXXXX attribute name = “attribute value” attribute name = “attribute value” >
4. Xxxxx is the various functions provided in the tag library, which are detailed as follows:
2、 Core tag library core or c.tld
a) Classification:
1. General tag: operate the scope variable created by JSP page
2. Condition label: operation condition operation judgment and processing
3. Loop label: operation loop operation
b) General label
1. Set, delete and display the variable value or object value created in JSP page
2. The scope of the variable is determined by the scope attribute
3. < C: set > is used to set the value of 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: 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 a normal constant value
2》 Output the set variable value: ${variable name}
5. < C: remove > is used to delete the created variables
a) < C: remove var = “name of variable” scope = “scope” >
(case: demonstrate the above labels)
c) Condition label
1. Provide two types of conditional operation: if choose
2. < C: if > syntax
a) < C: if test = “condition” var = “variable storing test condition results” scope = “scope” >
b) 。。。。。。。 Conditional body
c) </c:if>
d) Var stores: the result is true or false
e) Unable to implement else by
3. < C: choose > grammar
a) <c:choose>
i. < C: when test = “conditional expression 1” >
II. Statement to be processed 1
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 three. JSP)
d) Iteration label
1. There are two kinds: foreach fortokens
2. forEach
Simple application
< C: foreach var = loop control variable test = loop condition begin = loop start value end = loop end value varstatus = variable to save loop state >
Circulatory body
Access for collection objects
< C: foreach var = “variable storing the current record of the collection” items = “name of the collection to be rotated” begin = “start index position of the collection” end = “end position of the collection” >
Note: if begin is not set, end will retrieve all the elements, and step is not set. The default value is 1
3. Fortokens (different from < C: foreach >, used to round a string)
< C: fortokens items = “string to rotate” delims = “word separator” var = “variable to store word” >
(case: demonstrate the above labels)
3、 SQL Tag Library — sql.tld
General enterprise application is inseparable from the database operation, in many cases need to access the database in the JSP page, so it is very important to provide support for database access. The SQL tag library in JSTL provides a set of tags that can access, query, update and other operations on the database, which greatly facilitates the database access in the JSP page
1. Function: used for database operation
2. Add tag library in JSP page
<%@ taglib uri=“ ” prefix=”sql” %>
3. Specific use mark
a) Set data source< sql:setDataSource >
Specific usage:< sql:setDataSource driver= ”Drive class path “url =” connected database settings “user =” user name “password =” password “var =” connection object name “/ > the
b) Query data< sql:query >
Specific usage:
① < sql:query sql= ”Query statement “var =” result set name “scope =” result set save range “datasource =” associated data source object “maxRows =” maximum number of rows “startRow =” index of start row “/ >
② < sql:query var= ”Result set name “scope =” save range “>
Select statement
The result set obtained by the operation
<sql:query var=”students” scope=”session” sql=”select * from stu” dataSource=”conn”/>
${students. Rowcount} returns the number of data rows in the result set
${students. Columnnames} returns a collection of all fields
${students. Rowbyindex} returns the data set of a row in the result set
c) Data update< sql:update >
Specific usage:
① < sql:update sql= ”SQL statement “datasource =” datasource object name “/ >
② < sql:update dataSource= ”Data source object name >
SQL statement
d) Transaction application< sql:transaction >
Specific usage:
< sql:transaction dataSource= ”Data source object name “isolation =” transaction isolation level “>
< sql:query >Or< sql:update >
The isolation level above:
Note: if the datasource property is set in the transaction, there is no need to set it in the tag inside.
e) Parameter setting< sql:param >
① Function: parameter setting for query or data operation
② Use: usually as< sql:query >And< sql:update >Child tags of tags
Case study:
<sql:query dataSource=”conn” var=”stus” scope=”page”>
select * from student where stuage>?
< sql:param value= ”Parameter value
4、 Internationalized and formatted labels – FMT or fmt.tld
1. Function: to standardize the output of number, date and time to solve the problem of Chinese garbled
2. Add tag library in JSP page
<%@ taglib uri=“” prefix=”fmt” %>
3. Use of specific labels
a) <fmt:setLocale>
① Function: used for JSP page localization
② Use:< fmt:setLocale value= ”Language and region
b) <fmt:formatNumber>
① Function: used for JSP page data format
② Use:< fmt:formatNumber type= ”Number|current|percent “value =” data to be formatted “/ >
c) <fmt:formatDate>
① Function: used for JSP page date formatting
② Use:< fmt:formatDate type= ”Time | date | both “value =” date data to be formatted “/ >
d) <fmt:requestEncoding>
① Function: specify the encoding method of request data
② Use:< fmt:requestEncodding value= ”Coding method
For example:< fmt:requestEncoding value= ”gb2312”/>
In general, it can also be used as follows:
a) Create a property file, such as len_ zh .properties
b) < fmt:setLocale value= ”Point to the name of the file zh “> (zh is Chinese, en is English, fixed, which will be covered in Y2 course.)
c) < fmt:setBundle basename= ”The name of the file len “> (len is the name from)
d) < fmt:message key= ”Key words in the property file “> (the property file is len)_ zh .properties)
be careful: during internationalization, *. Properties files should be placed in the classes directory.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]