| [ directory ] |
A few points about custom tags have been touched on, but there are many good reasons as to why custom tags are helpful. Before we get to how one goes about building a custom tag, it is helpful to understand why exactly you would want to build one. In summary, here is a formal list of the primary reasons:
Custom tags provide a method to cleanly separate logic from content: Custom tags and the scripting elements provide the same functionality. A JSP custom tag can do everything that can be done with a scriptlet. The difference is in how the two technologies are authored. Scriptlets directly embed code with chunks of static markup. Custom tags abstract code behind markup that resembles HTML. Scriptlets are not a good method for abstracting logic from formatting. A scriptlet often mixes both data-manipulating code and the code that is responsible for presenting data. This results in the JSP being overly complicated and requiring a competent Java developer to even create it. Custom tags move all of the logic into separate Java classes that are bound to simple tags. The logic can easily be manipulated without touching the JSP, and the simple tags can easily be used for authoring content.
Ease of use: Custom tags are easy to use. Both programmers and non-programmers can intuitively use custom tags. They are an ideal mechanism to enhance the Web tier of an application, especially when collaborating with a mixed group of developers. An HTML- savvy developer with no Java experience can easily pick up and successfully use a custom tag library. For this reason custom tags are a great way to interface complex logic into a simple presentation layer authored in languages such as HTML or XML.
Portability: Custom tags are portable. A complete set of custom tags can be packaged into a single JAR file and deployed across many Web Applications. Compared to scriptlets, this is a huge advantage, both when developing the code and when using existing custom tag libraries.
A good way to think of a custom tag is as a component, written in either Java or JSP, that encapsulates some behavior. To the page author, custom tags look like HTML, or at the very least XML. To a tag developer, a custom tag can either be a Java class or a fragment of a JSP. In all cases the syntax is very familiar and follows with the strong points of the respective language.
| [ directory ] |