Only introduce the use of HTML tag libraries in strut (first part)

xiaoxiao2021-03-06  104

Improve development speed with label library

Struts provides a powerful HTML tag library to help you build a web application.

BY Budi Kurniawan

In order to make it easier, develop, Struts provides five label libraries similar to other JSP tag libraries, namely: HTML, Bean, Logic, Template and Nested. . The first part tells how to configure a Struts application to use this tag library and introduce the first group of labels; the second part tells the second set of labels;

The HTML tag library is mainly used to display HTML elements. If you don't have this method, you must use the HTML tag to specify them. From the surface, this label library is simple. But through in-depth research, we will find it very powerful. For example, it can place a key content to generate JavaScript in a specific input element of an HTML form, or enter a verification generated script for the client. Moreover, you can use it to handle errors through a line of code. However, before you use this tag library for your Struts application, you need to do some simple preparations.

Configure the application to use the tag library to configure a Struts application through three steps before using the Struts HTML tag library.

1. Register the label library in the deployment descriptor (web.xml file):

/web-inf/struts-html.tld

/web-inf/struts-html.tld

The above code told the Servlet Container about the situation of the Struts HTML tag library, and where to find the TLD file of the label library.

2. Confirm that the struts-html.tld file is copied to the web-inf directory. You don't have to worry about label library class files because they are already included in the struts.jar file.

3. In each JSP page using the label library, insert the following indicator:

<% @ Taglib Uri = "/ Web-INF / STRUTS-HTML.TLD"

Prefix = "html"%>

In addition, you can also use struts-html.tld files to learn what it supports the label and which properties can be taken.

It is usually easy to use a custom label library: as long as you follow the steps described above. However, some tag libraries contain some principles that make us not so easy when using these labels. The Struts HTML tag library is one of them. Some tags are simple, easy to use; however, some labels depend on other tags or other elements in the Struts application.

I am roughly divided into two categories in the HTML tag library: simple, easy to use tags, I call them "independent tags", and the second class is the label that must be used through a FORM label. I simply be simply called the second class of labels related to Form. This article mainly tells the independent label, and I will tell the form-related label in this series of articles.

The HTML tag library contains several easy-to-use independent labels. Here, there are several important labels.

Tags Tag is the easiest label in the HTML tag library. It has two properties: Locale and XHTML, both of which are not required. The following code is selected from a JSP page using the tag:

<% @ Taglib Uri = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

Welcome </ Title></p> <p></ hEAD></p> <p><body></p> <p>Hello World!</p> <p></ body></p> <p></ html: html></p> <p>Note that we have no standard <html> elements, but use the <HTML: HTML> tag. The first HTML refers to the prefix, the second is the <html> tag itself. And we also used the local property. The JSP page will be displayed:</p> <p><html lang = "en"></p> <p><HEAD></p> <p><title> Welcome </ Title></p> <p></ hEAD></p> <p><body></p> <p>Hello World!</p> <p></ body></p> <p></ html></p> <p>Note that <html: html local = "true"> is converted into LANG = "En" in the generated HTML page. The resulting result depends on the locale of the server located at the Struts application. If you deploy an application to a different locale server, you don't need to change the code. Locale will automatically adjust.</p> <p><base> Tags <base> Tag Displays an HTML element that represents the absolute position of the included JSP page with an HREF property. It is only valid when this tag is embedded in a Head tag portion. For example, this JSP page:</p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><html: html locale = "true"></p> <p><HEAD></p> <p><title> Welcome </ Title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><body></p> <p>Hello World!</p> <p></ body></p> <p></ html: html></p> <p>Convert to the following form:</p> <p><html lang = "en"></p> <p><HEAD></p> <p><title> Welcome </ Title></p> <p><base href = "http://www.domain.com/mystrutsapp/testing.jsp"></p> <p></ hEAD></p> <p><body></p> <p>Hello World!</p> <p></ body></p> <p></ html></p> <p><IMG> Tags <IMG> Tags are used to generate an HTML <IMG> tag. Its most important properties are:</p> <p>· Page: The image file is relative to the path of the module; there must be a slash.</p> <p>· Height: The height of the image.</p> <p>· Width: The width of the image.</p> <p>· Alt: If the image is not found, the text is displayed.</p> <p>E.g:</p> <p><html: img Page = "/ logo.gif" Height = "50"</p> <p>Width = "200" alt = "Company logo" /></p> <p>Convert to: <img src = "/ mystrutsapp / logo.gif" Height = "50"</p> <p>Width = "200" alt = "Company logo"></p> <p><LINK> Tags <LINK> tab generates a hyperlink. For example, tag:</p> <p><html: link page = "/ index.html"> click</p> <p>Here </ html: link></p> <p>Will convert to:</p> <p><a href="/mystrutsapp6/index.html"> Click</p> <p>Here </A></p> <p><ERRORS> Tags <Erroors> The easy usability of the tag usually masks its true powerful function. Through a simple <HTML: ERRORS /> tag, you can display a full-defined error message on a JSP page.</p> <p>This tag checks for the property collection of the Request object to find a reserved key. If it finds a reserved key, it assumes that this key is a string, or a string array (which contains Message Keys in the module's MessageResources, or the type ORG.Apache.Struts.Action.ActionerRRRUTS.Action.Actionerror Object.</p> <p>If there is a corresponding information in the application resource, you can use the following optional Message Keys:</p> <p>Errors.Header: The corresponding information is displayed before the individual list of the error message.</p> <p>· Errors.footer: The corresponding information is displayed after the individual list of the error message.</p> <p>· Errors.prefix: The corresponding information is displayed before the individual list of the error message.</p> <p>· ERRORS.SUFFIX: The corresponding information is displayed after the individual list of the error message.</p> <p>The Struts HTML tag library will make development easier and faster. In order to effectively use this tag library, you need to be familiar with the label in the label library. In the first part of this series, you learned how to configure a Struts application to use the label library and have an overall understanding of the independent tag. In the second part, you will learn the labels related to Form;</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-123729.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="123729" 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.038</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 = '13M7FWuY9RUYanmS4bcTo2gC1yArVxf1TEXgYfd8VApi8rg4wFCkmzfLKhqzSg1ZBzLTk7MhEhbeXQBhYl4dlg_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>