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 struts2 MVC pattern - business logic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts2 MVC pattern - business logic question" Watch "struts2 MVC pattern - business logic question" New topic
Author

struts2 MVC pattern - business logic question

Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
Hi all,

I've been assign to a struts2 project and can't really understand the MVC pattern.
My question is mostly about the Action; should I think of any actions as the controller? Is this the place where I place all my business logic and the action's purpose is just to 'dispatch' the view? If so how do I do that?

In other words, what comes in the action (in first sight I thought -wow, all business logic should come here and life is easy [sa in the code below], but then I was reprimanded for doing so because I was told that this (action) is just the dispatcher).

So consider I need to do this:
1. user clicks a link and an action is started
2. I have a userID (based on the link)
3. I need to check a status of a registration
4a. if registration is APPROVED - create a pdf(A) and email to user+manager
4b. if registration is DENIED - create a pdf(B) and email to user+manager
4c. if registration is ON_HOLD - create pdf(C) and email user

can anyone provide a psude code for such action

thanks for any pointers, again - the question is not about the code, is more about the business logic --how should it be constructed and if the way presented above is ok (MVC in mind)

THANK YOU!
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Put your business logic into helper classes and have them invoked by the Action. Yes, the Action should be part of the Controller in MVC and should not contain biz logic.
sudeep mahapatra
Greenhorn

Joined: Apr 06, 2007
Posts: 4
Struts Action classes are part of controller so they should not contain any business logic. A business delegate is used to delegate the business to business layer. In this manner we can separate the business logic from View and Controller.
More on MVC : http://java.sun.com/blueprints/patterns/MVC.html


Thanks and Regards,
Sudeep
Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
I'd appritiate if anyone can give an example

Will this constitute a good MVC? is this how MVC expects me to separate the ACTION from BUSINESS LOGIC?


ACTION:



and the business side:


How would you do it better? (am I on the right track)
sudeep mahapatra
Greenhorn

Joined: Apr 06, 2007
Posts: 4
A BusinessDelegate is to delegate/pass the information to business layer (For e.g. to a Bean) not to contain business logic itself.
Usually BusinessDelegate passes the info to a SF(SessionFacade) where the actual business logic resides.
For Example:

SampleAction [Action class]
/** Business Delegate instance */
private SampleBD sampleBD = new SampleBD();
execute() [this method should contain code to call testPDFCreate()]
testPDFCreate() [Here you call the createPDF() present in SampleBD]
{
sampleBD.createPDF();
}

SampleBD [It should contain a method that calls the business method present in Bean]
public createPDF()
{
// create Bean (EJBObject) of SampleSFRemote
// call createPDF()
// catch RemoteException
}
SampleSFRemoteHome [This is the Home Interface having the create method to create an EJB object]
SampleSFRemote create() throws CreateException, RemoteException;
SampleSFRemote [This is a session bean containing the abstarct method createPDF()]
createPDF() throws RemoteException ;
SampleSFBean
public createPDF() {}[Give the method body here. The actual business logic.]

One Important thing to check how you are passing information. Normaly ValueObject is used.
These objects are Serializable.
Can you please check in your project code. I think there must be some DesignPattern being followed.
Just see how other business flow is working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts2 MVC pattern - business logic question