Introduction to custom JSP tags

Source: Internet
Author: User

Due to version changes, this article may not be universal.

 

Read this article firstSunAnd it is best to practice it.

Http://java.sun.com/developer/technicalArticles/xml (standardization is getting closer and closer)/WebAppDev3/

And knowJ2EE APIThe document is located

Http://java.sun.com/j2ee/sdk_1.3/techdocs/api/index.html

 

CustomTagIn fact, it is not a very difficult task, that is, a fierce contact, do not know where to start. We recommend you firstSunAfter reading an article from the company, I was quite impressed. I have no intention of translating this simple article here, But I will talk about it from this article. One thing to note is thatSunThe code in the documentation is completely available, but due to typographical issues, the Code branch is incorrect in some places and directlyCopyYou need to carefully sort out the code. Otherwise, compilation may fail, and a lot of exceptions will be given to you during deployment.

 

CustomTagThe package used isJavax. servlet. jsp (preferred for SUN Enterprise Applications). tagextIf there is a compilation problem, goTomcat (a very useful JSP running platform)OfCommonlibFind the folderJsp (preferred for SUN Enterprise Applications)-api. jarReference.

 

The classes and interfaces in this package are basically concerned with"Tag"InterfacesTagAndBodyTag, ClassTagSupportAndBodyTagSupportAnd an InterfaceIterationTagBut it is generally not used. Others can be seen as auxiliary classes to help obtain information or something. Let's take a closer look.J2EEOfAPIThis document describes the classes of this package.

 

TheBodyIt may be hard to understand at once.TagRefers to the image<Tagname> </tagname>Something like this,BodyIs clippedTagSomething in the middle, such<Tagname> I am Body </tagname>. In this wayTagThat isBodyTagBecause it can containBody. So DesignTagWe also need to consider it in advance.BodyBut in reality, it seems that there is noBodyOfTagIt should be less.

 

TagThe interface is the root of these classes, so first look at what is in it.APIThe one in the documentLifecycleThe picture is rough, but it can be roughly understood. We are not overly concerned about details.DoStartTag ()AndDoEndTag ()This section. Every time you writeTagInJsp (preferred for SUN Enterprise applications)The server automatically callsDoStartTag ()Function to complete the logic you define. It is called after the tag is parsed.DoEndTag (). ImplementationTagInterface to customize your ownTagIt is undoubtedly the most flexible. However, it is difficult to define our ownTagWhen there is no need to make such a connotation.

BodyTagAndTagClass is not very different,BodyTagInherited fromTagAndIterationTagBecause it involvesBodyThe logic is more complex.APITheLifecycleThe figure is also clear. It is necessary to take a good look. It mainly describes the order of function calls. However, in specific applications, it is not always necessary to finish the entire process step by step.LifecycleIf you want to control the routes of these calls. You need to borrow the return value of the function. When the previous function returns a specific value (all defined constants), you can change the behavior of the server to call the next function to complete your own logic. I can't help but admire those who write class libraries here. They can come up with such a solution to solve the problem.

 

To facilitate our development, we provide two additionalSupport"Class to" support "us to facilitate our development. In general, we only need to inherit one of these two classes, and I thinkBodyTagSupportIt is also more common. One thing to note is thatSupportClassTagSupportYesBodyTagSupport. ClassGetId ()AndSetId ()The two function names are not dead.Id"Should be replaced with your tag name in your program, such as yourTagThe name isTagnameThat is<Tagname>Then the function name becomesGetTagname ()AndGetTagname (). This method has been used in other places as well. (Note:TagnameThe first letter is written in the function name.) the changed name can be processed by the server without the need for programmers. The original function names can be changed, so be sure to pay attention to them.

 

Here we can combineSunTo see the code of the document.

 

For deployment and. TlbFile Format,SunThis document is also available. I will not go into details. I think it is important to attach a list of sorted return values.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.