Classic introductory tutorial: JSP standard template library (transfer)

xiaoxiao2021-03-06  34

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 in JSP ScriptleTlet, the JSP page is as follows:

count to 10 in jsp scriptlet </ title> </ head> <body> <% for (INT i = 1; i <= 10; i ) {%> <% = i% > <%}%> </ body> </ html></p> <p>As you can see in the above example, 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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p><% @ Taglib URI = "http://java.sun.com/jstl/core" prefix = "c"%> <html> <head> <title> count to 10 example (useing jstl) </ title> < / head></p> <p><body> <c: foreach var = "i" begin = "1" end = "10" step = "1"> <c: out value = "$ {i}" /></p> <p><br /> </ c: foreach> </ body> </ html></p> <p>When you check the code of this example, you will see that the JSP page contains only the label. The above code uses an HTML tag such as <HEAD> and <br>. This label usage is not limited to HTML tags. This code can also use a JSTL tag such as <C: Foreach> and <C: Out>. In this article, some JSTL foundations will be described. Install JSTL</p> <p>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.</p> <p>To install JSTL in Tomcat, there are three steps below:</p> <p>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 entries that need to be added are as follows:</p> <p><taglib> <taglib-uri> http://java.sun.com/jstl/fmt </ taglib-uri> <taglib-location> /web-inf/fmt.tld </ taglib-location> </ taglib></p> <p><taglib> <taglib-uri> http://java.sun.com/jstl/fmt-rt </ taglib-uri> <taglib-location> /web-inf/fmt-rt.tld </ taglib-location> </ taglib></p> <p><taglib> <taglib-uri> http://java.sun.com/jstl/core </ taglib-uri> <taglib-location> /web-inf/c.tld </ taglib-location> </ taglib></p> <p><taglib> <taglib-uri> http://java.sun.com/jstl/core-rt </ taglib-uri> <taglib-location> /web-inf/c-rt.tld </ taglib-location> </ taglib></p> <p><Taglib> <taglib-uri> http://java.sun.com/jstl/sql </ taglib-uri> <taglib-location> /web-inf/sql.tld </ taglib-location> </ taglib></p> <p><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></p> <p><taglib> <taglib-uri> http://java.sun.com/jstl/x-rt </ taglib-uri> <taglib-location> /web-inf/x-rt.tld </ taglib-location> </ taglib></p> <p>After completing these three steps, you can now prepare to test your JSTL installation. You can verify it by creating a JSP page containing JSTL. A simplest example program is the counter program above. You should place it in your Webroot directory (C: / Program Files / Apache Tomcat 5.0 / WebApps / root). Once Tomcat starts, you should be able to view this page by browsing http://127.0.0.1:8080/count.jsp.</p> <p>If you don't have JSTL correctly, you may not have an error message. If JSTL can't explain your tag meaning, then it will skip directly on a web browser. The web browser will then explain these unknown HTML tags. Most browsers simply ignore these unknown HTML tags.</p> <p>To Be Continue ...</p> <p>Translated by caiyi0903 (Willpower), 2004.2.19 Classic Getting Started Tutorial: JSP Standard Template Library (on)</p> <p>JSTL Tag Library JSTL is often considered a single tag library. But JSTL actually has 4 label libraries. These tag libraries are summarized as follows: Core Tag Library - The necessary tags containing some web applications. Examples of the core label library include cyclic, expression assignments, and basic input output. Format / International Label - Includes label libraries that are used to parse data. This tag analyzes data, such as dates, different places, and more. Database Tag Library - contains labels that are used to access the SQL database. These tags typically only create prototype programs. This is because most programs do not process database access from the JSP page. Database access should be embedded in EJB that can be accessed by the JSP page. XML Tag Library - contains labels that are used to access the XML element. Because XML is widely used in Web development, XML processing is an important feature of JSTL. In this article, we will only make a brief introduction to the core label. We will examine a simple example to explain how to handle the data entered in the form. Before we start checking the programs, we must first see how JSTL handles expressions, JSTL's expression processing is completed by using the EL expression language, only can be used in JSP2.0. In the next section, we will examine the EL expression language. EL Expression Language JSP2.0 is the new expression language called EL. EL is widely used in JSTL. However, it is important to remember that EL is the function of JSP instead of JSTL. The JSP Scriptlet code used in JSP 2.0 can include an EL expression. The following line code demonstrates the EL: <P> your total, incruding shipping is $ {total shipping} </ p> as you can see, Total and Shipping are added after viewing. The value is displayed in the device. These expressions can also be used in the JSTL tag. An important issue for JSTL1.0 is that JSTL cannot be used on JSP1.2 versions. Because JSP1.2 does not support EL, there is a need to provide some additional JSTL tags 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 require JSP 2.0 to run. This needs can be achieved by providing a label that can display an EL expression. JSTL paradigm We will examine an 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 result of the POST.</p> <p>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.request.method == 'post'}> <c: if test = "$ {param.guess == 'java'}> You Guessed IT <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!" /> </ form> </ body> </ html> This simple web page displays a form that requires the user to guess the computer language thinking. Of course, the computer wants "java". This page begins by checking "whether it is submitted". This makes the form and the code for processing the form can be placed in the same page. It is achieved by the following JSTL IF statement. <c: if test = "$ {pageContext.Request.method == 'post'}> Now you see, <c: if> Tag Use the EL expression to determine if the request is requested by POST. If the data is POST to the page, then their guess values ​​entered by the user are stored in the parameters named "guest". This is because "guest" is the name of the form input. We must now check if this parameter is equal to "java". This is done by the following <c: if> tag: <c: if test = "$ {param.guess == 'java'}> You Guessed it! </ C: if> As you can see If the expression of the TRUE, the <C: IF> tag is executed. In this article, we have begun to investigate how JSTL is installed, and how it operates, and it also provides some JSTL small examples to help you understand. JSTL's core tags also include loop, stackers, and variable processing. By using the label, you can use itrate in Collectes, access the user's session data, perform other core tasks.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-63961.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="63961" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.047</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '1JO3qcyz4NmLoepozmCYYlgNKheZmAhToXajt_2BbAYDh_2FNl6pbVc4HHAYSAOV3kM_2F11bTH83FPQbjti8EewW_2F_2Bg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>