JSTL (JSP Standard Tag Library)

xiaoxiao2021-03-06  42

JSTL (JSP Standard Tag Library) Introduction Author: Let Master (

Shaoye@dev-club.com

The foremail starts from the JSP 1.1 specification, JSP supports using custom labels in JSP, custom labels, have caused programmers to repeat the definition, which has contributed to the birth of JSTL (JavaServer Pages Standard Tag Library). Because JSTL needs to be used in the work, but the Internet can't find Chinese information about JSTL, so there is this article. JSTL Introduction JSTL is a JSP label library that is constantly open source code, which is maintained by the Jakarta team of Apache. JSTL can only run on a container that supports JSP1.2 and Servlet2.3 specification, such as Tomcat 4.x. But in JSP 2.0, it is standard for standard support. JSTL currently has a current version of 1.02, and finally published version 1.0. JSTL contains two parts: tag libraries and EL (Expression Language Expression Language) languages. The label library currently supports four labels:

Label URI prefix sample Corehttp: //java.sun.com/jstl/corec XML processingHttp: //java.sun.com/jstl/xmlx i18n capable formattingHTTP : //java.sun.com/jstl/fmtfmt Database Access (SQL) http://java.sun.com/jstl/sqlsql

Core supports some basic operations in JSP; XML Processing supports processing of XML documents; I18N Capable Formatting supports internationalization of JSP pages; Database Access (SQL) supports JSP's operations of the database. Since I have limited level, this article only introduces the Core tag, if you are interested, you can explore the use and expansion of other three labels. EL Language Introduction The EL language is a representation of a JSTL output (input) a Java expression. In JSTL, the EL language can only be used in attribute values. The EL language can only be called by establishing an expression $ {exp1}. There are three ways to use expressions in attribute values. 1. The Value property 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: is an el, which is equivalent to the JSP statement <% = Request.getattribute ("UserName")%> or <% = session. GetAttribute ("UserName")%> 2, the value property contains one or more properties, these attributes are split or revolve around the In this case, the expression is calculated from left to right, and converts the result into a string type (according to the type conversion rule), and assigns the result to the value attribute 3, the value attribute only contains text In this case, the string type attribute value VALUE will convert to the type of tag according to the type conversion rule. The EL language operator obtains an object or collection attribute value in order to obtain the attributes in the collection, and EL supports the following two operations 1. Use. Operators to get properties with names. For example, the expression is $ {user.username} indicates the userName property 2 of the object user. Use the [] operator to obtain an attribute that is named or filed. Expression $ {User ["UserName"]} and expressions $ {user. Username} meaning the same expression $ {row [0]} Indicates the first entry for the ROW collection. Here User is a class object, its property UserName must meet the standard Javabean specification, that is, the corresponding getter, setter method must be defined for the username property. EMPTY operator (null value check) uses the EMPTY operator to determine whether the object, the collection, or string variable is empty or NULL. For example: $ {empty param.username} If the UserName value in the parameter list of Request is NULL, the value of the expression is true. EL can also be compared to NULL directly using comparison operators. Such as $ {param.firstname == NULL}. Comparison Operator Operators Description == or EQ equal check! = Or NE No candidates or GT is greater than checking <= or LE less than or equal to check> = or GE is equal to or equal to check

Digital operators and logical operators are the same as Java language, no longer list. Core Tag Library 1, General Tags Tags are used to display data in JSP, which has the following properties

The selection must default to the value value output information, which can be the EL expression or constant is no default value as an empty display message whether there is no escapexml to avoid special XML characters. TRUE

example:

Your username is: Display the user's username, if the air is displayed

Specifies to get the value of UserName from the session;

The value of the username is displayed, and the default is taken from the request (page). If there is no user named username in the requestion, it is taken from the session, and the session is not taken from Application (servletContext). If no value is not taken, no value display. tag for saving data, it has the following properties

Attribute Description Must default value value to save information, you can be an EL expression or a Target that needs to modify the variable name of the attribute. Generally, no Property needs to modify the JavaBean property No Var need to save information variable No No Scope Saves Variables Wid Over the Variables

If the Target property is specified, the property property must also be specified. example:

Save the value of Test.Testinfo into the session Test2, where Test is an instance of a JavaBean, and Testinfo is the properties of the Test object.

Save the value of the Cust.Address of the object Cust.Address to the Variable City tab is used to delete data, it has the following properties

A selection is a variable that must be default VAR to delete the variable that does not have the scope to be deleted. All ranges, including Page, Request, Session, Application et al.

example:

Remove Test2 variables from the session. 2, stream control tag tag has the following properties

This is a class that must be default the conditions that require the evaluation of TEST, which is equivalent to the condition in the if (...) {} statement is the variable number of the VAR requires the saving condition results. No Scope save condition results.

This tag does not accept any attribute tag has the following properties

Conditions if you must default, it is necessary to evaluate the conditions for TEST.

This tag also does not accept any properties:

user.wealthy is true.

If the user.wealthy value True, User.wealthy is true is displayed.

user.stingy IS true. User.generous and user.stingy> Only when the condition user.generous returned value is TRUE, User.Generous is true. User.stingy is true is displayed only when the condition user.stingy is True. Other all situations (ie, user.generous and user.sting are not true) all display USER. Because JSTL has no conditional statement such as if () {...} else {...}, this form of statement can only use , and Tags are completed together. 3, loop control tag tag for universal data, it has the following properties

The subsequent items that must be default Items to loop-looped items No Begin Start Condition No 0nd End Condition No Collection The last item STEP Step Length 1var Represents the current item variable name No Varstatus display loop state variable

example:

Equivalent to the Java statement

For (int i = 0; i

Here, Vectors is a Java.util.Vector object, which is stored in String data, and Vector is the String object under the current cycle. In fact, the VECTORS here can be any object that implements the java.util. Collection interface.

count =

Output: count = 0 ... count = 100 Tags have the following properties

The property description must default Items to loop-free items that is not a Delims split. No Begin Start Condition No 0nd End Condition No Collection The last item STEP Step No 1var Represents the current item variable name No Varstatus display loop status None

example

The use of this label is equivalent to the Java.util.StringTokenizer class. Here, the string A: B: C: D is followed by: Separate loop four times, token is a string that is looped to the currently split. 4. Import files and urljstl core tag libraries Support to use to print and format URLs using , and use to replace the URL. Tag contains another page code to the current page, which has the following property attribute description Must default URL URL URL need to import the page does not have a context / post-second Web application Name. Application CHARENCODING Whether ISO-8859-1VAR is ISO-8859-1VAR accepts the variable amount of imported text to accept variables that accept the imported text variable range to accept the Java.io.Reader variable name of imported text. No Varstatus Show whether the variable in the loop state is not

Tag outputs a URL address, it has the following properties

Attribute Description Must default The URLURL address is whether the name without context / after the name of the local web application is whether the current application Charencoding is used to import data whether ISO-8859-1VAR accepts the processed URL variable name, the variable storage URL No Output to the page scope storage URL variable number of variables to the variable number of variables

example:

Contain URL http://www.url.com/edit.js to the current location of the current page and save the URL to the newsfeed variable

Output in the current position of the current page Output tab will realligate to another page, it has the following properties

A selection is a default value URLURL address is whether the name without context / back to the local web application is currently available.

example:

The request will be reset to http://www.yourname.com/login.jsp page, which is equivalent to response.setRedirect ("http://www.yourname.com/login.jsp"); Tags are used to pass parameters to a redirect or contain page, which has the following properties

A selection of sexy means that the value of the variable name set in the request parameter is not valued in the request parameter is not set.

example:

Pass the parameter 888 with the name to the login.jsp page, which is equivalent to the advantage of login.jsp? Id = 888JSTL 1. Provide a consistent interface between the application server, and maximize the web application in each application server. The transplantation between. 2, simplify the development of JSP and web applications. 3. Reduce the number of Scriptlet code in JSP in a unified manner, which can achieve programs without any Scriptlet code. In our company's project is not allowed to have any scriptlet code appear in JSP. 4, allowing JSP design tools to develop further integration with web applications. I believe that there will soon support the iDE development tools that support JSTL. Summary The above is only part of JSTL, if there is time, I will continue to write other parts to everyone. If you want to use JSTL, you must put JSTL.jar and Standard.jar files in ClassPath, if you need to use the XML Processing and Database Access (SQL) tab, put the related JAR files in ClassPath, these JAR files All exist in the Zip file downloaded back. This zip file can be from http://jakarta.apache.org/builds/jakarta-taglibs/releases/standard/jakarta-taglibs-standard-1.0.zip

download. Reference 1,

http://java.sun.com/products/jsp/JSTL/

Sun's JSTL site 2,

Http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html

JAKARTA group of JSTL sites 3,

http://www.manning.com/bayern/appendixa.pdf

JSTL's reference documentation, many of this article is translated from this PDF file. 4, << J2EE Programming Guide (1.3 Edition) >> Introduced JSTL's prototype, Wrox book is a boutique.


New Post(0)