JSP standard tag library promotes development of web pages
Learning JSTL how to improve JSP to simplify the implementation of the web page
Overview
The release of the Java Server Pages (JSP) Standard Tag Library (JSTL) is a significant progress for JSP / Servlet developers. With an expression language (EL) and a set of four powerful, easy to learn standard tag libraries, JSTL is likely to become a main method of achieving dynamics, Java-based sites.
In 1996, the introduction of JSP Servlet made Java a reasonable choice for dynamic web development. The Java Server page (JSP) that appears is a step in the immediate, maintenanceable Java web page implementation process, which follows the JSP tagging. However, the release of JSTL (JSP Standard Tag Libraries) in 2002 is a further step of further accelerating and simplifying the development process.
In this article, I explain the functionality of JSTL, covering everything you need to start JSTL. Suppose you have the basic understanding of Java, JSP, XML, and install a web container. If you feel unfamiliar with these topics, you can browse on the background reference on Resources (http://www.javaworld.com/javaworld/jw-02-2003/jw-0228-jstl.html?#resources#resources). In the following description, assume you have XML and SQL knowledge.
Install JSTL support
For our JSTL installation example, we use Tomcat 4.1 (although arbitrary support servlet 2.3 and JSP1.2 descriptions of Servlet containers should be able to work). First, download Tomcat 4.1 and install it according to the instructions (Note: JSTL requires a JSP1.2 web container).
Start Tomcat with Tomcat4 Start programs and run the index.html page to make sure Tomcat is activated and is running well.
Next, install JSTL support, you can download JSTL support from Jakarta Site, (http://www.javaworld.com/javaworld/jw-02-2003/jw-0228-jstl.html?#resources#resources) and follow The following steps:
1. Download the JSTL search, extract / detail the file from the Jakarta site. (Http://www.javaworld.com/javaworld/jw-02-2003/jw-0228-jstl.html?#resources)
2. Copy You have decompressed JAR files to Common / lib into your Tomcat installation path (although our project does not need all JAR files); this makes the JSTL file for your web application.
3. For any Web application you want to use by JSTL, copy the .TLD file to your web-inf directory of your web application.
4. For your JSTL web app, edit Web.xml and add the following input:
5. When you create a JSP page that uses JSTL, you enter him to your web application's home directory, just like other JSPs and HTML pages. You can name any of the page, but it must have a .jsp extension.
Basic principle
First, all JSTL is also a JSP page, JSTL is just a supercoming of JSP functions.
Similarly, all JSTL tags are effective XML. This means: If you use contents outside the JSTL tag as a temporary text (usually HTML), the remaining JSTL tag must be explained as a legal XML. There are some implicit rules here, which requires most of the XML syntax.
JSTL provides a set of four standard tag libraries (Core core, INTERNATIONALIZATION / FORMAT international standardization / format, XML, and SQL) and supports EL. The basic design of JSTL and EL is to simplify web development and implementation.
In this article, we follow the naming rules of the JSTL manual and use the JSTL tag as actions. A JSTL tag corresponds to an action; calling action clearly reminds us: They add dynamic behavior to a further static page.
There are two versions of the JSTL tag library: one inserts JSP expression inserted, such as <% =...%>, Another use of JSTL EL. I will discuss the EL support in JSTL later. .
EL support
To understand the status of current JSTL support to EL, let's check how the relevant manual is processed. Java Manual Request (JSR) Employee Member Decision: For some reason, the EL specification description should be part of the JSP manual, not part of the JSTL manual. EL complete manual becomes part of JSP2.0. Because JSTL1.0 has been completed before JSP1.3, JSTL author has to make a reasonable guess for EL support for JSTL1.0 (the result may be quite good). A JSTL maintenance version will be consistent with the JSP 1.3 manual, and the JSTL maintenance version made some corrections in order to make EL suitable for JSP 1.3 manual. The EL summary described herein may be changed in the upcoming JSTL version, but any changes are tiny.
EL is used for simple syntax defining a set of powerful languages, it is easy to learn. It combines some styles of the JavaScript language and PERL language. EL expression, integrated JSTL tag from four standard tag libraries, provides a huge, flexible property set.
All EL expressions are enclosed in $ {}. The expression in JSTL is always part of the attribute value in the JSTL tag. The expression can be part of the property or merged and embedded in a string direct amount. JSTL properties can also include a simple string direct amount. In the next JSTL, we describe each case in a C: OUT action from the core library, this core library is sent to the JSP output of its value attribute value:
EL also defines a set of rules to enforce values in the express expression into a type corresponding to the context. We don't discuss these rules in detail; however, its method is very similar to the way of using Perl language (just like the practice in Perl language, the method is based on the operation, but, occasionally, it may not be you The result of very wanted, but you can accept).
EL provides support for access object properties, set elements, set hidden objects, and related, logical, and arithmetic operators. For the attributes of the index, including arrays and java.util.list, the elements can be accessed by the following syntax:
$ {alist [4]} $ {alist [Somevariable]}
JavaBean properties and java.util.map elements (which represent a series of names / value pairs) can be obtained using one of the following methods. In both of the following starts, we can access an attribute named Aproperty with a JavaBean language or to access a Map entity with keyword Aproperty. In the third expression (Note: I have omitted quotation marks), we use the name stored in the variable AvariableContainingPropertyName to access an element within Anobject:
$ {anObject.Aproperty} $ {anobject ["apropertyname"]} $ {anobject [avariableContainingPropertyname]}
Many hidden variables are defined in EL:
PageContext: PageContext object used for this page
PageScope, RequestScope, Sessionscope, and ApplicationsCope: These are MAP sets that map each of these ranges to values.
Param and Paramvalues: Parameters passed by page access, as header and headervalues like page JSP: The header passed by page request, as in JSP
Cookie: Map maps cookie program to specific cookie objects
EL defines a full set of operators that are very familiar with you in Java. Algorithm operators include , -, *, / (or div), and% (or mod). Related operators include ==,! =, <,>, <=,> =, Which correspond to EQ, NE, LT, GT, LE, and GE. I don't want to explain these operators in detail because they can instruct it. JSTL tag library
At this point, I have explained some basic principles and covers the EL syntax. I have to discuss four JSTL tag libraries. I mainly discuss the core library, because it is what you must use; however, I will discuss the rest of the rest in detail so that you can start it.
First of all, although I should talk more about the two styles of the JSTL tag library, each JSTL tag library mentioned above appears in two versions: a expression that supports using EL; another support standard JSP expression. When you introduce any tag library to a JSP page, you want to define a prefix, which specifies the namespace corresponding to the library.
Four standard tag libraries, and the prefix regulations defined by their JSTL specification, the list is as follows. Remember: You can define your own prefix, but this is definitely no benefit.
Four standard tag libraries
Library
EL library prefix
Request time (RT) library prefix
core
c
C_RT
International standardization / format
FMT
FMT_RT
SQL / DB support
SQL
SQL_RT
Xml
x
X_RT
In order to use the core tag library in the page (actually, you make your page to the namespace of the tag library), in your page, contain the instructions in the following example:
<% @ taglib prefix = "c" URI = http://java.sun.com/jstl/core%>
In order to use the tag of the tag library, you can design a prefix you in your containment statement, add the prefix for each tag:
Core database
Let us test the core database in more detail, first look at his most common function.
Display / set value and exception processing
The most basic tag of the core library is a C: OUT tag, which displays the value of an EL expression in the page. One expression using C: OUT is usually true:
We Have
In the above example, the value attribute is the expression we send to page output. I also explain the properties and default properties of options Escapexml. Escapexml's property specifies whether an XML character (<,>, &, and.) should be converted to the corresponding character entity code (so they can make these characters in an HTML page); the default attribute is used for EL cannot estimate the value Or the case where the value is calculated.
Note: When EL supports complete throughout JSP2.0, you don't need to use C: OUT action; you can only nested JSP expressions directly on the page.
Another universally used core action is C: SET, which sets a variable in the page. You can use the C: SET action in two ways, the first method sets the variable defined in the VAR attribute to the value defined in the value property, the method is as follows:
The above selection Item scope property specifies that we want to set a variable CustomerID within the session period; if the range is not set, the range defaults to the page.
C: The other powerful use is to assign the contents of the C: SET tag to a specified variable:
In the above example, the C: SET action defines a variable named CellContents (within the range of pages), which has content defined in the body of the tag. In this case, the body defines an HTML table cell element, a C: OUT operation in the calculation, and the result of the calculation includes the directivity of the string in the body. Just as you expect, JSTL is a bit early. In a typical JSP page, you have two exception handling: trying / capturing blocks in the small script code directly into the page; or use a JSP ErrorPage directive. The JSP ERRORPAGE instruction provides a good way to capture all anomalies to process any possible exceptions in the page. JSTL provides a good option with a CATCH action. This C: catch action provides a finer granular effective method to handle exceptions. And no embedded Java code to page. One C: catch action is like this: C: Catch action has an optional attribute, which is a variable that points to the abnormality. You may not be willing to use the C: Remove tag. This tag has an attribute for variable names and ranges and can delete the specified variables from the specified range. Process control Let us discuss the process control and condition tag of JSTL. If you have used conditions and process control statements in any language, there is no fresh thing here. C: IF action handles the test of simple conditional statements. Calculate the value of the Boolean expression in the test attribute, if the expression is true, the contents of the calculation. In the following action, we also explain the option VAR attribute. For future use, the VAR attribute saves the test results in the page (if there is no other scope attribute). Below we show support with C: Choose, C: When, and C: OtherWise exchange logic. A set of C: WHEN actions may include within an alternate tag, if any expression calculated in the C: When block is true, do not calculate the test within the C: Choose action. If there is no test calculation value in the c: when block: If the C: OtherWise action content occurs, calculate the contents of the C: Otherwise action: Due to the error of the system, you can't upload the full text, please check the system administrator, what is the 8000000009 accident?