This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm starting work on a new project that has well into construction. As I'm looking through the code base, I'm trying to take in the existing design so that I can more thoroughly understand the application. I've run across a practice that I'm not so sure I agree with and I wanted to bounce it off some other folks that may have had more experience with Struts and JSP's. (By the way, if this is more of a Struts question than a JSP question, feel free to move this post.)
When using Struts, all of the form parameters are encapsulated in a Form Bean (an object that extends ActionForm). In general, my concept of a Form Bean was to encapsulate the form data and do validation on that data. In addition, the Form Bean serves a wonderful purpose when the user enters an error. In that case, it's common that execution will be sent back to the same screen the user was just on but it will display an error message so that the user can correct what was wrong and resubmit the form. You can then use the Form Bean to repopulate the form (with the data the user had previously submitted), like this:
However, in this new application, I'm finding that the Form Bean is also being used to encapsulate model information.
For example, a call is made to a database to retrieve a list of locations. Then, in order to get that information to the view, the list is put into the Form Bean. Then, from the JSP, you can do something like this (note that the locations are put into the Form Bean under the member name "siteNames"):
This works, but it feels wrong to me. Am I just naive to the way Struts is supposed to work (this is my first full-fledged Struts app I've worked with)? I would have thought the model information would have been put into some other Java object (probably a Java Bean) and that object would have been explicity put into the request for JSP access.
I guess I'm just not sure if I'm comfortable with Form Beans being used in this way. Any comments?
Edited by Corey McGlone: Turned off smilies to keep them from interfering with my jsp tags. [ October 28, 2004: Message edited by: Corey McGlone ]
I agree with you. This seems to be placing responsibilities on an ActionForm that it really shouldn't have. I would have just set the list of site names to the request, or as an attribute of some JavaBean which I made available in the request. I don't see how it belongs in any way in the ActionForm.
Corey, I agree with Jason. But from what I've read about JSF, it looks like they encourage everything to be thrown in one bean/form type class. And I have coworkers who feel the same way as yours do. So I think both ways are valid.
I should start by saying: I am not experienced in Struts ... and than continue. Starting with the name ActionForm I would say that everything that exists inside a class extending ActionForm would be according to it (meaning it would manipulate/validate the form data and perform the corresponding action => submitting). But for the submitting/retrieving data I would rather use something like Transaction Script pattern