You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Holger Wiechert <ho...@business-logics.de> on 2001/07/22 15:37:10 UTC

"Throwing" ActionErrors within Action class?

Hi everybody,

so far, I've only read about the possibility to use the
<struts-html:error>-Tag by
"throwing" ActionErrors within the validate of an ActionForm. But it would
be quite
helpful to do something like this from within an Action class. Let's assume,
I just sent a message to a business object (for instance to insert an entry
into
a database), and received the response message, that some error occured.
So it's something, the ActionForm cannot validate. Is there a way of
creating
some ActionErrors (in the perform of Action) and then forward to a JSP
that uses the <struts-html:error>-Tag?

Thanks in advance,
Holger


Re: "Throwing" ActionErrors within Action class?

Posted by Ted Husted <hu...@apache.org>.
Yes, you typically create ActionErrors in the perform() method of an
Action, and then return an ActionForward to a JSP to display an error
message. 

ActionErrors errors = new ActionErrors();

< ... />

if (something bad happens) {
    errors.add(ActionErrors.GLOBAL_ERROR,
        new ActionError("action.something.bad.happened"));
}

if (errors.empty) { .. continue processing .. }

< ... />

// Report any errors
if (!errors.empty()) {
    saveErrors(request, errors);
    return (new ActionForward(mapping.getInput())); // or some other
error forwarding
}


// Exit normally
return (mapping.findForward("continue"));


-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/


Holger Wiechert wrote:
> 
> Hi everybody,
> 
> so far, I've only read about the possibility to use the
> <struts-html:error>-Tag by
> "throwing" ActionErrors within the validate of an ActionForm. But it would
> be quite
> helpful to do something like this from within an Action class. Let's assume,
> I just sent a message to a business object (for instance to insert an entry
> into
> a database), and received the response message, that some error occured.
> So it's something, the ActionForm cannot validate. Is there a way of
> creating
> some ActionErrors (in the perform of Action) and then forward to a JSP
> that uses the <struts-html:error>-Tag?
> 
> Thanks in advance,
> Holger