File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes why business logic not written in Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "why business logic not written in Action class" Watch "why business logic not written in Action class" New topic
Author

why business logic not written in Action class

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
why business logic not written in Action class.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Separation of concerns, and easier to test.
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
still am not clear. can you little bit better explanation.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Separation of concerns


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
Correct me if I'm wrong but, aren't the Action classes SUPPOSED to be where the business logic goes? You're supposed to separate business and presentation layers. I always thought you didn't want your presentation logic to creep into your actions, and your business logic to creep into your JSPs.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're wrong. While what you say about presentation is correct, there are other concerns. Putting business logic in actions makes that logic much more difficult to test, and impossible to re-use.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
I guess my question would then be, what exactly is "business logic" and where would that go? What is the "logic" that goes into the action classes?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Action classes, ideally, would contain only things related to the web layer. Data wrangling, error handling and redirection, how to report what happens on the business side to the user, etc.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
Ok. I guess that makes sense. I've read a lot of things that have simply stated that the Action class is like the Model in MVC and contains the business logic. I've been working through Struts 2 In Action and what they say is, "a Struts 2 action serves two roles. First, an action is an
encapsulation of the calls to business logic into a single unit of work. Second, the
action serves as a locus of data transfer." I guess this makes sense.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

"Encapsulation of business calls" is a good way of putting it.

Think of it this way: how can you unit test your business logic if it's all wrapped up in the web layer? (I guess you *can*, but it's needlessly complicated.) And re-using it becomes *substantially* more difficult.
Pj Casaro
Ranch Hand

Joined: Jul 13, 2010
Posts: 47
Makes sense. I was looking through the action classes that I've written and most of the "execute" methods call other things.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why business logic not written in Action class
 
Similar Threads
How much logic should go in Action class?
Struts Action Classes - Presentation or Business Logic Layer?
Reason for business logic in servlets only
Exception
Action class will be a falling under Presentation Tier or Business Tier