It’s very tempting to set String and other simple data types (Date, Long, etc) as request or session attributes. It’s easy to program and a method for passing information from your classes to view objects (e.g. Struts actions to
jsp pages).The problem is, this can become unruly very quickly. Some pages end up with a long list of
jsp:
useBean’s making them long and hard to follow. It’s hard to enforce naming conventions. As pages get upgraded, it’s difficult to know what attributes have become obsolete. Finally, these attributes often get implemented with little or no documentation. Worse, if you have a chain of actions or multiple paths to a
jsp view, it’s hard to know what attributes are needed or are available.
There is a simple solution. Create value objects (VO’s) and add these to the request (or session) contexts. Then, create a naming convention for these VO’s. The VO classes can be used to document the definition and rules around these attributes and house any logic. If you’re using Eclipse then the overhead for creating the VO’s is very light. Create the VO, add your attributes, then let Eclipse generate the getters and setters.
Simple enough? Yes, but even my team at TripConnect has trouble keeping up with this standard. Why? That’s for another blog…
Leave a Reply