Rabbit Eight Brother Notes 3: JSP Custom Label Test

zhaozj2021-02-16  47

I. Overview

There is an important technique in JSP: Custom Tag, which has been found in Struts, such as HTML, beans, etc. in Struts in recent days. So I have made a simple trial and learn this technology.

First introduce this technology!

1. advantage:

Replaced the JAVA program in JSP and can be reused, which is easy to familiarize with Java programming web designers.

2. Development Process:

(1) Write JSP and use a custom label in JSP.

(2) The location of the TD (Label Description File) file used in the JSP in the JSP is specified in Web.xml.

(3) The class specifies the class used in TLD file.

3. Customized tag classification:

(1) Simple tag: such as

(2) With attribute tags: such as >

(4) Tags that can be used by Script:

The label defined by the id and Type property can be used by the scriptlet behind the label.

<% ORADB.GETCONNECTION ();%>

4. Interface and other

In fact, the custom label processing class implements the Tag Handler object. JSP technology provides multiple Tag Handler interfaces in javax.servlet.jsp.tagext, and TAG, BODYTAG, ITERATIONTAG interfaces are defined in JSP1.2, which adds the SimpleTAG interface in JSP 2.0. JSP also provides the implementation of the above interface Tagsupport, BodyTagsupport, and SimpleTagSupport (SimpleTagsupport only in JSP2.0). Bodytagsupport implements the BodyTag, Tag, and IterationTag interfaces.

Interface and its method

TAG interface

method

SIMPLETAG DOTAG DOSTARTTAG, DOENDTAG, Release Itertag Dostarttag, Doaftertag, Release BodyTAG DOSTARTTAG, DOENDTAG, RELEASE, DOINITBODY, DOAFTERBODY

The table below is from the JSP online tutorial of Sun.

Tag Handler Methods

Tag Handler Type

Methods

SIMPLE

Dostarttag, Doendtag, Release

Attributes

DostartTag, Doendtag, SET / GetAttribute1 ... n, releasebody, evale and no ortho interaction

Dostarttag, Doendtag, Release

Body, ITERATIVE EVALUATION

DostartTag, DoafterBody, Doendtag, Release

Body, Interaction

Dostarttag, Doendtag, Release, DoinitBody, DoafterBody, Release

The EVAL in the table below is an abbreviation of Evaluate, meaning: evaluation, estimation, seeking the value of the ..., the meaning in the following return value is executed.

Return Value Skip_body indicates that the DOENDTAG () method is directly called without the processing tab. Skip_page ignores the JSP content behind the label. After the EVAL_PAGE processes the label, continue to process the contents of the JSP. Eval_body_buffered indicates that the label is required. Eval_body_include indicates that you need to process tabs, but bypass setBodyContent () and DOINITBODY () method EVAL_BODY_AGAIN to loop the label cycle.

For specific usage, you can see other references.

SUN Java Tutorial Related section: http://java.sun.com/webservices/docs/1.0/tutorial/doc/jsptags.html

Second, experiment

1. Test introduction

The following experiment is based on the above development processes.

(1) Specify the URI: <% @ taglib URI = "/ helloworld" prefix = "mytag"%> of the JSP.

(2) Configure Tag-location in Web.xml:

/ helloworld

/web-inf/helloWorld.TLD

(3) Define the processing class that implements the tag in the .tld file specified in tag-location:

MyTag

HelloWorld

mytag.helloworldtag

EMPTY

(4) Perform the DostartTAG and DOENDTAG method of processing class mytag.helloworldtag, and then enter the result into the JSP, and output together with the contents in the JSP. In fact, custom labels and other content in JSP are compiled into servlets with WebServer.

2. Directory structure of the completed test

Apply MYJSP under WebApps of Tomcat.

The MyJSP contains J2EE standard directory structure: web-inf and hello.jsp. Web-INF contains subdirectory classes and lib and web.xml, and the TLD file can be placed under Web-INF, or it is placed under the sub-directory of Web-INF.

3. Start experiment

3.1. Write JSP

<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ Taglib Uri = "/ HelloWorld" prefix = "mytag"%>

</p> <p>JSP1</p> <p></ title></p> <p></ hEAD></p> <p><body bgcolor = "# fffc0"></p> <p><h1></p> <p>The following shows the content in a custom label</p> <p></ h1></p> <p><br> <br></p> <p><MyTag: HelloWorld> </ mytag: helloworld></p> <p><br></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>3.2. Write web.xml</p> <p><! -Web.xml source code -></p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><! - edited with xmlspy v5 rel. 4 u (http://www.xmlspy.com) by Williams (501) -></p> <p><! Doctype web-app public "- // sun microsystems, inc.//dtd Web Application 2.3 // en"</p> <p>"http://java.sun.com/dtd/web-app_2_3.dtd"></p> <p><web-app></p> <p><taglib></p> <p><taglib-uri> / helloworld </ taglib-uri></p> <p><taglib-location> /web-inf/helloWorld.TLD </ taglib-location></p> <p></ taglib></p> <p></ web-app></p> <p>3.3 Write a TLD file</p> <p><! -Helloworld.tld's source code -></p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p><! Doctype taglib public "- // Sun microsystems, Inc.//dtd jsp tag library 1.2 // en</p> <p>"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"></p> <p><taglib></p> <p><TLIB-VERSION> 1.0 </ Tlib-Version></p> <p><JSP-VERSION> 1.2 </ jsp-version></p> <p><Short-Name> MyTag </ Short-Name></p> <p><tag></p> <p><name> HelloWorld </ name></p> <p><tag-class> mytag.helloworldtag </ tag-class></p> <p><body-content> EMPTY </ body-content></p> <p></ tag></p> <p></ taglib></p> <p>3.4 Writing a label implementation class</p> <p><! - Label implements the source code of HelloWorldTag.class-></p> <p>Package mytag;</p> <p>Import java.io.ioException;</p> <p>Import javax.servlet.jsp. *; import javax.servlet.jsp.tagext. *;</p> <p>Public class helloworldtag extends tagsupport {</p> <p>Public helloworldtag () {</p> <p>}</p> <p>Public int dostarttag () THROWS JSptagexception {</p> <p>RETURN EVAL_BODY_INCLUDE;</p> <p>}</p> <p>PUBLIC INT DOENDTAG () THROWS JSptagexception {</p> <p>Try {</p> <p>PageContext.getOut (). Write ("Hello World");</p> <p>}</p> <p>Catch (IOException EX) {</p> <p>Throw new JSptagexception ("error");</p> <p>}</p> <p>Return Eval_page;</p> <p>}</p> <p>}</p> <p>3.5 Performing effect</p> <p>To deploy to Tomcat's WebApps directory, start Tomcat, enter: http: // localhost: 8080 / myjsp / hello.jsp</p> <p>"Hello World" is the result of the processing class output of the label part we define.</p> <p>3.6 Note:</p> <p>I did 2 days, always report an error, making it very discouraged, I thought the TLD file or web.xml file configuration was not correct, but I couldn't find why.</p> <p>Tonight, I finally found the reason, because .class file must be placed in the Classes folder, I put it in the lib.</p> <p>.jar or servlet files are placed in the lib directory, and .class is in place in the class content, if you want to put it in the lib directory, you must pack the file in MyTag into .jar file, then put myTag.jar in LIB Directory.</p> <p>I hope you don't make this mistake I have made! ^ _ ^</p> <p>I have time I will write a note of the Struts detailed processing process.</p> <p>This note is indeed a custom label of JSP1.2, which has changed in the format of the XML file in JSP2.0, and the other is not known! But I want to change it too much, and the other standard is compatible, interested friends can take a look, this is my study notes, may have some use of others, so it is dedicated!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-28045.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="28045" 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.040</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 = '4XHarZGLQrWri7NYf8LudEEU_2FvYEK07mCzjz2G8DqKV4zPJRq3_2BrD_2F2VP6sWn_2BLrOXO7KzQX4oksF4a_2FQebobg_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>