Struts Tip #8 - Use result objects to transfer values
Web developers tend to spend a lot of time tinkering with how data is input into an application. This only makes sense -- after all, garbage in, garbage out -- right?
Absolutely! There is nothing more important then getting correct input from a user. Much of the Struts framework centers on gathering and validating input. But that's no reason to give output the short-shift. Presenting our hard-won data back to the user can be just as important as gathering it in the first place.
Most presentation components, like JSP tags and Velocity templates, are quite adept at rendering data from objects in a servlet context. So much so, that many developers just tuck their data into one of the standard Collection classes and leave the rest to the presentation layer. While this works, using a specialized result object works even better.
A result object is simply a transfer object equipped with methods designed to give the presentation layer a helping hand. Most often these are very simple methods, like a helper to return the size of the collection as a standard JavaBean accessor. Other common helpers could include a function to render text in all upper case, a function to localize a date for the instant user, or an accessor that transform a string of digits into a formatted telephone number.
Another good use of result object is to return the result of complex logical tests as a simple value. If you find yourself wanting to do ANDs and ORs with JSP tags, think about a helper method instead!
The result object itself is usually just a wrapper around some collection class. One good approach is to have your result object implement the Collection interface so that it can be used anywhere you are already using a Collection (or equivalent class). The ModelResult class in the scaffold package is one example [
org.apache.scaffold.model.ModelResult]. This class is optimized for use with another scaffold class, but the principles can be used with any type of data access object.
Here are some of the helpers it provides.
int getSize()- Return the number of elements on the result list.
String getDescription()- Return a description of the result list.
terator getIterator()- Return an iterator for the result list.
When used with the Struts JSP tags, it becomes very easy to do things like print a custom legend over the result and test whether the result was empty or not. The following JSP code prints a description of the result object, but only prints the table header when there is one or more element in the result.
<bean:write name="RESULT" property="size"/>
<bean:write name="RESULT" property="description"/>
<logic:notEqual name="RESULT" property="size" value="0" >
<logic:iterate name="RESULT" property="iterator" id="row">
<TD><bean:write name="row" property="article"/></TD>
<html:link forward="article" paramName="row" paramProperty="key"
<bean:write name="row" property="title"/>
<TD><bean:write name="row" property="contributed"/</TD>
When using the type of layered architecture Struts promotes, it's important to get as much generic code as possible out of the presentation layer and up into the control or business layers. If the result of method does not involve writing HTML markup, the code probably belongs in a helper method. This lightens the load for the page and makes it possible to reuse the helpers in another presentation environment.
You'll probably end up with the same amount of code either way. The question is where can you put the code where it can be best managed and reused. Once you start asking that question, you may be surprized how often result object helpers are the answer!
HTH - Ted.
----- Struts Tips are released weekly on the MVC-Programmers List. To subscribe, visit BaseBean Engineering. ----- MVC-Programmers mailing list
About Ted. Ted Husted is an active Struts Committer and one of the authors of Professional JSP Site Design. Ted also moderates the Struts mailing list and the JGuru Struts FAQ.
Copyright Ted Husted 2002. All rights reserved.
Struts Tips are released weekly on the MVC-Programmers List. To subscribe, visit BaseBean Engineering.
MVC-Programmers mailing list