You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kevin HaleBoyes <kz...@yahoo.com> on 2002/09/19 20:40:00 UTC

MVC and Struts

I have a struts application modelled after the struts-example.
It has
   XForm.java as the form bean,
   x.jsp to display the form, and
   EditXAction.java and SaveXAction.java to process the actions.

Edit and Save actions need to use an XMaintainance class as it
provides the knowledge of the business logic.

In SaveXAction I've checked to make sure the request wasn't cancelled
and checked the transaction token (normal struts stuff).  I need to
do additional validations when an XForm is being created so I've
added a validate() method to XMaintainance.

So the SaveXAction code does something like the following:

    ActionErrors errors = new ActionErrors();
    XForm xf = (XForm)form;
    XMaintainance xm = new XMaintainance();
    if ( ! xm.validate(xf) ) {
        errors.add("error", new ActionError("Xerrors"));
    }

and later check to see if there have been any errors, and if so,
saveErrors() and saveToken() are called and we forward back to the
mapping.getInput().

That works great!

My question is:

XForm is considered to be a View component.
SaveXAction is considered to be a Controller component.
XMaintainance is a Model component.

By passing the XForm to XMaintainance haven't I mingled the two
components when there was no need?  Mingled is the wrong word.

Instead,
SaveXAction could mediate the interaction between the view and
the model by copying values from the view into the controller:

    xm.setXFormField1( xf.getField1() );
    ...
    if ( ! xm.validate() ) {
        errors.add("error", new ActionError("Xerrors"));
    }


Performance wise it would be wasteful to copy the values from
XForm to XMaintainance.  But isn't one of the goal of MVC to
separate the Model from the View by way of the Controller?

Where do we make the split between the three components?  

For instance,
how do report errors from XMaintainance?  I could change the
code above to be:

    ActionErrors errors = new ActionErrors();
    XForm xf = (XForm)form;
    XMaintainance xm = new XMaintainance();
    if ( ! xm.validate(xf, errors) ) {
        errors.add("error", new ActionError("Xerrors"));
    }

But now I've mingled a core Controller element (ActionError) to
the Business logic layer.  If I don't do this though, I'll have to
use something similar to ActionErrors that XMaintainance knows about.
SaveXAction would have to copy these errors to the Struts
ActionErrors.  It seems like a lot of copying is being done in order
to separate the components.

Does any of this make any sense?  I guess I'm looking for advice,
on how to split the three MVC components and pass information
between the layers.

Thanks,
K.




__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: MVC and Struts

Posted by Jin <ji...@hotmail.com>.
IMHO you're definitely on the right track by copying/passing values
between the tiers in your app i.e not passing XForm directly to
Xmaintainence. etc

You'd probably want to create you own DataTransferObject (DTO) in the
action, populate it somehow ( there's an easy way to do this*) and then
pass it to Xmaintainence.  This way your API doesn't need to now about
Struts (Xform). 

There's probably a cost involved in this approach however the benefit of
separation outweighs this.  

The Commons packages provide useful utilities for copying between
objects using reflection.  This can reduce your get/set/get/set to one
line (PropertyUtils.copyProperties()) and make your code slightly more
maintainable.  Again, any performance hit is worth it (*imo*).

In terms of error reporting, Xmaintainence could just return a array of
strings which are keys to error messages in your resources.  The action
would then detect these and translate in ActionErrors.  Your Xform would
do basic validation ie format/type checking. The business validation of
the values would be done in Xmaintainence.

>From what I've gathered from being on this list for over a year the
general consenus is that the: 

Actions should translate from the web tier an pass objects into the
business tier, then catch/receive errors/results from the business tier
and translate them back into something that Struts/the view layer can
understand.

HTH

Jin
-----Original Message-----
From: Kevin HaleBoyes [mailto:kzboyes@yahoo.com] 
Sent: 19 September 2002 19:40
To: struts-user@jakarta.apache.org
Subject: MVC and Struts

I have a struts application modelled after the struts-example.
It has
   XForm.java as the form bean,
   x.jsp to display the form, and
   EditXAction.java and SaveXAction.java to process the actions.

Edit and Save actions need to use an XMaintainance class as it
provides the knowledge of the business logic.

In SaveXAction I've checked to make sure the request wasn't cancelled
and checked the transaction token (normal struts stuff).  I need to
do additional validations when an XForm is being created so I've
added a validate() method to XMaintainance.

So the SaveXAction code does something like the following:

    ActionErrors errors = new ActionErrors();
    XForm xf = (XForm)form;
    XMaintainance xm = new XMaintainance();
    if ( ! xm.validate(xf) ) {
        errors.add("error", new ActionError("Xerrors"));
    }

and later check to see if there have been any errors, and if so,
saveErrors() and saveToken() are called and we forward back to the
mapping.getInput().

That works great!

My question is:

XForm is considered to be a View component.
SaveXAction is considered to be a Controller component.
XMaintainance is a Model component.

By passing the XForm to XMaintainance haven't I mingled the two
components when there was no need?  Mingled is the wrong word.

Instead,
SaveXAction could mediate the interaction between the view and
the model by copying values from the view into the controller:

    xm.setXFormField1( xf.getField1() );
    ...
    if ( ! xm.validate() ) {
        errors.add("error", new ActionError("Xerrors"));
    }


Performance wise it would be wasteful to copy the values from
XForm to XMaintainance.  But isn't one of the goal of MVC to
separate the Model from the View by way of the Controller?

Where do we make the split between the three components?  

For instance,
how do report errors from XMaintainance?  I could change the
code above to be:

    ActionErrors errors = new ActionErrors();
    XForm xf = (XForm)form;
    XMaintainance xm = new XMaintainance();
    if ( ! xm.validate(xf, errors) ) {
        errors.add("error", new ActionError("Xerrors"));
    }

But now I've mingled a core Controller element (ActionError) to
the Business logic layer.  If I don't do this though, I'll have to
use something similar to ActionErrors that XMaintainance knows about.
SaveXAction would have to copy these errors to the Struts
ActionErrors.  It seems like a lot of copying is being done in order
to separate the components.

Does any of this make any sense?  I guess I'm looking for advice,
on how to split the three MVC components and pass information
between the layers.

Thanks,
K.




__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>