| Knowledge Base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Inicio > Cursos > JSP-Taglibs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
JSP taglibs: Better usability by design
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Indice de contenido
JSP technology: Myths and alternatives
JSP and custom tag libraries
Designing a custom tag library
Who's the client?
Creating declarative tags
Advantages of using JSP custom tags
Conclusion: Aiming for usability
JavaServer Pages (JSP) technology is an excellent architecture for delivering Web applications, but one of its most important facilities, custom tag libraries, is often poorly leveraged. The primary reason for the failure to properly exploit the taglib facility is not technological, but linguistic. Tag library producers and consumers are not speaking the same language. JSP expert and consultant Noel J. Bergman exposes the nature of the problem and offers some workable solutions. It's generally agreed among Web application developers that separating the underlying content model from presentation is a good thing. In most large development projects, programmers implement the back end, and presentation is left to one or more Web page designers. This breakdown ensures a final product that is both technologically solid and well-presented, but it does require the two teams to communicate and work together effectively. This can be a challenge, given that each team is working from a different knowledge base and attending to a very different set of concerns. Prior to the introduction of custom tag libraries in version 1.1 of the JavaServer Pages (JSP) specification, it was necessary to use JSP scripting elements to provide any custom functionality within a JSP page. The explicit use of scripting elements violated the separation of model and presentation. It also required Web page designers to have Java coding experience in order to do anything more than retrieve properties from JavaBean components. This led to widespread concern over the use of scripting elements within JSP pages, which in turn led to the development of alternative solutions. One classic solution is the development of Model-View-Controller type usage models. Under such usage models, application intelligence is placed in servlets and beans, and JSP pages simply retrieve content for rendering. Jakarta Struts is a good example of this sort of model. Other development teams have created alternative technologies such as the Velocity content engine or Apache's Cocoon project. While these solutions are worthwhile in their own right, they are generally nonstandard, and they ignore the continued evolution of JSP technology. In this article we'll focus on one of the most underused facilities of JSP technology: custom tag libraries. My goal is to change the way you think about custom tag libraries and, more specifically, tag design. I'll start the discussion by dispelling a few myths about JSP technology and about some of the alternative solutions, and then move on to the central topic.
There are quite a few common myths about JSP technology, some of them as daunting to potential users as they are generally outdated. Table 1 looks at the most pervasive myths.
Each of the myths outlined above was at least partially true at one time. However, JSP technology is a major part of the J2EE specification and is widely used. As such, there is a tremendous multivendor effort to evolve and mature JSP technology to resolve user concerns. This is one of the benefits of using a standard technology instead of an ad hoc solution. Nonetheless, a common thread persists among managers responsible for deploying Web applications. Not knowing the truth behind the myths, upon being told that JSP technology intermingles programming with presentation, their first response is to say that they'll find an alternative to JSP, such as Velocity. So the question is, do solutions such as Velocity really solve the problem? Another popular alternative to JSP technology is the Cocoon project. Cocoon is a lovely technology that uses XML as its source data format. XSLT transforms are used to render the XML content into formats suitable for the user agent, for example, HTML for a browser. Cocoon has its own dynamic page format called XSP. XSP is a close cousin to JSP, but works within the Cocoon environment. XSP exists in large measure because there was no good way to send XML output from a JSP page through Cocoon on the way to the browser. The servlet chaining mechanism in early servlet containers was insufficient for the job. Today, however, Sun has introduced a new servlet filter mechanism that is perfectly capable of allowing XML output from any arbitrary servlet to be sent through a series of filters. From that perspective, the need for XSP is dramatically reduced, and the need for the Cocoon project to provide its XSLT transform engine as a servlet filter becomes apparent. The bottom line is that JSP technology is constantly evolving to meet the demands of its users. This means that any evaluation of the technology must involve careful review of the most recent specification. Reviews and editorials even a few months old can be rendered obsolete by new developments (see Resources).
JSP technology is not a tool, it is a platform: a fundamental dynamic-content architecture upon which tools are built. While it's true that the JSP platform includes scriptlets, declarations, and expressions, and its scripting language is Java by default, other languages are supported by the specification, and by an increasing number of implementations. In addition, the platform includes a rich architecture for developing tag libraries; anything that can be described by HTML or XML tags can be implemented using the standard JSP technology. The JSP platform's inclusion of scripting elements to connect front-end content presentation to back-end functionality does violate the premise that Web page designers should not have to be programmers. But the good news is that these scripting elements (declarations, scriptlets, and expressions) are only a quick fix, and in fact you don't have to use them at all (see Resources). Scripting elements are good for programmers, and good for rapid prototyping, but they are not the long-term solution. Custom tag libraries are the long-term solution. Scripting elements that are used to prototype solutions should migrate into tag libraries as part of the development process.
Custom tag libraries were introduced in version 1.1 of the JSP technology specification. The addition of this facility allows programmers to create XML tags that specifically address the needs of Web page designers, while hiding the underlying implementation detail. Taken as a set, a tag library's tags form a language: the syntax is predefined by the JSP specification to be XML, the elements are defined by the tag library's DTD or schema, and the semantics are provided by the library. The most important thing to understand when authoring a tag library is that tag design is language design. The onus is on the library designer to make sure that the language created by the tags is suitable for the front-end page designer, who must be able to understand and use the library's functionality. Unfortunately, the vast majority of tag libraries currently available do not employ semantics appropriate for non-programmers. These tag libraries are good examples of bad language design. They use the wrong semantics and a vocabulary that is ill-suited to their intended use.
As I mentioned before, the concept you're working with when it comes to authoring a custom tag library is that tag design is language design. You may be familiar with a slightly different variation of that expression, which stems from object-oriented programming. It states that library design is language design. A good class library designer creates class libraries that use a vocabulary derived from the problem domain. This is because the problem domain is easily understood by those who will be using the library. In the case of a class library, the library's user, its client, is a programmer by definition. In the case of a tag library, your client is a non-programmer. What this means is that you're designing a product for which you are ultimately not a domain expert. As in any development project, your first step should therefore be to call in one or more domain experts. Interview the domain experts, identify the real problems to be solved and, in concert with the domain experts, design a tag language using the vocabulary appropriate to that domain.
Most current tag libraries focus on verbs (actions) and adverbs (parameters) to produce their results, whereas I'd like to see more tag designers working with nouns (the desired result) and adjectives (modifiers). This entails a shift in perspective that may be difficult for many programmers, but it boils down to a very simple directive: focus on the what and hide the how.
You'll note from the listings above that there is no implementation specified on pages using custom tags. This is a very important point. The scriptlet and XSP solutions shown in Listing 1 and Listing 2 embed the solution within the presentation page. The declarative tag language does not. To understand why this is important, consider whose time is being measured. In both of the scripting examples, the time is the server's time. But what if we wanted to change the page to reflect the user's time? A page based on scriptlets or XSP would have to be rewritten. A page based on declarative tags could be tweaked to emit JavaScript. What if we wanted the option of reflecting either the server's time or the client's time? If we built our page using scriptlets, we'd have a mess on our hands. If we used a declarative tag language we could simply extend the tag language to (optionally) allow the following: <xyz:greeting machine="server or client"> ... </xyz:greeting> When rendering the server's time, the tag would provide the correct greeting. When rendering the client's time, the same tag might instead generate JavaScript to present the correct greeting. Another important advantage to using JSP custom tags arises when we consider presentation logic. People sometimes talk about the separation of presentation from logic, but what they're really talking about is the separation of presentation logic from the content model and business logic. Presentation logic needs to be encoded at the proper level of discourse for view production. For example, a user-agent tag should be easily understandable by a Web page designer. A declarative user-agent tag might allow such constructs as those shown in Listing 6. Listing 6. Sample user-agent tags <xyz:user-agent agent="opera | ie 5+ | ns 4+"> ... include contents only if the user-agent is one of the above ... </xyz:user-agent> <xyz:user-agent type="robot"> ... include contents only if user-agent is categorized as a robot ... </xyz:user-agent> <xyz:user-agent supports="JavaScript"> ... include contents only if user-agent supports JavaScript ... </xyz:user-agent> Another way to accomplish this sort of thing might be to generate XML, and have something like a Cocoon filter apply an XSLT transform based on the user-agent.
None of the examples in this article have focused on how the underlying Java technology works, or even whether or not the ultimate implementation occurs on the client or the server. The point, rather, is the usability of the technology for the end user. The nice thing about JSP is that, although ease of use is the goal, we need not sacrifice in the area of implementation. Something visually simple like: <xyz:input type="text" width="32" validate="[pattern]"/> could emit client-side field validation code. The generated page might incorporate complex JavaScript, but who would know? The underlying complexity would be hidden from the page designer and generated by the tag library. On the other hand, the "pattern" dialect does need to be appropriate for page designers. This means that, for example, regular expressions might not be the best choice of dialect to choose when designing the tag library, despite the fondness most programmers have for them. But the tag library could accept multiple forms for specifying patterns. Weighing the possibilities and making decisions that promote tag usability is the foundation part of designing a custom tag library. I think I have demonstrated that JSP provides considerable choice when it comes to the implementation of your tags; as with any development project, however, your first consideration when designing a custom tag library should be the end user. Copyright © 1998-2008 DEISTER, S.A. - Todos los derechos reservados. Modificado: 5-mayo-2011 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||