JSTL tag library
JSTL is generally considered a single tag library. But JSTL actually has 4 tag libraries. These tag libraries are summarized as follows:
Core tag library - Contains some necessary tags for Web applications. Examples of the core tag library include loops, expression assignment, and basic input and output. .
Formatting/Internationalization Tag Library—Contains tag libraries used to parse data. This tag parses data such as dates, different venue areas, etc.
Database Tag Library—Contains tags used to access SQL databases. These tags usually only create prototype programs. This is because most programs will not handle database access directly from the JSP page. 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 the core tags. We'll look at a simple example of how to handle data entered by the user into a form. Before we start checking the program, we must first look at how JSTL handles expressions. Expression processing in JSTL is done using the EL expression language, which can only be used in JSP2.0. In the next section, we'll examine the EL expression language.
EL expression language
A major component of JSP 2.0 is the new expression language called EL. EL is widely used in JSTL. However, it is important to remember that EL is a feature of JSP and not JSTL. JSP scriptlet code used in JSP 2.0 can contain EL expressions. The following line of code demonstrates EL in JSP scriptlet code:
<p>
Your total, including shipping is ${total+shipping}
</p>
As you can see, adding total and shipping produces HTML and displays their values in the browser. These expressions can also be used in JSTL tags. An important problem with JSTL1.0 is that JSTL cannot be used with the JSP1.2 version. Because JSP1.2 does not support EL, it is necessary to provide some additional JSTL tags to facilitate the use of EL. For example, if you wanted to display the above expression using JSTL, you could use the following code.
<p>
Your total, including shipping is <c:out var="${total+shipping"/>
</p>
It is worth noting that JSTL does not require JSP2.0 to run. This requirement can be achieved by providing a label that displays the EL expression.
JSTL Example We will examine a simple example using JSTL. In this example, we'll examine a common process in web applications. We'll see how to submit (POST) a form and handle the results of the POST. 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 of?
<input type="text" name="guess" />
<input type="submit" value="Try!" />
<br/>
</form>
</body>
</html>
This simple Web page displays a form that asks the user to guess the computer language the program has in mind. Of course, the computer is thinking "Java". This page starts by checking "has it been submitted". This allows the form and the code that handles the form to be placed on the same page. This is achieved through the following JSTL if statement.
<c:if test="${pageContext.request.method=='POST'}">
Now you see, the <c:if> tag uses an EL expression to determine whether the request method is POST. If the data is posted to the page, then the user's input of their guess will be stored in a parameter called "guest". This is because "guest" is the name of the form input. We now have to check if this parameter is equal to "Java". This is accomplished through the following <c:if> tag:
<c:if test="${param.guess=='Java'}">
You guessed it!
</c:if>
As you can see, if the expression is true, the body of the <c:if> tag is executed. In this article, we began to examine how JSTL is installed and how it operates. We also provided some small JSTL examples to help everyone understand.
The core tags of JSTL also include loops, iterators, and variable handling. By using tags, you can iterate within collections, access a user's session data, perform other core tasks, and more. In addition to the core tag library, XML, database and formatting tag libraries also provide more advanced uses.
(End)
Note: EL Expression Language is translated into EL expression language here, and some are translated into EL expression language, which means the same thing.
Translated by Caiyi0903(Willpower),2004.2.19