Use of custom markers in JSP

zhaozj2021-02-16  62

Use of custom markers in JSP

Summary

There is a mechanism in JSP that allows you to insert a mark similar to HTML in the JSP page. This article describes the basic concepts and compositions of JSP custom tags, and how to develop and apply JSP custom tags.

Keyword

JSP, XML, TLD, Marking

What is a tag?

We can edit our web page using HTML language:

</p> <p>Hello World</p> <p></ Title></p> <p></ HEAD></p> <p><Body></p> <p>Hello World</p> <p></ Body></p> <p></ Html></p> <p>Here we call </ head>, <title>, <body> as tag. HTML Markup is the control language of the HTML document, which is used to specify how to display and print documents. It is a phrase and symbol enclosed by smaller thanks from "<" and greater than numbers ">", such as <HTML> , </ Body>, etc. Many HTML markers appear in a pair, such as <title> </ title>, <body> </ body>, etc. In JSP, we can also customize your own tags for JSP page, as shown in the following example.</p> <p><! - login.jsp -></p> <p><% @ Taglib Uri = "/ TLDS / Taglib.tld" prefix = "tagclass"%></p> <p><html></p> <p><HEAD></p> <p><title> login </ title></p> <p></ hEAD></p> <p><body></p> <p><tagclass: login width = "200" height = "100"></p> <p></ tagclass: login></p> <p></ body></p> <p></ html></p> <p>In the above example </ tagclass: login> is a JSP custom marker. Widtht, Height is the properties of this tag. <% @ Taglib URI = "/ TLDS / Taglib.tld" prefix = "tagclass"%> is a tag library definition instruction, which we will discuss later. The tag is customized in JSP, which is essentially in the form of a markup, a separate functionality Java class. The use of tags reduces Java code directly embedded in JSP page, which is convenient for the layout of the page and facilitates the reuse of the code, and improves the efficiency of development.</p> <p>JSP server resolves the process of tagging</p> <p>So how does the JSP server parsed this tag when a tag is embedded in the JSP page? Let's take a look at it together:</p> <p>The meaning of each object in the figure is as follows:</p> <p>Client: Indicates the client.</p> <p>JSP-Server: JSP server.</p> <p>JSP-PAGE: JSP page.</p> <p>TLD: Tag library description file, define various attributes and processing files of tags and tags.</p> <p>Tagclass tag handler</p> <p>When a user accesses a JSP page, this request is sent to the JSP server, and the JSP server calls the corresponding page according to this request. If there is a custom tag in this page, the JSP service will be based on page instructions <% @ taglib > To access TLD to get information about the handler, then call the constructor method of the handler, start the tag handler, and read the attributes of the tag and the corresponding value. Call the corresponding set method for each of the properties. When the marker is used for the first time, it will not be set, so the set method is called for each attribute. After the property is set, the JSP server calls the dostartTAG () of the handler (), and then call the DOENDTAG () method. Finally, the JSP server will continue to process the remaining pages, call the Release () method to clean up all the resources occupied at the end of the page. TLD file</p> <p>TLD (TLD: TAG LIBRARY Descriptor Tag Library Descriptor) File, the standard XML format tag definition file, is used to store information about the logger, and below is a typical TLD file.</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p><! - XML ​​version and its character set -></p> <p>DOCTYPE TAGLIB</p> <p>Public "- // Sun microsystems, inc .//dtd jsp tag library 1.1 // en"</p> <p>"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"></p> <p><! - Document Type Definition -></p> <p><taglib></p> <p><! - This tag description We start describing a tag library -></p> <p><Tlibversion> 1.0 </ TLIBVERSION></p> <p><! - Tag Library Version -></p> <p><jspversion> 1.1 </ jspition></p> <p><! - The version of JSP used -></p> <p><ShortName> tagclass </ shortname></p> <p><! - Default Name -></p> <p><tag></p> <p><name> login </ name></p> <p><! - Name of the mark -></p> <p><tagclass></p> <p>Tagclass.login.login</p> <p><! - Handling the name of the corresponding class of this TAG -></p> <p></ tagclass></p> <p><info></p> <p><! - Description of this marker -></p> <p></ info></p> <p><Attribute></p> <p><! - Start Defining Tag properties -></p> <p><name> Height </ name></p> <p><! - Name of attribute -></p> <p><required> True </ reguired></p> <p><! - Indicates that this property is not necessary -></p> <p><RTEXPRVALUE> TRUE </ RTEXPRVALUE></p> <p><! - Indicates whether this property can be output with the result of the JSP program -></p> <p></ attribute></p> <p><Attribute></p> <p><Name> Width </ name></p> <p><required> True </ reguired></p> <p><RTEXPRVALUE> TRUE </ RTEXPRVALUE></p> <p></ attribute></p> <p></ tag></p> <p></ taglib></p> <p>In this TLD file, only one tagger library is defined, which calls an applet to verify the legality of the user. Treating this tag is tagclass.login.login. Width, Height is two properties of this tag. The attribute is the value sent as a parameter when using the tag. We can add several tags in the examples, or you can add several properties for each tag. When we develop marker libraries, it is not necessarily to start from scratch, write a new TLD. We can use an integrated environment or modify the above example. Taglib instruction</p> <p>Then how do it define a tag library when the JSP server is parsing a tagger? This is the main responsibility of the Taglib Directive.</p> <p>Taglib instruction</p> <p>Define a tag library and its custom tag.</p> <p>JSP syntax</p> <p><% @ Taglib Uri = "Uritotaglibrary" prefix = "tagprefix"%></p> <p>Example <% @ Taglib URI = "/ TLDS / Taglib.tld" prefix = "tagclass"%></p> <p><html></p> <p><HEAD></p> <p><title> login </ title></p> <p></ hEAD></p> <p><body></p> <p><tagclass: login width = "200" height = "100"></p> <p></ tagclass: login></p> <p></ body></p> <p></ html></p> <p>description</p> <p><% @ taglib%> The directive declares that this JSP file uses a custom tag, and references the tag library.</p> <p>It also specifies the prefix of their tags. You must use <% @ taglib%> instruction before using custom tags.</p> <p>Attributes</p> <p>URI = "URITOTAGLIBRARY": UNIFORM Resource Identifier (URI) The unique naming of the custom tag according to the prefix of the tag, the URI can be a relative or absolute path. Prefix = "tagprefix": Prefixed before the custom tag. As in the above example, </ tagclass: login></p> <p>Tag Handle's Tag Handle</p> <p>We still take an example to see how to implement a Tag Handle. The first is to look at its class diagram:</p> <p>Let's take another look at it:</p> <p>Package tagclass.login;</p> <p>Import javax.servlet.jsp.tageXt.tagsupport;</p> <p>Import javax.servlet.jsp. *;</p> <p>Import java.io. *;</p> <p>Public Class login extends tagsupport</p> <p>{</p> <p>Public login ()</p> <p>{</p> <p>Super ();</p> <p>}</p> <p>Public int dostarttag () THROWS JSptagexception</p> <p>{</p> <p>Jspwriter out = pageContext.getut ();</p> <p>Try</p> <p>{</p> <p>Out.println ("<Applet CodeBase = Applet / Login / Code = Login.class Width = 200 Height = 100> </ Applet>");</p> <p>}</p> <p>Catch (Exception E)</p> <p>{</p> <p>}</p> <p>Return Skip_body;</p> <p>}</p> <p>Publicc Int Doretag () THROWS JSPTAGEXCEPTION</p> <p>{</p> <p>Return Eval_page;</p> <p>}</p> <p>Public void release ()</p> <p>{</p> <p>Super.release ();</p> <p>}</p> <p>Public void setWidth (String Language)</p> <p>{</p> <p>THIS.WIDTH = Width;</p> <p>}</p> <p>Public String getWidth ()</p> <p>{</p> <p>Return this.width;</p> <p>}</p> <p>Public void setHeight (String Height)</p> <p>{</p> <p>THISHEIGHT = HEIGHT;</p> <p>}</p> <p>Public string getHeight ()</p> <p>{</p> <p>Return this.height;</p> <p>}</p> <p>PRIVATE STRING WIDTH;</p> <p>PRIVATE STRING HEIGHT;</p> <p>}</p> <p>From the above we can see that there are several requirements for achieving a simple tag handler: 1 Add a class to inherit the java.Servlet.jsp.tagext.tagsupport class. This class provides all methods required by java.servlet.jsp.tagext.tag interface. In addition, some basic APIs need to be used to allow JSP containers to call our own marker handlers. 2 You must create a get <Attribute> and Set <Attribute> method for each marker property, and the JSP container needs to use these method handler to pass parameters. 3 To build a constructor and self-desorring device for the marker handler. JSP needs to use the constructor to start the handler. The self-destruction is defined in the RealEase () method. At the end of the lifecycle of the handler, you need to call the resources occupied by the self-desorreler release. 4 Create two methods called dostarttag () and doendtag (), perform specific processing and output actions. These two methods are called at the starting position and end position of the custom marker. Their return value is a static int defined in the Tag Interface, which of these static values ​​are:</p> <p>Skip_body implies 0: skip the code between the start and end tags.</p> <p>Eval_body_include implicit 1: output the content of the body to the existing output stream</p> <p>Skip_page hidden 5: Ignore the remaining pages.</p> <p>EVAL_PAGE implies 6: Continue to perform the following page</p> <p>Of course, the marker also has its own shortcomings. It is very inconvenient package process, limited function. For some non-complex and functional logical descriptions, the parameters required to pass are not high, and the JSP tag is used to easily. For most commercial logic applications, it is also better to use Beans, and it is also suitable for servlet control.</p> <p>Appendix: Complete code for the example used in the article</p> <p>JSP code: login.jsp</p> <p><% @ Taglib Uri = "/ TLDS / Taglib.tld" prefix = "tagclass"%></p> <p><html></p> <p><HEAD></p> <p><title> </ title></p> <p></ hEAD></p> <p><body></p> <p><tagclass: login width = "200" height = "100"></p> <p></ tagclass: login></p> <p></ body></p> <p></ html></p> <p>Tag Description Library: taglib.tld</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p>DOCTYPE TAGLIB</p> <p>Public "- // Sun microsystems, inc .//dtd jsp tag library 1.1 // en"</p> <p>"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib></p> <p><Tlibversion> 1.0 </ TLIBVERSION></p> <p><jspversion> 1.1 </ jspition></p> <p><ShortName> tagclass </ shortname></p> <p><tag></p> <p><name> login </ name></p> <p><tagclass></p> <p>Tagclass.login.login</p> <p></ tagclass></p> <p><info></p> <p></ info></p> <p><Attribute></p> <p><name> Height </ name></p> <p><required> True </ reguired></p> <p><RTEXPRVALUE> TRUE </ RTEXPRVALUE></p> <p></ attribute></p> <p><Attribute></p> <p><Name> Width </ name></p> <p><required> True </ reguired></p> <p><RTEXPRVALUE> TRUE </ RTEXPRVALUE></p> <p></ attribute></p> <p></ tag></p> <p></ taglib></p> <p>Markup handler: login.java</p> <p>Package tagclass.login;</p> <p>Import javax.servlet.jsp.tageXt.tagsupport;</p> <p>Import javax.servlet.jsp. *;</p> <p>Import java.io. *;</p> <p>Public Class login extends tagsupport</p> <p>{</p> <p>Public login ()</p> <p>{</p> <p>Super ();</p> <p>}</p> <p>Public int dostarttag () THROWS JSptagexception</p> <p>{</p> <p>Jspwriter out = pageContext.getut ();</p> <p>Try</p> <p>{</p> <p>Out.println ("<Applet CodeBase = Applet / Login / Code = Login.class Width = 200 Height = 100> </ Applet>");</p> <p>}</p> <p>Catch (Exception E)</p> <p>{</p> <p>}</p> <p>Return Skip_body;</p> <p>}</p> <p>Publicc Int Doretag () THROWS JSPTAGEXCEPTION</p> <p>{</p> <p>Return Eval_page;</p> <p>}</p> <p>Public void release ()</p> <p>{</p> <p>Super.release ();</p> <p>}</p> <p>Public void setWidth (String Language)</p> <p>{</p> <p>THIS.WIDTH = Width;</p> <p>}</p> <p>Public String getWidth ()</p> <p>{</p> <p>Return this.width;</p> <p>}</p> <p>Public void setHeight (String Height)</p> <p>{</p> <p>THISHEIGHT = HEIGHT;</p> <p>}</p> <p>Public string getHeight ()</p> <p>{</p> <p>Return this.height;</p> <p>}</p> <p>PRIVATE STRING WIDTH;</p> <p>PRIVATE STRING HEIGHT;</p> <p>}</p> <p>Applet: login.java used in the tag handler</p> <p>Import java.awt. *;</p> <p>Import java.awt.event. *;</p> <p>Import java.applet. *;</p> <p>Public Class Login Extends Applet Implements ActionListener {</p> <p>PRIVATE STRING S_USERNAME;</p> <p>PRIVATE STRING S_USERPASSWORD;</p> <p>Private button b_ok;</p> <p>PRIVATE button B_REGISTER;</p> <p>PRIVATE LABEL L_USERNAME;</p> <p>PRIVATE LABEL L_USERPASSWORD;</p> <p>PRIVATE TEXTFIELD T_USERNAME;</p> <p>PRIVATE TEXTFIELD T_USERPASSWORD;</p> <p>Private GridLayout G_GridLayout;</p> <p>Public void init ()</p> <p>{</p> <p>B_ok = New Button ("OK");</p> <p>b_register = new button ("register");</p> <p>l_username = new label ("name");</p> <p>l_userpassword = new label ("password");</p> <p>t_username = new textfield ();</p> <p>T_Userpassword = new textfield ();</p> <p>B_ok.addactionListener (this);</p> <p>B_register.addactionListener (this);</p> <p>g_gridlayout = new gridLayout (3, 2, 10, 10);</p> <p>THIS.SetLayout (g_gridlayout);</p> <p>//this.setBackground ;Color.blue;</p> <p>Add (l_username);</p> <p>Add (t_username);</p> <p>Add (l_userpassword);</p> <p>Add (t_userpassword);</p> <p>Add (b_ok);</p> <p>Add (b_register);</p> <p>}</p> <p>Public Void ActionPerformed (ActionEvent EV)</p> <p>{</p> <p>String s_label = ev.getActionCommand ();</p> <p>IF (S_Label.equals ("OK"))</p> <p>{</p> <p>T_Username.Settext ("Name");</p> <p>}</p> <p>IF (s_label.equals ("register")))</p> <p>{</p> <p>t_userpassword.settext ("password");</p> <p>}</p> <p>}</p> <p>Public void Paint (Graphics G)</p> <p>{</p> <p>}</p> <p>}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24177.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="24177" 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.030</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 = 'BOctdibPLDzmk0gJKNvHV3wWd3JBSBxfl0ImAcRb6ZSEKXeb_2FQK6MxqSjvzJ8lFAHqbPhjWsTj6MMXfiKaXgaQ_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>