JSP tag library introduction

xiaoxiao2021-03-06  20

JSP tag library introduction

I have been programmed with JSP and ASP. In these two server-side programming solutions, I am more and more practical than ASP is much better than ASP. Why should I choose JSP to choose the preferred server-side web application development tool? Of course, JSP is fascinating, but the JSP label library is one of the most important causes of this decision. Why do you say this way? There are two aspects: maintenance and development speed. The server-side scripting language is actually a furnace that develops Internet. On a server page, you can easily mix a variety of different script methods and objects. This page is simply a concrete for building web. It is this "material" mixed to give the server-side script powerful information processing power. It allows server-side programmers to develop dynamic, flexible web pages. However, on the other hand, the free mixing of the script also has its disadvantage, which is very troublesome, especially as the scale of the project is growing. Worse, as the complexity of the code increases, the speed of development will slow down, which is not conducive to developing medium and large web applications, many medium-size or large server-side web applications are available very late and cost also Unable to control. In addition, once the development is completed, the site also finds qualified programmers to maintain these quite complicated code. As a result, these programmers have become a general web designer, and the server-side application is in the final graphic design and implementing these two aspects. It is weakened. In order to overcome this problem, ASP introduced COM object technology, and JSP provides J2EE as a countermeasure. These solutions are built on the centralized and reusable code library. However, they can be too difficult to use, and there is a lot of time spent on learning. Also, these solutions do not reduce the temptation of the establishment of chaos code. As a result, we can only organize a large, internal structure well-structured development team to use these technologies. For medium projects, usually use such methods less, but in fact, medium web application projects are most. Therefore, many projects have to use a development and maintenance environment that does not meet their needs. Fortunately, JSP provides a better way to solve this problem. Tag libraries offers a simple way to build reusable code blocks. Once the label is designed, it can be used again in many projects. More convenient, unlike COM and J2EE, as long as you know how to write JSP, you can build a label library without learning any other techniques! Finally, the label library also improves the maintenance of the web application. This improvement in maintenance is: easily implements XML-based customizable interfaces on the JSP page. The results can be imagined that the web designers can establish JSP web applications without knowing what JSP is going. In this way, Web development has become a very efficient team development task. JSP programmers can establish custom labels and backend code modules, while web designers can use custom labels and fully focus on the web design itself. The label library solves the problem of code confusion, and is clean and beautiful (in fact, XML is to solve the essence of these issues, but the label library still has a considerable role).

What is a label library? The JSP tag library (also known as custom label library) can be seen as a method of generating XML-based scripts through JavaBean. Conceptually, the label is a very simple and reusable code structure. For example, in our latest JSPKIT (in JSP Insider), use the XML tab to achieve easy access to the XML document. Please see the following list A. Listing A: Example tab of XML / XSL conversion and its HTML page <% @ Taglib URI = "http://www.jspinsider.com/jspkit/jaxp" prefix = "jaxp"%> C: / xml/example.xsl The above example is simple Tags to access the more powerful code behind the scene, the label section of the label section first loads an XML file, then applies an XSL file to convert the content in the XML file into a performance format, and send it to the client, this Everything is just a very simple label. Custom labels make it easy to create reuse open source modules in the JSP project, and you need just label libraries and its documentation description. The important feature of the label library 1. It is easy to install on multiple items to be labeled. It is easy to migrate from one JSP project to other items. Once a label library is established, you only need to pack all things as a JAR file, you can reuse in any JSP project. Because the tag can be reused, the label library can easily use your own project, so the label library is getting more and more. Currently, the best label resources can be found at the site of jsptags.com.

2. Extending the JSP tag library can have any features and features in the JSP specification (JSP 1.2), you can extend and increase the functionality of JSP without restrictions without limiting, without waiting for the next version JSP. For example, you are not satisfied with JSP's include call. You can build your own include label, which is your own specification.

3. Easy to maintain the label library makes JSP web applications are very easy to maintain, and the reasons are: (1) Label application is simple, easy to use for anyone, easy to understand. (2) All program logic code is set in the label processor and JavaBeans. This means that when you upgrade the code, there is no need to modify each page that uses the code, you only need to modify the code file. (3) If you need to add a new feature, you don't need to modify any already existing pages, you can join additional properties in the tab, which introduces new behaviors, and other old properties constant, so all old pages can be normal work. For example, you have a label that lets all texts to be blue: My Text But in the later project, you want to make the blue darker. You can keep the original label, just add a new attribute: shade, as shown below: my text All old labels can still generate blue text, but Now you can use the same label to create a dark blue text. (4) The label increases the reuse of the code. The code that has been tested and used must have fewer bugs. Therefore, the JSP page with custom labels also has fewer defects, which is naturally more convenient to maintain.

4. Fast Development Time Label provides a simple way to reuse code. In the language of the server, one of the standard reuse code is used to use the template. The label library is a better solution relative to the template library. With template libraries, you must modify templates for each project or create strict interfaces, and label libraries do not have these limits, and have all object-oriented benefits, you can make flexible and more scalability, and by reuse You can spend less time to develop, more time can be used in designing your web application. The label library interface is also very simple, it is very easy to do insert, use and debug. The structure of the label is very easy to use, but it is quite complicated to build a label library, but the at least is complicated than the establishment of a simple JavaBean. This complex is from the label library consists of several parts. However, you only need to master the knowledge of Java and JSP. A simple label consists of the following elements: 1. JavaBean: In order to get Java and the top-oriented benefits, reusable code should be placed in a separate code container, which is JavaBean. These JavaBeans are not an essential part of the label library, but they are the base code module for label libraries to perform allocated tasks. 2. Label processor: The label processor is the true core of the label library. A tag handler refers to any external resource (JavaBean) it needs and is responsible for accessing information on the JSP page (pageContext object). The JSP page passes the tag attributes set on the page and the contents in the label to the label processor. After the label processor completes its processing, it will send the processed output result to the JSP page to further process . 3. Label library descriptor (TLD file): This is a simple XML file that records information such as properties, information, and location of the label processor. The JSP container learns from this file and how to call a label library. 4. Web site web.xml file: This is an initialization file for the web site. In this file, you need to define the custom labels used in the Web site, and the TLD files used to describe each custom label. 5. Publish file (WAR or JAR file): If you want to reuse custom labels, you need a way to migrate it from a project to other projects. Packing the label library as a JAR file is a simple and effective way. 6. Label Library Declaration on the JSP page: To use a custom label in the JSP page, you need to use the tag library marker on the page on the page. It seems that there is a lot of work to do, and it will be a bit tricky when I started using it, but it is not very difficult. It is not yet encoded, but how to properly organize each part. This hierarchical structure is important, which makes the label flexible and easier to transfer. More important things, these levels can make the entire establishment of label libraries automatically complete through JSP IDE (JSP integrated development environment). JSP IDE can automate most of the work of creating custom tags, and you only need to be responsible for establishing code and label processors.

(Note: A label processor only defines a custom label; a label library is a collection of label processors that handle the same task) Create your first tab to teach you step by step to teach you how to build a custom label. The specific example is extended JSP, which has its own HTML encoding function. This feature replaces all characters with HTML code. It can be easily extended to do other coding processing. To simplify, this example only explains the basic elements of establishing a custom label. Any reusable part of creating a JavaBean code should be placed in a javabean. This is very important. This way you can reuse these codes in other projects. Since any code placed in the label processor is not reusable outside of the label, it is important to independently independently.

In this example, the logic for HTML encoding is common, so put in the JavaBean, please refer to the list B List B: HTML encoded javabean / * html_format.java * / public class html_format extends Object imports java.io.serializable {/ ** Create a new HTML_FORMAT * / PUBLIC HTML_FORMAT () {} / ** Replace all characters in a string instead of * / public string html_encode (string as_data) {int li_len = as_data. Length (); / * String Buffer length is higher than the original string long * / stringbuffer lsb_encode = new stringbuffer (li_len (li_len / 10)); / * Replacing all character * / for (int Li_Count = 0; li_count ")) ls_next ="> "; lsb_encode.append (ls_next);} return (LSB_Encode.toString ());}} Creating a tag processor tag processor code, please refer to List C: List C: HTML Code Label Processor import java.io.IOException; import javax.servlet.jsp *;. import javax.servlet.jsp.tagext *;. public class HTML_FormatTag extends BodyTagSupport {/ * 1) calls this function * / public int doEndTag at the end of the label () THROWS JSptagexception {Try {/ * 2) Get the text in the tab * / bodycontent l_tagbody = getBodyContent (); String LS _output = "; / * 3) If the label has text, it handles it * / if (l_tagbody! = null) {html_format l_format = new html_format (); / * 3A) Convert the content of the label to a string * / String ls_html_text = l_tagbody.getstring (); ls_output = l_format.html_encode;} / * 4) Write the result back to the data stream * / pageContext.get (). Write (ls_output.trim ()); } catch (ioException e) {throw new jsptagexception ("tag error:" E.TOString ());} / * Let JSP continue to process the content of the following page * / return eval_page;}} This process is simple, it includes there :1. Read the text 2 located between start and end tags. Call the HTML encoding function 3. Return the result to the JSP page.

Creating a label descriptor We need to describe a custom label to let the system know how to handle it. The suffix of this description file is .TLD, TLD file usually naming with a label processor and stores under the "/ web-inf /" directory. Please refer to the list D. Listing D: HTML Code Label Descriptor .0> 1.1 > html encoding tag HTMLENCODE html_formattag encode HTML Updating Web XML files can now tell the JSP container how to use the label library. To this end, you want to modify the web.xml file, specifically to join a taglib project to register the tag library and assign a URI for the label. URI is the only index of this particular tag on the Web site. Since the label can be used in different Web sites in the future, you should best adopt a complete URL and / or package name (package name) to ensure this uniqueness. This example is simplified, please refer to the list E for sample code. Listing E: Modify Web.xml file htmlencode /Web-inf/html_formattag.tld

转载请注明原文地址:https://www.9cbs.com/read-61121.html

New Post(0)