7.4. Forwarding Users to Alternate Destinations

Problem

You want to define locations, such as other servlets, JSPs, or Struts components that you can forward users to from your application code.

Solution

Define the global forwards in the struts-config.xml file. If the forward is for a specific module, define it in that module's struts-config.xml file:

<form-beans> 
  <!-- snipped ... -->
</form-beans>
<global-forwards>
    <forward name="main" path="/index.jsp" 
         redirect="true"/>
    <forward name="logon" path="/Logon.do" 
         contextRelative="true" redirect="true"/>
    <forward name="logoff" path="/Logoff.do" 
         contextRelative="true" redirect="true"/>
</global-forwards>
<global-exceptions> 
  <!-- snipped ... -->
</global-exceptions>

Discussion

The URL paths you use in your application commonly evolve and change as your application develops. You can create logical references to application paths using a Struts forward. Global forwards—defined using forward elements nested in the global-forwards element—create logical destinations that can be accessed from anywhere in your application. The Struts html:link, html:rewrite, and html:frame tags all support the forward attribute that accepts the name of a global forward. The tag uses the logical path for that forward to generate the actual URL.

Local forwards are specific to a given action. Local forwards are specified as nested elements of an action element:

<action path="/LoadData" type="com.oreilly.strutsckbk.ch07.LoadDataAction" scope="request" name="TestForm"> ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.