Rookie learning JSP (3)

zhaozj2021-02-16  43

Although the use of Scriplet and JavaBean in JSP can complete a lot of dynamic operations such as: Accessing the database, implement E-mail, bbs, and more. It is very difficult to use Scriplet to insert Java code in JSP, but this also has a certain difficulty of maintaining future maintenance. As an alternative JSP provides a mechanism called a custom label, using this mechanism you can extend JSP in your needs, with your own defined tag package, make JSP pages more simple, More easily develop and maintain.

Once you have customized your tag, you can use them to develop it like SUN. Its syntax is the same as

Since there are so many benefits, let's take a look at what is Custom Tag. The definition of Sun is the JSP tag defined by the user (is a user-defined Jsp Language ELEMENT). And there are some features: 1. The parameters are obtained from the request page. 2. You can access objects that all JSP pages can be accessed. 3. You can modify the Response generated by the request page. 4. Can interact with each other. You can use a javabean that makes a tag and use it in another TAG. 5. Mutual nested. (就 Meaning)

Below we will learn from a simple example to learn from JSP TAG.

To use tag, you must first complete the following things: 1. Declare a tag library. 2. Implement the declared tag library.

To declare the tag library first to create a TLD (Tag Library Descriptor) file, this is an XML file, but with TLD is extension, such as Database.tld. The establishment of a good TLD file is generally placed in Web-INF (mentioned in the previous two articles) directory.

Ok, now we build a TLD file in Web-Inf, such as Database.tld. Then the following code COPY to the beginning of the TLD file:

Code 3

The above is that each TLD file is in place that is essential. As for web-jsptaglibrary_1_2.dtd This file you can find in the Tomcat4 or updated version in% Catalina_Home% / common / lib servlet.jar (if you are interested, see if you don't look), the specific location is Servlet.jar / Javax / Servlet / JSP / Resources, and you will find an old version under this folder: web-jsptaglibrary_1_1.dtd. But Sun's documentation is recommended to use new versions as much as possible. (All the TLD files that have occurred later, if they do not declare, they are assumed to add top 3).

*********************************************************** ************************* (I have to specifically explain the opening in the process of studying: If you use the beginning to compile, you will tell you the external file http file: //java.sun.com/j2ee/dtd/WEB-JSPTAGLIBRARY_1_2.DTD, I It may be that the above URI is used in the enterprise version, and I didn't install J2EE. If I said not to ask prawn to correct, my e-mail: flyhellenessuanwei@yahoo.com.cn. **** *********************************************************** ***********************

With the above foundation, we can add content to the document just established.

1.0 1.2 Heller.Database A Simple Tab Library for the Database ...

Code 4 taglib is the root of the TLD document (only one). TLIB-VERSION indicates the version of the tag, the same JSP-Version indicates the version of JSP. The URI gives a unique URI to locate the tag library. Description is a short description of the tag library. (There are also some optionally interested people to refer to Sun's document http://java.sun.com/webservices/docs/1.0/tutorial/doc/jsptags.html).

One or more TAG elements can be inserted in Code4. For example, we join: ... simtag temp.simtag JSP ... ...

Code 5

The names in the TAG element are specified for the TAG you define, and the tag-class is used to indicate the Class name that handles this tag. Body-content points out the type of content allowed between . In general, you can use JSP. Because this option does not affect the processing of processing TAG's Class to Tag content, it is only useful to use some development tools to describe it. The following is the completed TLD document:

1.0 1.2 Heller.Database a Simple Tab Library for the Database

simtag temp.simtag jsp

Let's implement the TEMP.SIMTAG class defined above.

Package temp;

Import javax.servlet.jsp.tagext. *; import javax.servlet.jsp. *;

Public class simtag extends tagsupport {private static int guestNum = 0;

Public int dostarttag () throws jspexception {tryContext.getut (). Println ("Hello!"); PageContext.getut (). Println ("You are the" guestnum "st guset!");} Catch (Exception EX) {throw new jsptagexception ("simpletag:" ex.getMessage ());} Return Skip_body;

Public int doendtag () {returnval_page;}}

Code 5

Maybe you don't understand the following, it doesn't matter, let's take a look at the results. Compile the code, and then create a folder called Classes in the web-INF folder mentioned in the first partial article, and then add the following code to the CLASS folder after compiling. Add the following code. The Web.xml established in the first article is as follows:

/Database.tld /web-inf/database.tld

Code 6

This is to make the server know how to find your DATABASE.TLD (remember to put it in Web-IRI) by the logical name defined by taglib-uri. You can use "/Database.tld" logical name to quote Database.TLD.

Ok, I will notice it immediately, and the Hello.jsp of your original written is as follows:

<% @ Taglib Uri = "/ Database.tld" prefix = "database"%> <% @ page contentty = "text / html; charSet = GBK"%> <% @ page import = "java.io. *"% >

Hello </ title> </ head> <body bgcolor = "# 00ffff"> <h1> <database: simtag> </ database: simtag> </ h1> </ body> < / html></p> <p>Code 7</p> <p>OK! Now your home directory should be the following status:</p> <p>Home | | JSP | | Hello.jsp | | Web-INF | Classes | | Temp | | Simtag.class | Web.xml | Database.tld</p> <p>Ok, run Your Tomcat (see the first article in 127.0.1:8080/HOME/jsp/hello.jsp You can see the results. You will find that the result is the same as the last JSP completed in the second article, just the refreshing page. In the next part of the article, we will learn more about Custom Tag, and the meaning of Code5 in Code5 is obvious. Goodbye after the holiday!</p> <p>There is any opinions or refer to you, please contact me with my e-mail: flyhelleryuanwei@yahoo.com.cn.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-23710.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="23710" 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 = 'fc23AED11iS02dmwk8NfZ1ekyayc3jj93frtDArqGvZeunGPMuhtU1mrVGDy5CRhDD9P82UJwWXn_2Bh2sfjd0xA_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>