Classic getting started Tutorial: JSP standard template library (below) jstl tag Library
Jstl is generally considered a single-tag library. However, jstl actually has four tag libraries. These tag libraries are summarized as follows:
- Core tag Library-Includes necessary tags for some web applications. Examples of the core tag Library include loops, expression assignment, and basic input and output ..
- Format/International tag Library-Includes the tag library used to parse data. This label will parse the data, such as the date and region.
- Database tag Library-Includes tags used to access the SQL database. These labels generally only create prototype programs. This is because most programs do not process database access directly from JSP pages. Database Access should be embedded in ejbs that can be accessed by JSP pages.
- XML tag Library-Contains tags used to access XML elements. Because XML is widely used in web development, XML processing is an important feature of jstl.
In this article, we will only give a brief introduction to core labels. We will look at a simple example to illustrate how to process the data entered by the user in the form. Before we begin to check the program, we must first look at how jstl processes expressions. jstl expressions are processed by using the El expression language and can only be used in jsp2.0. In the next section, we will examine the El Expression Language.
El Expression Language
A major component of jsp2.0 is the new expression language called El. El is widely used in jstl. However, it is important to remember that El is a JSP function rather than a jstl. The JSP scriptlet code used in jsp2.0 can contain El expressions. The following code demonstrates El in JSP scriptlet code:
<P>
Your total, including shipping is $ {total + shipping}
</P>
As you can see, total and shipping will generate HTML and display their values in the browser. These expressions can also be used in jstl labels. An important issue of jstl1.0 is that jstl cannot be used in jsp1.2. Because jsp1.2 does not support El, it is necessary to provide some additional jstl labels to help El use. For example, if you want to use jstl to display the above expression, you can use the following code.
<P>
Your total, including shipping is <C: Out Var = "$ {total + shipping"/>
</P>
It is worth noting that jstl does not need jsp2.0 to run. You can achieve this by providing a label that can display EL expressions.
Jstl example
We will examine a simple example of using jstl. In this example, we will examine a common process in Web applications. We will see how to submit (post) A form and process the post result. The program code is as follows:
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix = "C" %>
<HTML>
<Head>
<Title> If with body </title>
</Head>
<Body>
<C: If test = "$ {pagecontext. Request. Method = 'post'}">
<C: If test = "$ {Param. Guess = 'java'}"> You guessed it!
<Br/>
<Br/>
<Br/>
</C: If>
<C: If test = "$ {Param. Guess! = 'Java'} "> you are wrong
<Br/>
<Br/>
<Br/>
</C: If>
</C: If>
<Form method = "Post"> guess what computer language
I am thinking?
<Input type = "text" name = "Guess"/>
<Input type = "Submit" value = "try! "/>
<Br/>
</Form>
</Body>
</Html>
This simple web page displays a form that requires the user to guess the computer language that the program thinks. Of course, the computer is thinking of "Java ". This page starts by checking "submitted. This allows the form and the Code for processing the form to be placed on the same page. It is implemented through the following jstl if statement.
<C: If test = "$ {pagecontext. Request. Method = 'post'}">
Now you can see that the <C: If> label uses the El expression to determine whether the request method is post. If the data is post to the page, the user-input conjecture values will be stored in the parameter "guest. This is because "guest" is the name of the input form. Now we must check whether this parameter is equal to "Java ". This is done through the <C: If> label below:
<C: If test = "$ {Param. Guess = 'java'}">
You guessed it!
</C: If>
As you can see, if the expression is true, the TAG body of the <C: If> label is executed. In this article, we begin to examine how jstl is installed and how it runs. We also provide some small examples of jstl to help you understand.
Jstl core labels also contain loops, stacks, and variable processing. By using tags, you can perform iterate in collections, Access session data, and execute other core tasks. In addition to the core tag library, XML, database, and formatted tag library, it also provides more advanced functions.
(End)
Note: El Expression Language is translated into El Expression Language, and some are translated into El Expression Language.
Translated by caiyi0903 (willpower), 2004.2.19