Classic entry tutorial: JSP standard template library (on)
Introduction
JSP Standard Template (JSTL) is a new component developed by Sun. JSTL allows you to use the label (Tags) to develop JSP page, rather than using most JSP programmers have been habitually developed. JSTL can almost anything to do with traditional JSP Scriptlet code. You may be confused, why do we need another such HTML to generate language? STL allows JSP programmers to program with Tags instead of Java code. To show this is more superior, an example will be given below. We will check a very simple JSP page from 1 to 10. We will check two ways, one is based on JSP-based scripTlet, one is JSTL. When the example of this counter page is written with JSP scripTlet, the JSP page is as follows:
count to 10 in jsp scriptlet title> head> <% for ( INT i = 1; i <= 10; i ) {%> <% = i%> <%}%> body> html> as seen in the above example In that, the page source code generated using the Scriptlet code will contain a mixed HTML tag and Java statement. This hybrid programming method is not the best way, and the main reasons have the following points. The main reason is its readability. This readability is mainly based on humans and computers. JSTL allows programmers to view a page that contains only a full HTML and a tag similar to HTML. The readability of the SP Scriptlet code is not suitable for humans. This mixed scripTlet and HTML code are also difficult to read for your computer. Especially for those HTML official tools such as Dreamweaver and Microsoft FrontPage, it is more prominent. Currently, most HTML official tools isolate the JSP Scriptlet code in the form of non-editable blocks. This HTML official tool usually cannot directly modify the JSP Scriptlet code. The following code shows how this counter example is written in a JSTL method. As you can see, this code list is unchanged, just a label is used. HTML and JSTL tags are mixed to produce this program. <% @ Taglib URI = "http://java.sun.com/jstl/core" prefix = "c"%>
count to 10 example (useing jstl) title> < / head>
< / c: foreach> body> html> When you check the code above this example, you will see that the JSP page contains only tags. The above code uses an HTML tag such as and
. This label usage is not limited to HTML tags. This code can also use a JSTL tag such as and .
In this article, some JSTL foundations will be described. Installing JSTL To use JSTL, you must have a JSP1.2 (or higher) container. The most common JSP container is Apache Tomcat. You can download from http://jakarta.apache.org/tomcat/ here. Independent Tomcat allows you to use regular JSP scripTlet code. To use JSTL, you must install JSTL in Tomcat. JSTL's main URL is http://java.sun.com/products/jsp/jstl/. To use JSTL, you must decompress this file and install it to the correct position of Tomcat. To install JSTL in Tomcat, there are three steps below:
Copy the JSTL JAR file to the lib directory of Tomcat. If you use Windows, then the most likely location of the lib directory is C: / program files / apache tomcat 4.0 / webapps / root / web-inf / lib. You should copy these JAR packages to your Tomcat jar directory. Copy the JSTL TLD file to Tomcat's Web-Inf directory you check the JSTL release file, you should notice that 8 files are tailing with a TLD extension. All these 8 files should be copied into your web-inf directory. Modify the web.xml file contains these TLD files. Finally, you must modify your web.xml, add 8 label libraries (entry). The entry that needs to be added is as follows: http://java.sun.com/jstl/fmt taglib-uri> /web-inf/fmt.tld < / taglib-location> taglib> http://java.sun.com/jstl/fmt-rt taglib-uri> / web-inf / fmt- Rt.TLD taglib-location> taglib> http://java.sun.com/jstl/core taglib-uri> / web-inf / C.TLD taglib-location> taglib> http://java.sun.com/jstl/core-rt taglib-uri> / web- INF / C-RT.TLD Taglib-Location> taglib> http://java.sun.com/jstl/sql taglib-uri> / Web-inf / sql.tld taglib-location> taglib> http://java.sun.com/jstl/sql-rt taglib-uri> /Web-inf/sql-rt.TLD taglib-location> taglib> http://java.sun.com/jstl/x taglib-uri> /web-inf/x.tld taglib-location> taglib> http://java.sun.com/jstl/x-rt taglib-uri > /web-inf/x-rt.tld taglib-location> taglib> After completing these three steps, you can now prepare to test your JSTL installation.