I'm trying to learn Struts by building a noddy web application. The middle tier retrieves information from the database in the form of transfer objects [TO]. This information needs to be displayed in the presentation layer. Normally I would add the TO to the session or request obj as an attribute in the Action class, and then refer to it in the jsp. However I do recall reading somewhere (on this site) that another appraoch is to use ActionForms (instead of the TO). What is the best practice in this case? Note that I'm not collecting information from the user, but simply retrieving info from a database to display it.
Technically, you should populate an ActionForm (or something that extends ActionForm) with the contents of your TO. That way, the layers are still separate (MVC).
posted 12 years ago
technically, you should populate an ActionForm (or something that extends ActionForm) with the contents of your TO. That way, the layers are still separate (MVC).
Whilst I can see your point about seperation of concerns, I'm a little bit uncomfortable with duplication which arises from storing the same information in 2 distinct objects. Is this standard practice?
If you're only displaying information retrieved from the database, I see no problem at all with just putting the objects in the session or request and then referring to them in your JSP. Another approach is to "embed" them as properties in an ActionForm. The advantage this has is that it keeps things neat. If you have 5 DTOs, you just have 5 properties in your ActionForm rather than 5 different namespaces you're using up in your session or request.
If you're inputting or updating information, that's a different story. In that case, the best practice is to map the input information to an ActionForm and then copy the properties to a DTO to be used by the back-end process. The copyProperties method of BeanUtils comes in handy for this. An ActionForm should definitely not be used as a DTO because it adds a dependency on Struts to your model layer.
When my project started off a few years ago our common practice was to convert our DTO objects into "Display Objects". This has some advantages. For example, either when you populate the Display Object or retrieve data you can add code for formatting numbers or maybe concatenating First, Last and Middle names together. I don't like putting that display logic in the JSP or injecting it into the DTO. Also, if the DTO is a complex object that contains a lot of other objects you can hide that complexity from the presentation layer.
The downside is that an awful lot of work is involved with defining objects for all the layers and writing (and updating) code to translate back and forth. These days I am much more likely to pass the DTO all the way through to the presentation layer and I have been know to add methods to the DTO such as getFullName() or getFormattedTotal().
posted 12 years ago
Chaps, Thanks for the helpful replies and suggestions. The suggestion put forward by Merrill about embedding the DTO as ActionForm properties seems very promising.