Recipe6.5.Forwarding Requests


Recipe 6.5. Forwarding Requests

Problem

You want to provide an action that forwards a request to any action, JSP page, servlet, or other resource of your web application.

Solution

Use a forwarding action. Specifying the module-relative path to the resource as the value of the forward attribute is the most convenient way:

<action  path="/ForwardTest"     forward="/forward_test.jsp"/>

Alternatively, if you use a custom RequestProcessor, which overrides the processForwardConfig( ) method, you must use the Struts built-in ForwardAction, specifying the context-relative path for the value of the parameter attribute.

<action  path="/ForwardActionTest"          type="org.apache.struts.actions.ForwardAction"     parameter="/forward_test.jsp"/>

Discussion

Struts provides two mechanisms for creating an action that forwards directly to a specified resource. You can use the forward attribute or the ForwardAction. In most situations, the forward attribute will work just fine. The value of the attribute is treated as a module-relative path. At request time, the RequestProcessor checks the action mapping specifies a forward attribute. If so, it converts the forward attribute value to a context-relative path by adding the module prefix to the front of the path. The request processor then hands the request and response to the RequestDispatcher.forward( ) method and immediately returns.

The ForwardAction is a Struts prebuilt action class included with the Struts distribution. This Action forwards the request to the path specified as the value of the parameter attribute in an action mapping. The parameter attribute may contain an HTTP query string for passing request parameters.

<action  path="/SendToLegacyServlet"          type="org.apache.struts.actions.ForwardAction"     parameter="/LegacyServlet?foo=bar"/>

Unlike handling the forward attribute, when you use the ForwardAction, the RequestProcessor routes the request through all steps of the process. This difference is relevant if you are using a custom RequestProcessor, which overrides the processForwardConfig( ) method, for custom processing of action forwards. If so, you should use the ForwardAction to preserve your custom processing.

Whether using the forward attribute or ForwardAction, your action acts as a bridge between requests. Since the action performs a forward and not a redirect, request data is maintained.

If you are prototyping a Struts application, a forwarding action can save you significant time and effort as your application develops. A forwarding action allows you to define your action pathsthe URIs that link the application togetherprior to coding the actual custom Action classes. Suppose your web designers have created a prototype comprised of static HTML pages and simple JSP pages. Instead of linking directly from page to page, create a forwarding action in your struts-config.xml that routes the request to the path of the prototype page.

You can use the action URI for hyperlinks and HTML form actions instead of the prototype JSP page name. As you create the real functionality of the application, reconfigure the action element to use your own custom Action class. You'll need to make changes to the JSPs, but you will already have established the workflow. This is a great development approach, particularly if you want to have a working prototype available.

This practice allows your application to take advantage of application-wide services provided by Struts. These services may be Struts-provided, like a role-based access, or they may be custom behaviors that you implement by extending the Struts RequestProcessor. Forwarding actions are useful for creating proxies to resources, such as traditional servlets. If you are adding Struts to an existing servlet-based application, you can use a forwarding action to route requests to the servlet. Once you have the proxy in place, you can replace the servlet with a JSP without breaking the application.

See Also

If you need to link to a servlet that is used to include a portion of the HTTP response, Recipe 6.6 provides a similar proxy ability as a forwarding action.

The Struts documentation on the ForwardAction can be found at http://jakarta.apache.org/struts/api/org/apache/struts/actions/ForwardAction.html.

The difference between the forward attribute and the ForwardAction has been discussed on the Struts-users mailing list. Searching the archives for "Forward attribute vs. Forward Action" will return several threads of interest.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net