You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Donald Ball <db...@rhoworld.com> on 2002/09/06 17:11:28 UTC

how to return errors from Model component

Hey guys, I have an MVC-type question. My View captures data from the user
and sends it to the Controller, which validates and massages it and uses it
to configure the Model and tell the Model to do something. Based on the
something, the Controller sends another View to the client. So far so good.

What happens when something, or rather, somethings go wrong when the Model
is doing its work? How should the Model communicate this to the Controller
in a way that doesn't violate the seperation of concerns? Currently, my
Model creates a struts ActionErrors object and puts the error messages into
it and returns it to the Controller. This strikes me as a bad design choice
because the Model object shouldn't have any notion of the environment in
which it lives. However, I'm unsure what the best practice is. Choices
include:

* Tossing an exception - but I would like to be able to send multiple
errors at the same time

* Return a List or array of String error messages - but then my messages
are hard-coded into the application, making i18n painful. I could make the
Strings returned be keys into the ApplicationResources properties file, but
then how do I attach parameters for messages that need them?

* Return, oh, a SortedMap of error messages, where the keys are the error
message keys, sorted in order of occurance, and the values are Lists of
parameters for the error messages - but this seems awfully convoluted.

Any suggestions?

- donald


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


Re: how to return errors from Model component

Posted by Ted Husted <hu...@apache.org>.
I generally use the second approach. The whole ResourceBundle concept is 
part of the  Java platform and not Struts specific. So I have I my 
business classes return message tokens and any pertinent replacement 
parameters. The parameters do not usually need to be translated so this 
is usually not an issue.

Actually, I have the business classes return a transfer object that 
wraps the messages and other detail up into a tidy bundle. For example, 
it can also provide a "dispatch" property (ActionForward name). So if 
the messages are confirmations, it passes through to success. But if the 
messages are errors, the dispatch can return "failure" instead. The 
Struts Action then looks up its failure forward.

My thinking is that the ActionForward *object* belongs to the web tier, 
but the tokens, like "success" and "failure", are a protocol that 
belongs to the application. Ditto for the tokens in the message resources.

http://cvs.apache.org/viewcvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/util/

look for ProcessResult.

This is then used on the Struts side by the ProcessAction or the 
ProcessDispatchAction.

http://cvs.apache.org/viewcvs.cgi/jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/

-Ted.



Donald Ball wrote:

> Hey guys, I have an MVC-type question. My View captures data from the user
> and sends it to the Controller, which validates and massages it and uses it
> to configure the Model and tell the Model to do something. Based on the
> something, the Controller sends another View to the client. So far so good.
> 
> What happens when something, or rather, somethings go wrong when the Model
> is doing its work? How should the Model communicate this to the Controller
> in a way that doesn't violate the seperation of concerns? Currently, my
> Model creates a struts ActionErrors object and puts the error messages into
> it and returns it to the Controller. This strikes me as a bad design choice
> because the Model object shouldn't have any notion of the environment in
> which it lives. However, I'm unsure what the best practice is. Choices
> include:
> 
> * Tossing an exception - but I would like to be able to send multiple
> errors at the same time
> 
> * Return a List or array of String error messages - but then my messages
> are hard-coded into the application, making i18n painful. I could make the
> Strings returned be keys into the ApplicationResources properties file, but
> then how do I attach parameters for messages that need them?
> 
> * Return, oh, a SortedMap of error messages, where the keys are the error
> message keys, sorted in order of occurance, and the values are Lists of
> parameters for the error messages - but this seems awfully convoluted.
> 
> Any suggestions?
> 
> - donald
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 


-- 
Ted Husted, Husted dot Com, Fairport NY US
co-author, Java Web Development with Struts
Order it today:
<http://husted.com/struts/book.html>


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