aspose file tools*
The moose likes Struts and the fly likes Redirect to another action in Interceptor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Redirect to another action in Interceptor" Watch "Redirect to another action in Interceptor" New topic
Author

Redirect to another action in Interceptor

Krishna Nagadev
Ranch Hand

Joined: Jan 04, 2005
Posts: 32
Actions are wrapped with interceptors and these configuration we kept it in struts.xml

Instead of controlling it at configuration level, I want to bypass the execution of Action/next interceptors in the stack from interceptor itself.. dynamically .. is it really possible ?
Let me explain my problem in detail here..

I need to check a cookie at interceptor and need to execute different actions based on cookie value.. I tried in below way, I am able to redirect the response to desired action and page but with exceptions. Here is the code ..



Error Message
java.lang.IllegalStateException: Cannot create a session after the response has been committed
org.apache.catalina.connector.Request.doGetSession(Request.java:2301)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://struts.apache.org/2.x/docs/writing-interceptors.html

You probably need a PreResultListener.
Krishna Nagadev
Ranch Hand

Joined: Jan 04, 2005
Posts: 32
Thanks David,

But I dont want to execute the Action, instead I want to redirect/forward to some other action from interceptor. if I choose PreResultListener then it will be called before the result is being executed. ie after execution of your Action. I dont want to happen in my case. Please let me know if any thoughts
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh; include the full source of your interceptor then. Or just look at the source for something like the workflow interceptor and see what you're doing wrong.
Krishna Nagadev
Ranch Hand

Joined: Jan 04, 2005
Posts: 32
Here is the intercept method of my interceptor.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you try it by using a normal S2 result rather than doing a sendRedirect? That's a better solution anyway.

I'm pretty sure you don't want a forward anyway, since that'd show the original URL in the browser bar.
Krishna Nagadev
Ranch Hand

Joined: Jan 04, 2005
Posts: 32
I may require to forward the request because I need to resend the same parameters that I received as part of request. That is a different problem again ;)
Can you suggest me how to redirect to S2 Action instead of response.sendRedirect() from Interceptor ?

Thanks for your help again
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Return the name of a global result.
Krishna Nagadev
Ranch Hand

Joined: Jan 04, 2005
Posts: 32
OK thanks. I will try that
Sandeep Murthy
Ranch Hand

Joined: Oct 05, 2005
Posts: 34
You should stop getting the "Result already committed" error message if you remove the "return null" statement after the redirect.

Explanation:

When you return null, the default result gets executed.
Since you have already done a response.sendRedirect(), you will get the error message when you try to forward again as per the default result.


SCJP 1.4
 
Consider Paul's rocket mass heater.
 
subject: Redirect to another action in Interceptor