A brief analysis of jsp standard label library

  • 2020-05-19 05:29:23
  • OfStack

1. JSTL profile
Introduction: In JSP and javaBean, when we are in a web page using a loop, or object method is used to connect to the database, we inevitably need to use to jsp scripting elements, which is embedded with a lot of java code, now developers want to avoid using jsp scripting elements, further will display application layer and business layer completely separate, more conducive to the application of collaboration, specifies the JSTL jsp developers, provides a set of series 1 general custom tag files, and will these files in 1 case, The jsp standard label library, JSTL, is formed.
1) definition: contains a set of standard tags for authoring and developing JSP pages
a) features: a scriptless environment for developers, no need to write JAVA scripts
b) contains
1. Core tag library
2. SQL tag library
3. Internationalization (I18N) and formatting tag library
4. XML tag library
c) classification: two versions
1. Use EL
2. Use request-time expressions
2) the role
a) further simplifies development, simplifies the page, and separates the page from the jsp script elements
b) make the java code appear as little as possible on the page
c) advantage: when used in conjunction with EL expressions, you can use EL instead of an attribute value to change dynamically one step further
3) use
Import the tag library (this step is required in tools such as Eclipse)
b) USES the implementation steps for tags in the tag library
i. Add jstl.jar and standard.jar to the WEB-INF /lib directory of the application (see the courseware directory "JSTL tag library" for the two jar files)
ii. Specify the description information of the tag library in the jsp page:
Grammar: < % @ taglib prefix c http uri = ":" = ". / / java sun. com/jstl core_rt "% >
Extensions to the use of the above syntax:
< % @taglib prefix = "tag alias" uri = "tag library location" >
1. Tag aliases are commonly used on c
2. Location of tag library :(that is, Settings of uri)
a) textbook: "http: / / java sun. com jstl/core_rt" library files to sun company website address, note that there is only an address point to, even if there is no Internet access, in the tools have also been corresponding to the path of engineering (note: at this point to two jar tag library file into the "project \ WEB - INF \ lib" directory.)
b) can also be used: "/ WEB-INF/c.tld" (note, however, that all *.tld files in the META-jar unzipping directory of the two jar files in the tag library should be placed in the "project name/WEB-INF/" directory, which ensures that there are c.tld files in the directory).
Note: the first of the two methods of importing the tag libraries is preferred, namely, only two tag libraries jar files need to be put into the "project \ WEB-INF \lib" directory.
c) is used in JSP
3. < c: xxxxxx attribute name = "attribute value" attribute name = "attribute value" >
4. xxxxx is a variety of functions provided in the tag library, detailed as follows:
2. Core tag library --core or c. tld
a) classification:
1. Generic tags: operate on the scope variables created by the JSP page
2. Condition label: operation condition operation judgment and processing
3. Loop label: operate the 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 property at the time of setting
3. < c: set > Used to set the value of a variable or object property
< 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 > For dynamic display of data (functionality similar to EL)
1: < c:out value= "expression" default= "default" escapeXml= "true/false" >
2: note: expressions can be 1) normal constants
2) output the variable value that has been set: ${variable name}
5. < c: remove > Use to delete the created variable
a) < c:remove var= "name of variable" scope = "scope" >
(case: demonstrate the tags above)
c) conditional labels
1. Provide conditional operations of two types: if choose
2. < c: if > grammar
a) < c:if test= "condition" var= "variable storing test condition results" scope= "scope" >
b)... The condition of body
c) < /c:if >
d) var stores: the result is true or false
e) cannot be implemented by the following implementation
3. < c:choose > grammar
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 three jsp)
d) iteration tag
1. There are two types: forEach forTokens
2. forEach
Simple application
< c:forEach var= "loop control variable" test= "loop condition" begin= "loop start value" end= "loop end value" varStatus= "variables that save the loop state" >
The loop body
< /forEach >
Access to collection objects
< c:forEach var= "the variable that holds the current record of the collection" items= "the name of the collection to be rotated" begin= "the starting index position of the collection" end= "the end position of the collection" >
< /forEach >
Note: if begin is not set, end will retrieve all elements. step is not set to 1 by default
3. forTokens (with < c:forEach > There is a difference, used to round 1 string)
< c:forTokens items= "string to be rotated" delims= "word delimiter" var= "variable to hold the word" >
< /c:forTokens >
(case: demonstrate the tags above)
3. SQL tag library -- sql.tld
1 of the application of the enterprise is inseparable from the database operations, in many time need to access the database in the JSP page, thus providing support for database access becomes 10 important in JSTL SQL tag library can provide 1 set to access database, query, update operations such as tag, greatly convenient JSP page database access
1, role: for the operation of the database
2. Add the tag library to the JSP page
< % @ taglib uri = "http: / / java. sun. com jstl/sql_rt" prefix sql "% =" >
3. Specific use marks
a) sets the data source < sql:setDataSource >
Specific usage: < sql:setDataSource driver= "driver classpath" url= "connection database Settings" user= "user name" password= "password" var= "connection object name" / >
b) query data < sql:query >
Specific usage:
1. < sql:query sql= "query statement" var= "result set name" scope= "range of result set saved" dataSource= "associated data source object" maxRows= "maximum number of rows" startRow= "index of start row" / >
2. < sql:query var= "result set name" scope= "save range" >
select statement
< /sql:query >
Operation to obtain the result set
< sql:query var= "students" scope= "session" sql= "select * from stu" dataSource= "conn" / >
${students.rowCount} returns the number of rows of data in the result set
${students.columnNames} returns 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:
1. < sql:update sql= "SQL statement" dataSource= "data source object name" / >
2. < sql:update dataSource= "data source object name" >
SQL statement
< /sql:query >
d) transaction application < 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 the dataSource property is set in the transaction, it is not set in the tag inside.
e) parameter Settings < sql:param >
Function: in the query or data operation parameter Settings
(2) use: usually as < sql:query > and < sql:update > Child of the tag
Case study:
< sql: query dataSource conn var "= =" "page stus scope" = "" >
select * from student where stuage > ?
< sql:param value= "parameter value" / >
< /sql:query >
4. Internationalize and format labels --fmt or fmt.tld
1. Role: standardize the output of Numbers, dates and time, and solve the problem of Chinese garbled codes
2. Add the 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 >
Function: for the localization of JSP page
(2) use: < fmt:setLocale value= "language and region" / >
b) < fmt:formatNumber >
Function: used for the formatting of JSP page data
(2) use: < fmt:formatNumber type= "number|currency|percent" value= "data to format" / >
c) < fmt:formatDate >
Function: used for JSP page date formatting
(2) use: < fmt:formatDate type= "time|date|both" value= "date data to be formatted" / >
d) < fmt:requestEncoding >
Function: specify the encoding of the requested data
(2) use: < fmt:requestEncodding value= "encodings" / >
Such as: < fmt: requestEncoding value gb2312 = "/" >
As if, or as if:
a) create a properties file called len_zh.properties
b) < fmt:setLocale value= "to the name of the file zh" > (zh is Chinese,en is English, fixed, and we'll talk about it in Y2.)
c) < fmt:setBundle basename= "name of file len" > (len is the original name)
d) < fmt:message key= "keywords in properties file" > (the properties file is len_zh.properties)
Note: for internationalization,*.properties files are placed in the classes directory.

Related articles: