Search code examples
javaapachestrutsstruts-1

Struts (Java), changing forward name for an action that takes input parameter=page doesn't let me access that page at new name


I'm still fairly new to Struts (for a legacy project for current employer), in this case I am using Struts 1 so not sure if that matters.

I am trying to edit an action that has a bunch of forwards but for simplicity I have just included one. This particular forward just maps to a static JSP and can normally be accessed without having to login to the application.

So normally this page is accessed at (assuming I'm running this locally):

https://localhost/sc.do?page=download

The forward is set as:

<forward name="download" path=".downloadPage"/>

Now all I tried doing was changing the name download to something else (in the forward below), such as downloadtest, I made sure to rebuild it, etc and relaunched, now I cannot access it at

https://localhost/sc.do?page=download but neither can I access the page at: https://localhost/sc.do?page=downloadtest

I'm really confused as to what else there would be in a configuration file that would prevent this from working, I have been reading tutorials and from my understanding this should work. Any advice is greatly appreciated.

      <action path="/sc"
              type="com.somecompany.wa.actions.PageDispatchAction"
              name="scAdminForm"
              parameter="page">

        <set-property property="secure" value="true"/>

        <forward name="downloadtest" path=".downloadPage"/>
</action>

This is the com.somecompany.wa.actions.PageDispatchAction class:

public class PageDispatchAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionContext context = new ActionContext(mapping, form, request, response);
        Command command = new CheckPageSecurityCommand();
        boolean stop = command.execute(context);
        if (stop) {
            return null;
        } else {
            return (ActionForward) context.get("forward");
        }
    }

    // redirects control to logout page
    // calls to this method should be followed by return null or end of method.
    protected void redirectToLogout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ServletWebContext context = new ServletWebContext(request.getSession(true).getServletContext(), request, response);
        Command command = new RedirectToLogoutCommand();
        command.execute(context);
    }
}

Solution

  • I'm more familiar with Struts 1.2 and what you have here looks like Struts 1.3, but I was surprised to see that PageDispatchAction didn't extend DispatchAction instead of Action. Typically, when you define the parameter attribute of an action in struts-config.xml the type refers to a subclass of DispatchAction. This indicates that whatever value for page is supplied in the request would map to a method in the action of the same name with the way things are defined here. So page=download would map to a download method in PageDispatchAction. Anyway, I'm not sure how what you have here is working since to my eye it looks like the return is looking for an ActionForward called forward. Perhaps it is my Struts 1.2 bias though and 1.3 works differently. Not sure.