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

Interceptor messes up action class in Struts2

Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
I have been following this tutorial Struts 2 + Spring 2 + JPA + AJAX. So I installed the app and it worked fine. Then, I added an authentication interceptor using an example from Struts2 in Action. At this poing, the server threw out 404 error on /list.action once the interceptor is added. It seems that the interceptor has prevented the executed() method in PersonAction from being executed.

I have been scraching my head and couldn't find an answer. I would greately appreciate it if anyone could throw some light on it.

Below are some relevant code and struts2.xml
=======================================

list.jsp


PersonAction


The interceptor

struts2.xml


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Do you get the error before or after (or both) you've logged in? You're also going to the "login" action, but you'll be going to main.action when you do, unless you're doing something not shown to get yourself to the "input" result with the form.

I'd also use a "redirectAction" result and simply go to "main".
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
Hi David,

Thanks. yes I do have error before logging in.
"No result defined for action com.test.action.PersonAction and result login"


<action name="list" method="execute" class="personAction">
<result>/pages/list.jsp</result>
<result name="input">pages/list.jsp</result>


After logging in, I got 404 error

The login is not intercepted by the interceptor becuase it is in a different package.

When you say "I'd also use a "redirectAction" result and simply go to "main"." , are you talking about the page you should see right after login? That is what it does now.

Any ideas would be greately appreciated.

Thanks,
Rich

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why are they in different packages? It's easy enough to set the action-specific interceptor-ref for the login action to just be "defaultStack" (as opposed to "secureStack").

Using "redirectAction" allows you to just specify the action name as the result's location rather than having to append the action suffix, that's all.
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
I remember tried that -- put everything in one package that means login will subject to the power of interceptor. It keeps kicking you back to the login page even after you logged in. Maybe I didn't do it properly... just starts learning...

I did try this --redirect. But it does not have any effect. The error page look a different:

Error 404: SRVE0190E: File not found: /list.action

<action name="list" method="execute" class="personAction">
<result name="success" type="redirect">pages/list.jsp</result>
<result name="input">pages/list.jsp</result>
</action>

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's why I said to override the login action's interceptor ref to be "defaultStack" instead of "secureStack".

Actions can define their own interceptor ref.
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
Thank you David. Following you suggestion, I put everything in one package and set interceptor ref to the login. It works like a charm. Thank you!

I also tried redirectAction.


I got a blank page on login. Is this the correct way of using redirectAction?

Thank you so much!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, "redirectAction" is for redirecting to an action. You originally had a redirect result to "main.action". A redirectAction result's location would be "main".
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
I thougt this would work but it doesn't. Anything wrong with this? Still gets a blank screen. Typing http://xxx/loginForm.action directly, gets Error 404: SRVE0190E: File not found: /loginForm.action. Thanks a lot.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your original code shows the interceptor returning "login" when there's no user in session. I don't, however, see a global result named "login", nor individual "login" results for each action (obviously the first would be preferred). Remember, the return value of an action invocation is the name of a result--not the name of an action.

Two asides: you should be able to use "redirectAction" as below, w/o using the <param...> element.

Secondly, since you're getting a fresh user object from the DB to set on UserAware actions you might as well set the fresh user to the session as well, assuming you're getting the DB user for a reason.

Dave
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
You are right. I added login globle result. I have put below an updated struts.xml. Thanks.

1. I still see a blank page when I replace <result type="redirect">main.action</result>
with <result type="redirectAction">main</result>. Strange.

2. The interceptor has added the user to session in ((UserAware)action).setUser(freshUser);

3. /list.action still not working. If I add <interceptor-ref name="defaultStack" /> to it, it works instantly.
It seems that actionInvocation.invoke() only returns a string "success". How would that alter the behavior of list.action?

I know you wrote a Struts2 book. Does it talk about interceptor? Maybe I should go grab your book.

Thanks so much.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Is the list action the only one that doesn't work?
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
I tried remove.action, it has the same problem: "Error 404: SRVE0190E: File not found: /remove.action"
The actions that work are main.action and apps.action.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That leads me to think it's a Struts/Spring issue, since those actions aren't defined as Spring beans.

That doesn't help explain why changing the interceptor stack triggers the problem, though, and that the interceptor stack appears to be the problem points in a different direction... So the actions defined as Spring beans work w/ "defaultStack"?
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
Yes, these actions work only with "defaultStack". Once in secureStack with the interceptor, they all stop working. Still can't figure out why.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interceptor messes up action class in Struts2
 
Similar Threads
struts2 properties file reload
Struts2 and Spring integration
In struts2 delete is not Working
problem getting list in jsp page in struts2
Retrieving request parameters (from the url) in a jsp page