(Transfer) Master the JSP custom label: (4)

zhaozj2021-02-16  84

Step 6: Declare Scriptlet Variables

Page 8 (9 pages a total)

To understand the Scriptlet variable, you must understand the role of the TLD file. This file is basically a repository of metadata. When the JSP page is converted to servlet, the tag uses these metadata. In the generated servlet, the Scriptlet variable is a local variable. To let the JSP conversion engine know that these variables should be declared, you need to add items below to add items in the TLD file: ID java. Util.map at_begin Before the above code snippet is placed behind the Body-Content element in the TLD file, the Attribute element before. Under the Variable Element, we declare three child elements: name-from-attribute, variable-class, and scope. Name-from-Attribute Specify the value of the id attribute is the name of the Scriptlet variable that the conversion engine will define. Variable-class is a class type that converts the variable to be defined. Scope specifies when the variable is available: it can nested into the body of the tag (NESTED), after the label (at_end), or at the beginning of the label (at_begin). We use at_begin scope that it means that the range of variables will be from the beginning of the label to the end of the current JSP page. Now I understand how to build a simple custom label. In the next section, we will analyze the labeling survival cycle method to understand the situation where the JSP page actual runtime.

Simple label survival cycle overview

Page 9 (9 pages a total)

If I first use the label for the first time, you may find that it is not easy to make the conversion engine survival cycle and the actual thing that actually happens. Like me, you may see a lot of charts that try to explain these concepts, which look like this. Here is the minimized image This chart doesn't make sense, but the generated code is meaningful, so we will analyze these code in this section. (Now I think these charts are meaningful, but I think these code can explain the problem. When we complete this section, you may want to analyze this chart again.) You may remember that the JSP page is actually camouflage servlet . The JSP file is converted to servlet before use. Below is a small JSP page called TestmapDefine.jsp, we will use it to show and test the mapdefinetag tag developed in the previous section: <% @ Taglib URI = "prefix =" MAP "%> test map define </ title> </ head> <body> <map: mapDefine id = "Employee"> <br /> The Employee IS <% = Employee%> <br /> <% Employee .put ("firstname", "kiley"); Employee.Put ("LastName", "Hightower"); Employee.Put ("age", new integer (33)); Employee.Put ("Salry", New Float (22.22));%> The Employee IS <% = Employee%> <br /> </ map: mapdefine> </ body> </ html> Note This page imports a custom label with MAP's URI. The reason why we can declare this TLD in the web.xml file, as shown below: <web-app> ... <taglib> <taglib-uri> map </ taglib-uri> <taglib-location> /Web-inf/tlds/map.tld </ taglib-location> </ taglib> ... there is another way to import tags. I think this method is more useful because it allows for a short name for the URI, and the short name is easy to remember. Note that taglib is described in its TLD file.</p> <p>The map.tld file in the web-inf directory looks like this: <? XML Version = "1.0" Encoding = "UTF-8"?> <! Doctype taglib public "- // sun microsystems, Inc.// DTD JSP Tag Library 1.2 // en "" http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd "> <taglib> <Tlib-Version> 1.0 </ TLIB-VERSION> <JSP-Version> 1.2 </ jsp-version> <short-name> Map </ short-name> <tag> <name> mapdefine </ name> <tag-class> Trivera.tags.map.mapdefinetag </ tag-class> <body- Content> JSP </ body-content> ... When the JSP converter encounters a custom label MAPDefine, it will query the TLD file according to the TAGLIB instruction and the TLD file specified in the web.xml file.</p> <p>Generated servlet then added the following code: public class _testmapdefine__jsp extends ... JavaPage {public void _jspService (HttpServletRequest request, HttpServletResponse response) throws ... {trivera.tags.map.MapDefineTag tag0 = null; java.util. Map Employee = NULL; try {... if (tag0 == null) {tag0 = new trivera.tags.map.mapdefinetag (); tag0.setPageContext (PageContext); tag0.SetParent ((javax.servlet.jsp.tageXT) .Tag) NULL); tag0.setid ("Employee");} int includebody = tag0.dostarttag (); if (includebody! = Javax.servlet.jsp.tagext.tag.skip_body) {Employee = (java.util. Map) PageContext.Findattribute ("Employee"); OUT.PRINT ("<br / / n the Employee IS" (EMPLOYEE) "<br / n"); Employee.Put ("firstname", " "); Employee.Put (" Lastname "," Hightower "); Employee.Put (" Age ", New Integer (33)); Employee.Put (" Salary ", New Float (222)); Out.print ("<br / / / n the Employee IS" "<br /> / n");} Employee = (java.util.map) PageContext.Findattribute ("Employee"); ...} Catch (java.lang.throwable _jsp_e) {pageco NTEXT.HANDLEPAGEEXCEPTION (_Jsp_e); ...} ...} The generated JSP servlet declares a local variable named tag0, type Trivera.tags.map.mapDefineTag. Then it creates an instance of the label, set the page, set the parent label as NULL, and set the ID as Employee. Then, the generated servlet calls the dostartTAG () method, which checks if the return type is set to tag.skip_body. If so, the container is not judged without the body of the label (here is IF block). If it returns Eval_Body_include, just like our label, the container will handle the body. The body of the label can be added to this technique - ie control flow. Note that the generated servlet has a local variable named Employee, set it to EMPLOYEE according to the ID attribute of the tag. Therefore, the conversion engine defines a local variable called EMPLOYEE when the conversion engine is not runtime. This concept makes a lot of newcomers confused. Understand nested label</p> <p>Page 1 (3 pages of)</p> <p>The previous JSP page example uses the JSP Scriptlet to add an item to the Employee Map. If you can do it with another label. Let us define a nested label called MapenTRYTAG, which is obtained by calling getParent () and converts it to MAPDEFINETAG. MAPDEFINETAG defines a getMap () method, which returns the newly created MAP.</p> <p>Nested MapenTryTAG in DoendTag () Use the mapDefineTag's getMap () method to add values ​​to MAP, as shown: public class mapenTrytag extends tagsupport {string type = "java.lang.string"; string id; string value; string name ; String property; String scope; public int doEndTag () throws JspException {/ * Grab the MapDefineTag using the getParent method and cast it to a MapDefineTag * / MapDefineTag mapDef = (MapDefineTag) this.getParent ();. Object objectValue = null; ... / * Instantiate a new string, integer or float based on the type. * / If ("java.lang.string")) {ObjectValue = Value;} else} else} else} else} else} else} else.equals ("Java .lang.integer ") {Integer INTVALUE = INTEGER.VALUEOF (Value); ObjectValue = INTVALUE;} else IF (" java.lang.float ")) {float floatvalue = float.Valueof (value); ObjectValue = floatvalue;} / * put the new entry INTO THE MAP. * / mapdef.getmap (); Ret (ID, ObjectValue); Return EVAL_PAGE; on the basis of the above code, and assume that necessary in the TLD file Item, you can use our label like this: <% @ Taglib URI = "MAP" prefix = "map"%> <html> <head> <title> test map Define entry </ title> </ head> <body> <map: mapDefine id = "employee"> <map: mapentry id = "firstname" value = "jennifer" /> <map: mapentry id = "lastname" value = VALUE = "Wirth" /> <map: mapentry id = "age" value = "33" type = "java.lang.integer" /> <map: mapentry id = "slu =" 22.22 "type =" java.lang .Float "/> </ map: mapDefine> The Employee IS set as <% = Employee%> <br /> The above list defines an Employee Map,</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-11712.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="11712" 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.046</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 = '0b0hUv2wAEW0wSGysF6oG_2BIcTKaVTYCyUUqJFIw1YotFPM0CZ0TvFcWz9qX5G2jxyHCMsA4Ko3oVTB1U'; 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>