You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Stanislav <sk...@inet.hr> on 2006/12/15 14:54:20 UTC

redirect or forward from method

   	

Hi!

I'm using Strus and this is my problem. I have this method A which have input and
outpur parameters.
There is try/catch block in method A that capture Exceptions. I want to redirect or
forward to jsp
page B from method A when some error is happend and NOT to return to class C from
which method A is
invoked. Is this possible to achieve?


  action class C 
         invoke method A ----------------> method A
                                              have try/catch block
         return values to class C <-------------if ok
                                                if error ----------->
redirect/forward to jsp or
some toher class
                                                                      and not return
anything to
class C

  action class C is struts action class
  method A is method in normal Java class that extends Action class


Tnx :-)

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


Re: redirect or forward from method

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stan,

Stanislav wrote:
> I'm using Strus and this is my problem. I have this method A which
> have input and outpur parameters. There is try/catch block in method
> A that capture Exceptions. I want to redirect or forward to jsp page
> B from method A when some error is happend and NOT to return to class
> C from which method A is invoked. Is this possible to achieve?

The best way to direct Struts to another location is to return the
appropriate ActionForward from your Action's execute() method.

Since you are using another method ("method A") to do your work, this is
not possible directly (that is, you cannot "return" a value from "method
A" and have it skip further processing in your action class). This just
isn't now Java (or any other programming language, really) works.

What you /can/ do is let the exception from "method A" propagate all the
way back up into your action code, and catch it /there/ instead. Like this:

public ActionForward execute(...) throws Exception
{
   .
   .
   .
   try {
     .
     .
     methodA();
     .
     .
     .
   } catch (SomeException e)
   {
      return mapping.findForward("method-a-error");
   }
}

If it is inappropriate to allow SomeException to propagate from "method
A" back into the action code, then consider wrapping that exception in
something else, like MethodAException:

public void methodA(...)
{
   .
   .
   .
   catch(SomeException e) {
     throw new MethodAException("Method A failed", e);
   }
}

and in your action code, catch MethodAException instead of SomeException.

Don't forget that your Action is really the place where decisions about
control flow need to be made... helper methods can accomplish other
tasks of help the Action make decisions, but ultimately, it's the
Action's responsibility to control the flow.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFgq449CaO5/Lv0PARAnDtAKC1rPl0ZmvCW3EnqrAW/qxq+wrQ0gCfWJgi
ra0kGCKdGxz7BZASiiTZmOY=
=Fp9O
-----END PGP SIGNATURE-----

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