Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

ActionForm vs Transfer Object

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

kind regards,
Mo
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
mo sayed
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

kind regards,
Mo
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().

- Brent
 
mo sayed
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaps,
Thanks for the helpful replies and suggestions.
The suggestion put forward by Merrill about embedding
the DTO as ActionForm properties seems very promising.

kind regards,
Mo
 
Die Fledermaus does not fear such a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!