You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ma...@yahoo.com on 2007/04/06 01:34:53 UTC

MVC 2 design, how to

Hello every one:
I am trying to write a little application to familiarize myself with 
struts 2. I am trying to design the application using mvc2. the 
application is nothing but a small utility for invoices. It store, 
retrieves and update invoices. Each invoice belongs to an Account in the 
DB. from the examples I have seen so far, the Action class is populated 
by values from the jsp page.
So here's my design:

newAccount.jsp
                            <------> AccountManager.java (ActionSupport) 
<----> Account.java
veiwAccount.jsp            

Account.java is the class where all the bussiness logic goes. 
AccountManager is part of the controller and based on the results it 
gets from the Account.java it forwards to the appropriate view. The user 
can view an existing account or create one through the jsp pages. Now 
because it doesn't make scense to populate the AccountManager with 
getters/setters for the same fields in Account, I will pass Account as 
an attribute of the request to the view. I mean, if Account.java has 
fields , accountID, accountName , ... then I need getters and setters to 
access them, but the same getters and setters has to exist in 
AccountManager.java so that AccountManager can communicate with the 
views. However, I have read some where that because the bussiness logic 
may contain other resources ( ie, db connection, ..) we  don't pass it 
to the view, but we pass a minimal verion of that class as a bean. So, I 
'll create another class called ActionBean.java and populate it from 
within Account.java .

PLease note that AccountManager can be accessed from many places and 
depending on the output it forwards to the correct view.

I need an advice or comments, as I am new to struts 2 and MVC, and I 
feel that I am missing something.
Can any one please correct me or give me comments?

 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: MVC 2 design, how to

Posted by Ted Husted <hu...@apache.org>.
On 4/6/07, mansour77@yahoo.com <ma...@yahoo.com> wrote:
> How many approach there is ? Where can I read about them ?

Just the two. Either put the properties on the Action class, or
implement ModelDriven.

A third might be to create your own interceptor that did the same
thing as ModelDriven, but with more objects.

> you mean getModel should be placed in the AccountManager (ActionSupport)
> and getModel should return an object of AccountBean. All the business
> logic is encapsulated in Account.java . Am I right ?

Yes, if Account is a singleton. Otherwise, you could just put the
properties on the Account object.

HTH, Ted
<http://www.husted.com/ted/blog/>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: MVC 2 design, how to

Posted by ma...@yahoo.com.

Ted Husted wrote:
> It sounds like that you might want to use the ModelDriven approach,
How many approach there is ? Where can I read about them ?

> where the AccountManager is the model object.
>
If I move the business logic to the action class then this becomes MVC1. 
>From my understanding the model should be separated.

> I was surprised to find that there doesn't seem to be a page or FAQ
> with regard to using ModelDriven.
>
> Essentially, all you need to do is place a model property on a base
> Action class, or at least the accessor.
>
The AccountManager extends  ActionSupport.

> public Object getModel() ...
>
> In the implementation of getModel, acquire an instance of Account
> Manager object and return it.
>
> On the page, you can address any JavaBean properties on the
> AccountManager as if they were coded directly on the Action class.
>
you mean getModel should be placed in the AccountManager (ActionSupport) 
and getModel should return an object of AccountBean. All the business 
logic is encapsulated in Account.java . Am I right ?



> (The framework pushes the Model object onto the ValueStack.)
>
> Many developers use Spring to acquire something like the Account
> Manager. With the addition of a setModel method, the Action Manager
> can be injected automatically.
>
> HTH, Ted
> <http://www.husted.com/ted/blog/>
>
> On 4/5/07, mansour77@yahoo.com <ma...@yahoo.com> wrote:
>> Hello every one:
>> I am trying to write a little application to familiarize myself with
>> struts 2. I am trying to design the application using mvc2. the
>> application is nothing but a small utility for invoices. It store,
>> retrieves and update invoices. Each invoice belongs to an Account in the
>> DB. from the examples I have seen so far, the Action class is populated
>> by values from the jsp page.
>> So here's my design:
>>
>> newAccount.jsp
>>                             <------> AccountManager.java (ActionSupport)
>> <----> Account.java
>> veiwAccount.jsp
>>
>> Account.java is the class where all the bussiness logic goes.
>> AccountManager is part of the controller and based on the results it
>> gets from the Account.java it forwards to the appropriate view. The user
>> can view an existing account or create one through the jsp pages. Now
>> because it doesn't make scense to populate the AccountManager with
>> getters/setters for the same fields in Account, I will pass Account as
>> an attribute of the request to the view. I mean, if Account.java has
>> fields , accountID, accountName , ... then I need getters and setters to
>> access them, but the same getters and setters has to exist in
>> AccountManager.java so that AccountManager can communicate with the
>> views. However, I have read some where that because the bussiness logic
>> may contain other resources ( ie, db connection, ..) we  don't pass it
>> to the view, but we pass a minimal verion of that class as a bean. So, I
>> 'll create another class called ActionBean.java and populate it from
>> within Account.java .
>>
>> PLease note that AccountManager can be accessed from many places and
>> depending on the output it forwards to the correct view.
>>
>> I need an advice or comments, as I am new to struts 2 and MVC, and I
>> feel that I am missing something.
>> Can any one please correct me or give me comments?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: MVC 2 design, how to

Posted by Ted Husted <hu...@apache.org>.
It sounds like that you might want to use the ModelDriven approach,
where the AccountManager is the model object.

I was surprised to find that there doesn't seem to be a page or FAQ
with regard to using ModelDriven.

Essentially, all you need to do is place a model property on a base
Action class, or at least the accessor.

public Object getModel() ...

In the implementation of getModel, acquire an instance of Account
Manager object and return it.

On the page, you can address any JavaBean properties on the
AccountManager as if they were coded directly on the Action class.

(The framework pushes the Model object onto the ValueStack.)

Many developers use Spring to acquire something like the Account
Manager. With the addition of a setModel method, the Action Manager
can be injected automatically.

HTH, Ted
<http://www.husted.com/ted/blog/>

On 4/5/07, mansour77@yahoo.com <ma...@yahoo.com> wrote:
> Hello every one:
> I am trying to write a little application to familiarize myself with
> struts 2. I am trying to design the application using mvc2. the
> application is nothing but a small utility for invoices. It store,
> retrieves and update invoices. Each invoice belongs to an Account in the
> DB. from the examples I have seen so far, the Action class is populated
> by values from the jsp page.
> So here's my design:
>
> newAccount.jsp
>                             <------> AccountManager.java (ActionSupport)
> <----> Account.java
> veiwAccount.jsp
>
> Account.java is the class where all the bussiness logic goes.
> AccountManager is part of the controller and based on the results it
> gets from the Account.java it forwards to the appropriate view. The user
> can view an existing account or create one through the jsp pages. Now
> because it doesn't make scense to populate the AccountManager with
> getters/setters for the same fields in Account, I will pass Account as
> an attribute of the request to the view. I mean, if Account.java has
> fields , accountID, accountName , ... then I need getters and setters to
> access them, but the same getters and setters has to exist in
> AccountManager.java so that AccountManager can communicate with the
> views. However, I have read some where that because the bussiness logic
> may contain other resources ( ie, db connection, ..) we  don't pass it
> to the view, but we pass a minimal verion of that class as a bean. So, I
> 'll create another class called ActionBean.java and populate it from
> within Account.java .
>
> PLease note that AccountManager can be accessed from many places and
> depending on the output it forwards to the correct view.
>
> I need an advice or comments, as I am new to struts 2 and MVC, and I
> feel that I am missing something.
> Can any one please correct me or give me comments?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org