Struts is a MVC-based framework. The application's business logic, including data CRUD operations are part of the Model application (aka Business Object Model).
A Struts Action class is part of the Controller. This is how you create a Controller for your specific application.
What does this mean? When programming with Struts, you should avoid writing business logic code in the Action class. This includes any operations that "save" data in a database.
In your example, the execute() method and a DAO object are tightly coupled. This mean that you have coded business logic in your Controller code, not a business object. Your are missing a business object.
The purpose of a Data Access Object is to encapsulate the "data access logic" and shield your "business logic" from the details, e.g. syntax, of the data access logic.
If you pollute the code of the Controller with "data access logic", e.g. Hibernate statements, you introduce a significant dependency upon the implementations. This is not acceptable in enterprise software. It is ok for Jim's marketing website with 100 visitors per month.
If you pollute the business logic with hard-coded syntax of your ORM implementaiton, you introduce a significant dependency. The overall objective is to foster "low coupling" between components, not "hard-coded" dependencies.
Again for emphasis, there is a difference between large-scale systems and Sayeed's Java Codes Interview Questions website
When programming with Struts, you should avoid writing business logic code in the Action class. This includes any operations that "save" data in a database.
True, I read (and I think it was you) that a good indication of 'the right way to write the actions' are with junit. If it's hard to test the action - then something is wrong with the way you design it.
Could you please comment my actions above, is this the right way to write them? Do I maintain MVC to the level of 'large-system'
Say an end-user made a payment and I wish to send him an invoice (or a reference for the payment). So s/he completes a form and clicks submit.
My question is this: given the action below - is this something that makes sense from MVC perspective? Is there a tight coupling between the action and the business logic? Am I missing the point of sturts' actions? how would you do it differently.
The essence of the Model-View-Controller design pattern is that the model application has no dependencies upon the View implementation.
Struts is a skeleton framework for a HTML-based View and a Java-based Controller. There is nothing concerning the Model application in Struts. This is a good thing.
So, for any business operation, you should be able to execute it from a simple command line as well as from an HTML control on an HTML web page. If you cannot run your Model application from a command-line then you have not properly implemented the MVC design pattern.
Remember, the Action class is the Controller. It should know absolutley nothing about what business logic occurs, i.e. sending emails, creating PDF files, whatever. All the Controller does is forward appropriate data from View to the appropriate business method via delegate, and send appropriate data returned from business method to appropriate View.
VIEW -----> CONTROLLER ------> MODEL -------> CONTROLLER --------> VIEW
The Struts Action classes and struts-config.xml file is how you create a specific Controller for your application.
The Struts JSP Tag Libraries are how you can create a View for your application.
In terms of the Model, you could used plain old Java objects or Session EJB
Hope this helps!
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, the permaculture playing cards