Learn about an adaptable approach which separates programming tasks from Web page design tasks. This strong conceptual model encourages good design, enables re-use of data definitions, and is well-suited to the construction of dynamic user interfaces. The authors also illustrate the particular challenges you might encounter when you dynamically change the analysis performed by Web pages. (This intermediate-level article was first published by IBM developerWorks, August 5, 2004, at http://www.ibm.com/developerWorks).
When filtering is applied to data, it is not uncommon to find that no rows of data match the filtering criteria. The page author might wish to deal with this and other exceptional occurrences gracefully. An ifDataState tag can be used to allow various common conditions to be anticipated, detected, and reported in a structured way. Listing 5 illustrates the use of ifDataState tags in a common pattern used to protect against empty result sets and access violations. Listing 5. A common error-handling pattern
<dd:ifDataState dataRef="sales" notAuthorised="true"> You are not authorised to access this sales data. </dd:ifDataState>
<dd:ifDataState dataRef="sales" rows="none"> Your selections have returned no data. </dd:ifDataState>
<dd:ifDataState dataRef="sales" rows="oneormore"> <%-- All seems OK, let's use the data here --%> </dd:ifDataState>
The example in Listing 5 assumes that identification, authentication, and authorization mechanisms are in place to match the user with access rights for the associated data. The details of such a scheme are beyond the scope of this article. What is important is that the problem has been encapsulated by the custom tag, and that the page author does not have to worry about the nasty details.
In this example, two cardinalities of result rows are then tested. Each cardinality test can only be satisfied if the authorisation test is met and data is successfully fetched. The first cardinality test recognises the empty result set situation. The next performs the data presentation if at least one result row is available. Other cardinalities might be tested for, but these are the most common.
Implementation notes
The JSP specification provides services which enable cooperating sets of custom JSP tags, such as those described here, to be developed and deployed successfully. This article is not intended to be a detailed survey of JSP custom tag development, but this section does address some of the principle issues in the implementation of sets of custom JSP tags of the type we have discussed here. It also provides pointers to appropriate parts of the API and the associated documentation.