struts <global-forwards> issue
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.
<<struts-config.xml>>
<global-forwards>
<forwardname="notLoggedIn" path="/home"/>
</global-forwards>
<<MyAction.java>>
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
HttpSession session = httpServletRequest.getSession();
Object accId = session.getAttribute("userId");
if (userId== null) {
log.error("userId in session " + userId);
String[] forwards = actionMapping.findForwards();
for (int i=0;i<forwards.length; i++) {
log.error("forwards["+i+"] : "+forwards);
}
return actionMapping.findForward("notLoggedIn");
} else {
log.error("userId in session " + userId);
return null; // do nothing
}
}
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 throw light on what could be the problem?
[1556 byte] By [
MrReddya] at [2007-11-27 9:11:35]

# 5
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!
# 6
Well there are a few ways you could implement it
1 - as a ServletFilter.
Pro: Will cover every single request you make
Con: Puts the entire logic of the login check outside of struts, so you couldn't use the global forward.
2 - Subclass the struts RequestProcessor class, and override method processActionPerform to do the login check for every struts process handled. In your struts-config you would have to specify your new RequestProcessor class.
Pro: Will handle every struts request
Con: Needs a bit more struts knowledge to implement this. If you want to use tiles/validator then you need to extend their appropriate request processor.
3 - Have every one of your actions extend MyBaseAction. MyBaseAction declares the execute() method as final, but invokes myExecute() which is what your subclasses implement. You can then put the login check in the execute method of MyBaseAction, and ensure it is always called, while retaining most of the flexibility of struts.
Pro: Simple and effective, requires no config change.
Con: Not quite standard method call for your child actions.
Theres more than one way to skin a cat :-)
Cheers,
evnafets