JSP and XML combination (1)

xiaoxiao2021-03-14  205

Overview: Extensible Markup Language (XML) is being quickly applied to the industry, which has been used as a wide range of application standards that describe and exchange data regardless of platform, language and protocol. XML and its auxiliary specification can be used to describe document performance of data, describing XML document type limits, describing links between XML documents and resources, describing the automatic conversion and formatting of XML documents.

How to develop a custom label library?

I have been using JSP and ASP programming has been a long time. In the two server-side programming methods, I have more and more practical functions of JSP. Not mentioned, the JSP label library is the reason I choose JSP as the preferred server-side web application development tool. why? Because: maintenance and development speed. In a single server page, you can mix a variety of different script methods and objects. Like? Quot; Concrete, this mixing can make the server-side scripts powerful, and let the server-side programmer design a very flexible and dynamic web page. But this free mix also has its shortcomings, that is, maintenance Very trouble, especially when the project gradually became bigger. Since the final product is maintained via a traditional web designer, it will bring problems. Worse, with the complexity of the code, the speed of development It will slow down, which is not conducive to the development of medium and large web applications. Once the development is completed, the site also finds qualified programmers to maintain these quite complicated code.

Fortunately, JSP provides a good solution. The label library provides a simple way to create a reusable code block. Once the label is designed, it can be used again in many projects. More convenient, unlike COM and J2EE, you can build a label library without learning any other techniques! As long as you know how to write JSP, you can build a label library. Label libraries can also improve the maintenance of web applications. This is a simple XML interface that benefits from the JSP page custom tag. In this way, the Web designer can even do any knowledge of any JSP, you can establish a JSP web application. This open web development is very effective for teamwork. JSP programmers can create custom tags and backend code modules, while web designers can use custom tags to create web applications and concentrate on web design.

1. Definition of the label library

The JSP tag library (also known as a custom library) can be seen as a set of methods that generate XML scripts, which is supported via JavaBeans. Conceptually, the label library is very simple and can be reused code construct.

Execute an XML / XSL conversion tag example and HTML page

<% @ Taglib URI = "http://www.jaxsider.com/jspit/jaxp" prefix = "jaxp"%> c: /xml/example.xmlc: / xml/example.xsl

In this example, by using a simple label to access the background more powerful code, an XML is loaded, and a result is generated by an XSL file, and send it to the client, all by using a simple label call. Arrived.

Custom label opens a door to create an easy-to-reuse code in the JSP project. What you need is just the label library and its documentation.

2. Label components

Although the label library is very easy to use, it is quite complicated to establish an internal design to support the label library, and 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 know that Java and JSP have enough knowledge.

A simple label consists of the following elements:

(1) JavaBeans: In order to get Java and the object-oriented benefits, reusable code should be placed in a separate code container. These JavaBeans are not part of the label library. However, it is the basic code block for your code library to perform related tasks. (2) Tag processing: This is the true core of the label library. A tag processor will reference any of its resources (your JavaBeans) and all information to access your JSP page (pageContext object). The JSP page also transmits all the tag properties that have been set and the contents in the label on the JSP page to the label processor. After the label processor is processed, it will send it back to your JSP page for processing.

(3) Description of the label library (TLD file): This is a simple XML file that records the properties, information, and location of the label processor. The JSP container learns from this file and how to call a label library.

⑷ Web.xml file: This is an initialization file for your website. In this file, you define the custom label used in the website, and which TLD file is used to describe each custom label.

⑸ Distribution file (a WAR or JAR file): If you want to reuse custom labels, you need a way to transfer it from a project to another. Packing the label library as a JAR file is a simple and effective way.

⑹ Be a label library declaration in your JSP file: very simple, if you want to use the tag, you can use it if you can declare on the page, then you can use it anywhere in the JSP page.

It seems that there is a lot of work to do, but it is not very difficult. It is not yet encoded, but how to properly organize each part. However, such layers are important, it makes the label flexible and easier to transfer. More importantly, the presence of these layers allows processing of the establishment of labels to be automated through a JSP IDE (JSP integrated development environment). It is expected that the future JSP IDE automatically completes most of the work of creating a custom label so you only need to write code and label processing.

Note: A label process only defines a custom label; a label library is a collection of label processors that handle the same task.

3. Establish your own label

Here you will teach you step by step to create a custom label, and 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.

(1) Creating a Javabean

Any reusable part in your code should be placed in a JavaBean. This is very important because you have to use these code elsewhere in the project. Any code placed in the label processor cannot be reused outside the label, so it is important to be independent of the reusable code part. In this example, the logic encoded for HTML is commonly used, so put it in JavaBean.

(2) HTML encoded Javabean

/ * Html_format.java * / public class HTML_FORMAT EXTENDS Object IMPLEments JAVA.IO.SERIALIZABLE {/ ** Create a new HTML_FORMAT * / PUBLIC HTML_FORMAT () {} / ** All characters all in a string Instead of HTML-encoded response * / public string HTML_Encode (string as_data) {int li_len = as_data.length (); / * length of the original string buffer than the string length * / StringBuffer lsb_encode = new StringBuffer (li_len (li_len / 10)); / * Replace all characters * / for (int Li_count = 0; li_count

The label processor uses the following code:

HTML tags encoding processor import Java.io.IOException; import Javax.servlet.jsp *;. Import Javax.servlet.jsp.tagext *;. Public class HTML_FormatTag extends BodyTagSupport {/ * 1} at the end of the label will call this function * / public int doendtag () THROWS JSptagexception {Try {/ * 2} Get text * / bodycontent l_tagbody = getBodyContent (); string ls_output = ""; / * 3} Processes if the label has text / if (l_tagbody = null!) {HTML_Format l_format = new HTML_Format (); / * 3a} converts the contents of the tag body to a string * / string ls_html_text = l_tagbody.getString (); ls_output = l_format.HTML_Encode (ls_html_text) } / * 4} Write the result back to the data stream * / pageContext.get (). Write (ooException 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, which includes:

o Read the label start and end of the end

o Call the HTML encoding function

o Returns the result to the JSP page.

⑷ Create a label descriptor

Custom labels need to be described to let the system know how to deal with. The suffix of this description file is .TLD, usually its name and tag processor, and store it in the "/ web-inf /" directory.

HTML Code Label Description < Taglibian (TLIBVERSION>1.0> 1.1.1 "/ j p> < a a h _ _ <<<<<>>> < URI> html encoding tag htmlencode html_formattag encode html ⑸ update Web XML file

You can now tell the JSP container to use the tag library. To this end, we must modify the web.xml file, specifically, to join a Taglib project to register the tag library. Most importantly, you have to assign a URI for Tag. The URI is a unique reference that applies only on this special label on the site. Using a full length URL or a package name is a good habit, it can ensure uniqueness because the tag can be used in different sites. This example is simplified.

Modify Web.xml file < Tagl Man "Taglib-Location) -Inf / html_formattag.tld

⑹ use new labels

Customized tags have been set, you can use on a JSP page. To do this, just use the Taglib command to declare the tag in this page, the tag is referenced by its unique URI and will be assigned a namespace prefix. The prefix can be arbitrarily, as long as it does not conflict with other namespaces.

Use an HTML encoded tag on a JSP page:

<% @ Taglib Uri = "HTMLENCode" prefix = "examples"%>

      Examples of sample code  Which displays as:  I encode all the code of this page. Interesting is that all custom labels are processed on the server. This means you will not see the custom label on the output page. Creating a label is not very difficult. The most difficult part is to learn all the details of the label processing. This is a very powerful function, we just mention the most basic place. Since this processing takes a few steps, the new JSP programmer will be confused when creating a label.

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

New Post(0)