aspose file tools*
The moose likes Struts and the fly likes global-forwards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "global-forwards" Watch "global-forwards" New topic
Author

global-forwards

Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
Hi,

I searched a bit on <global-forwards> and my understanding is that in the Action class, the ActionMapping object will try to look for the local forward defined in the <action-mappings> tag, if it is not found in that, it will then look for the <global-forwards> declaration.

I am having a problem in that the Action class is not able to find the <global-forwards> declarations.


In the list of forwards that is printed, I am not able to see the global forward value that was declared in the <global-forwards> section.

Can anybody help?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When in doubt, read the Javadocs. Here is the entry for findForward:
Find and return the ForwardConfig instance defining how forwarding to the specified logical name should be handled. This is performed by checking local and then global configurations for the specified forwarding configuration. If no forwarding configuration can be found, return null.

Here's The entry for findForwards:
Return the logical names of all locally defined forwards for this mapping. If there are no such forwards, a zero-length array is returned.

Note that findForwards only returns locally defined, not global forwards. If you want an array of the global forwards, use the findForwardConfigs method.
[ June 28, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
Thank you Merrill for your pointer...

I did see the JavaDocs. The entry for findForward says that findForward() returns local or global forward with the specified name -

public ActionForward findForward(java.lang.String forwardName)

Find and return the ForwardConfig instance defining how forwarding to the specified logical name should be handled. This is performed by checking local and then global configurations for the specified forwarding configuration. If no forwarding configuration can be found, return null.

Parameters:
forwardName - Logical name of the forwarding instance to be returned
Returns:
The local or global forward with the specified name.

That was the reason, I had the opinion I initially stated. If there was a error in my understanding, I would like to stand corrected.

If I want to use actionMapping.findForwardConfig("notLoggedIn") [instead of actionMapping.findForward("notLoggedIn")] how would I go about doing it? The former returns ForwardConfig object.
I would like the request to be sent to the path specified by the name "notLoggedIn" in <global-forwards> section of the struts-config.xml

Appreciate your help!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Bhaskar Reddy:

In the list of forwards that is printed, I am not able to see the global forward value that was declared in the <global-forwards> section.

The reason the global forward did not appear in the list is that you used findForwards to create the list, and as the Javadocs clearly state, this method does not retrieve global forwards.

However, the statement:

should have returned the correct global forward because findForward does search for global forwards. If the statement doesn't return the correct forward, there's something wrong somewhere. I guess I'm not understanding why you need to print a list of the forwards. Isn't it enough just to return the right one?
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
I actually wanted to forward it to the global-forward mentioned by the name="notLoggedIn". But since it was not working, I tried to see a list of forwards that are available at run-time and hence I used findForwards.

The real requirement is to forward all the requests to "home.do" if they dont have the session variable "userId".

And the way I am doing it is
1. to extend Action class in MyBaseAction and in the execute method, check for the session variable "userId", if not found, return actionMapping.findForward("notLoggedIn"); else (ie, if "userId" is found) return null.
2. All other Action classes will extend MyBaseAction and call super.execute() as their first statement (so that appropriate action is taken if the session variable 'userId' is not present).
3. The code sections are as under -


Do let me know if I am missing something...
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
It now works! The issue was more on the "return" flow that <<MyOtherActionClasses>> were not returning what <<MyBaseAction>> was returning (in case of "userId" not being available in session).

I wanted to have all this logic (check for "userId"; if present -> do this; else do that in one place in <<MyBaseAction>> rather than scattered in all <<MyOtherActionClasses>>. However, this doesnt seem to be possible.

Anyone has any thoughts on how to go about doing this? Appreciate your quick response!
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
Isn't it possible to extend the Action or DispatchAction and all other action classes extend this new extendedDispatchAction class?

Wonder what difference does it make really!


wanted to have all this logic (check for "userId"; if present -> do this; else do that in one place in <<MyBaseAction>> rather than scattered in all <<MyOtherActionClasses>>. However, this doesnt seem to be possible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: global-forwards