In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use JSTL, a new component of JSP programming. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The JSTL introduction allows JSP programmers to program with tags instead of Java code. To illustrate why this is desirable, a quick and simple example is given. Let's take a very simple example of counting from 1 to 10. We use the conventional scriptlet JSP-based web page and JSTL to make this page. When the example up to 10 is written in scriptlet JSP, the JSP page will look like this:
< html> < head> < title>Count to 10 in JSP scriptlet
< /title> < /head> < body> < % for(int i=1;i< =10;i++) {%> < %=i%> < br/> < % } %> < /body> < /html>As you can see from the example above, the source code of a web page generated with scriptlet code includes a mix of HTML tags and Java statements. There are many reasons why this hybrid programming style is not *.
Mixing scriptlet and tag-based code is not the main reason for choosing readability. This readability is for people and computers. JSTL allows programmers to view a program that consists of entire HTML and HTML-like tags.
The readability of JSP scriptlet code is not limited to humans. The mix of Scriptlet and HTML code is also difficult for computers to read. In particular, HTML production tools such as a Dreamweaver and Microsoft FrontPage. Currently, most HTML authoring tools separate JSP scriptlet code as non-editable blocks. HTML authoring tools usually do not directly modify the JSP scriptlet code.
The following code shows how to use JSTL to write this example from 1 to 10. As you can see, this code list is more coherent because only tags are used. Is an example of a mix of HTML and JSTL tags.
< %@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> < html> < head> < title>Count to 10 Example (using JSTL)
< /title> < /head> < body> < c:forEach var="i" begin="1" end="10" step="1"> < c:out value="${i}" /> < br /> < /c:forEach> < /body> < /html>When you check the previous code, you can see that the JSP page is made up of tags. The above code uses HTML tags such as
< head>And
< br>. The use of tags is not limited to HTML tags. This code also uses JSTL tags such as
< c:forEach>And
< c:out>. I'll introduce you to some of the basics of JSTL in this article.
Install JSTL
In order to use JSTL, you must install JSP 1.2 (or higher) containers. One of the most commonly used JSP containers is Apache Tomcat Web server. You can get a copy of Tomcat from http://jakarta.apache.org/tomcat/ here. Using Tomcat alone allows you to use regular JSP scriptlet code. In order to use JSTL, you must install JSTL to Tomcat. JSTL can be obtained from the same source as Tomcat. The main URL of JSTL is http://java.sun.com/products/jsp/jstl/. In order to use JSTL, you must extract the allocation files and install them to the correct location in Tomacat.
To use Tomcat at the same time, follow these three steps to install JSTL correctly:
Copy the JSTL JAR file to the lib directory of Tomcat.
If you use Windows, the possible location of your lib directory is C:\ Program Files\ Apache Tomcat 4.0\ webapps\ ROOT\ WEB-INF\ lib. Many JAR files were included when JSTL was released. You need to copy each JAR file to the Tomcat JAR directory.
Copy the JSTL TLD file to Tomcat's web-inf directory web-inf directory at C:\ Program Files\ Apache Tomcat 4.0\ webapps\ ROOT\ WEB-INF. If you study JSTL allocation files, you should notice 8 files with the extension TLD. These 8 files should be copied to your web-inf directory.
To modify the web.xml file to include the TLD file, you must modify your web.xml file and add paths to the 8 tag libraries you added. This depends on the Lord.
< web-app>Add to the instruction
< taglib>Instructions.
< taglib> < taglib-uri>Http://java.sun.com/jstl/fmt
< /taglib-uri> < taglib-location>/ WEB-INF/fmt.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/fmt-rt
< /taglib-uri> < taglib-location>/ WEB-INF/fmt-rt.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/core
< /taglib-uri> < taglib-location>/ WEB-INF/c.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/core-rt
< /taglib-uri> < taglib-location>/ WEB-INF/c-rt.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/sql
< /taglib-uri> < taglib-location>/ WEB-INF/sql.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/sql-rt
< /taglib-uri> < taglib-location>/ WEB-INF/sql-rt.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/x
< /taglib-uri> < taglib-location>/ WEB-INF/x.tld
< /taglib-location> < /taglib> < taglib> < taglib-uri>Http://java.sun.com/jstl/x-rt
< /taglib-uri> < taglib-location>/ WEB-INF/x-rt.tld
< /taglib-location> < /taglib>By completing the above three steps, you are ready to test your JSTL installation. This can be done by creating a JSP page using JSTL. The simplest example is the "count to ten" example shown above. You should put your JSP files in the Webroot directory (C:\ Program Files\ Apache Tomcat 4.0\ webapps\ ROOT). Once the Tomcat server is started, you can browse to http://127.0.0.1:8080/count.jsp to view your web page.
If you do not install JSTL correctly, there may be no error message. If JSTL can't translate your tags, they go directly through the web browser. The web browser will translate these tags as unknown HTML tags. Most browsers ignore unknown HTML tags.
JSTL tag library
JSTL is also often used as a separate tag library. In fact, JSTL has four tag libraries. These tag libraries are summarized as follows:
Core Tag Library- includes tags that are essential to Web applications. Examples of core tag libraries include loops, formula evaluation, and basic input and output.
Formatting/Internationalization Tag Library- includes tags that are used to parse code. Some of these tags parse the code, such as the date, based on the current area.
Database Tag Library- includes tags for accessing the SQL database. These tags are usually only used to create prototype programs. This is because most programs do not handle database access directly from JSP pages. Database access should be placed in EJBs and accessed through JSP web pages.
XML Tag Library- includes tags that can be used to access XML elements. Because XML is used in many Web applications, XML processing is an important function of JSTL.
In this article, we only need to look at a few core tags (core tags). We will give a simple example of how users deal with data when they enter a table. Before we proceed with this program, we first need to take a look at JSTL handles expressions. Expression processing in JSTL is done by using the EL expression language, as in JSP2.0. In the next section, we will examine the EL expression language.
EL expression language
One of the main components 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 feature of JSP, not JSTL. The JSP scriptlet code used with JSP2.0 includes EL expressions. The following example line of code uses EL in the JSP scriptlet code.
< p>Your total, including shipping is ${total+shipping}
As you can see from the above code, the "total" and "shipping" values are added and displayed as HTML. These expressions can also be used in the JSTL tag. One of the important requirements of JSTL1.0 is that JSTL should be used with JSP1.2. Because JSP1.2 does not support EL, it is necessary to provide some extra JSTL tags to use EL. 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"/>One of the requirements of JSTL is that it does not require JSP2.0 to run. This requirement can only be met by providing a tag that displays the EL expression.
JSTL example
Now let's look at a simple example of using JSTL. In this example, we will look at a common program that many Web applications can do. We will see how to POST a table and run the results from it. A simple program that can do this is shown below:
< %@ 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 will display a table and ask the user to guess which computer language the program can consider. Of course, the computer will consider "Java." The first thing on this page is to check to see if a POST has been completed. This makes the table and the code that processes the table on a single page. This is done through the following JSTL if statement.
< c:if test="${pageContext.request.method=='POST'}">Here, you see
< c:if>The tag uses an EL expression to evaluate whether the required way is a POST. If the data is pasted into the web page, the value that the user enters their guess is stored in a parameter called "guess". This is because "guess" is specified as the name of the entry in the form. We must now check to see if this parameter is equal to "Java". This passes through the following
< c:if>Tag to do it.
< c:if test="${param.guess=='Java'}">You guessed it!
As you can see, if the statement is evaluated correctly
< c:if>The body of the tag is executed. In this article, we begin to look at the basics of how to install JSTL and how it works.
The core tags of JSTL also include tags for loops, iterations, and variable handling. By using these tags, you can repeat these collections, access user conversation data, and perform other core tasks performed by all Web applications. In addition to core tag library,XML, database and formatting tag libraries also provide more advanced uses.
This is the end of this article on "how to use JSTL, a new component of JSP programming". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.