A brief discussion on custom JSP tags (tag)

Source: Internet
Author: User
Tags continue documentation interface tag name tagname
JS because of the change in the relationship between the version, this article may not be universal



Before reading this article, you should read Sun's article and try to practice it.

http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/

And know that the location of the Java API documentation is

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



Custom tag is not a very difficult thing, is a fierce contact, do not know where to start. The recommended first look at the Sun Company's article, after reading, has a general impression. There is no intention to translate this simple article, but from this article unfolds to say. The point is that the code in Sun's documentation is completely available, but because of typesetting problems, the code branches are not correct, direct copy of the time to carefully tidy up the code, otherwise it may not compile, deployment will give you a lot of exceptions.



Custom tag used by the package is Javax.servlet.jsp.tagext, if the compiler has problems, you can go to Tomcat \common\lib folder to find Jsp-api.jar reference on it.



This package inside the class and interface, basically our main concern is to "tag" the end of the several, interface tag and Bodytag, Class TagSupport and Bodytagsupport, there is an interface Iterationtag, but generally will not be used. The rest of us can be seen as auxiliary classes that help get information or something. We can take a closer look at the class description of the package in the Java EE API documentation.



The meaning of the body in the above class name may be difficult to understand all of a sudden. Tag refers to things like <tagname></tagname>, the body is sandwiched between the things in the tag, such as <tagname>i am body</tagname>. In this way, this tag will become a bodytag, because it can bring body. So design tag should also be considered in advance with the body, but the reality, as if there is no body tag should also be relatively few.



The tag interface is the root of these classes, so let's see what's inside. The lifecycle picture in the API document is rough, but it can be seen in general. We do not dwell too much on details, and the general concern is doStartTag () and Doendtag (). Whenever you write the tag in the JSP page to be interpreted, the server automatically calls your class inside the doStartTag () function to complete your defined logic, when parsing the label after the call Doendtag (). The implementation of the tag interface to customize their own tag is undoubtedly the most flexible. However, the difficulty is also relatively large, we have to define their own tag when there is no need to make such a connotation.

Bodytag and tag classes are not very different, Bodytag inherited from the tag and Iterationtag, because the body involved in the reason, logic to be more complex. The lifecycle diagram inside the API documentation is also clearer. It is necessary to have a good look. The main description is the order of function calls. However, in specific applications, it is not necessary to step through the entire lifecycle, if you want to control the route of these calls. To borrow the return value of the function, the previous function returns a particular value (all defined constants) to change the behavior of the server to invoke the next function to complete your own logic. See here I can not help but admire those who write class library, can come up with such a way to solve the problem.



In order to facilitate our development, but also provides two with "Support" class to "support" us, facilitate our development. In general, we just inherit one of these two classes, and I think bodytagsupport is more commonly used as well. The point to be explained is that the TagSupport in the two support classes is the Bodytagsupport parent class. The GetID () and SetID () in the class are not dead, and these two "IDs" should be replaced with your tag name in your program, such as your tag name is tagname, or <tagname&gt, then the function name becomes Gettagname ( ) and Gettagname (). This kind of way originally in other place also has the taste. (note tagname The first letter is capitalized in the function name) the name of the change is the server itself to deal with, without the programmer to worry about themselves. The original function names can be changed, we must pay attention to.



Speaking of this can be combined with the sun's code to look at the document, you understand.



The format for deployment and. tlb files is also available on the Sun documentation. Don't repeat it. Finally attach a list of sorted return values, which I think is more important.



English evaluate is roughly "evaluate, calculate" meaning, here we can understand as the server "read, read, processing" meaning, translation when I will adapt to the context, the following underlined, if there is improper, please correct me.




Eval_body_include: Read the body into the existing output stream, the doStartTag () function is available


Eval_page: Continue processing page, Doendtag () function available


Skip_body: Ignoring the handling of the body, the doStartTag () and Doafterbody () functions are available


Skip_page: Ignores processing of the remaining pages, Doendtag () function is available


Eval_body_tag: Abolished, replaced by eval_body_buffered


Eval_body_buffered: Application buffer, the setbodycontent () function of the Bodycontent object to deal with the body of tag, if the class implemented Bodytag, then doStartTag () available, otherwise illegal


Eval_body_again: Request to continue processing the body, return from Doafterbody (), this return value is useful when you make the loop tag. I observed the source code of JSTL, want to see how it's a foreach, but a look inside the logic is unusually complex, short time can not see the clue, so here also dare not to say, if any one of my fellow has research, but also please enlighten.



: The other classes in the package have not been studied, and if there is any research, write a separate document.




Related Article

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.