|
Recipe 7.4. Forwarding Users to Alternate DestinationsProblemYou want to define locations, such as other servlets, JSPs, or Struts components that you can forward users to from your application code. SolutionDefine 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> DiscussionThe 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 forwardsdefined using forward elements nested in the global-forwards elementcreate 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"> <forward name="success" path="/show_data_form.jsp"/> </action> Local forwards are used by action classes to specify a logical destination. The ActionMapping.findForward( ) method retrieves a forward by name. This method finds a matching local forward for the given name. If none can be found, it searches the global forwards. Here's how a typical custom Action uses the findForward( ) method to retrieve and return an ActionForward: public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // perform action here // forward to the logical "success" location return mapping.findForward("success"); } The forward element, whether global or local, accepts the attributes shown in Table 7-1.
The URI generated for the forward is determined by the contextRelative, module, and path attributes. For Struts 1.1 applications, the contextRelative attribute lets you create forwards between modules. For Struts 1.2, use the module attribute instead of contextRelative. Specifying a module value of / is the same as setting contextRelative to true. The path attribute contains the location to the desired resource. You can include request parameters by appending a query string. Just be sure to use the ampersand character entity (&) to separate name-value pairs. Table 7-2 contains some example global forwards and the resulting URIs.
See AlsoUsing a global forward specifically to switch to a page in a different module is covered in Recipe 7.5. More information on switching between modules can be found in Recipe 6-7. Global forwards can be used like bridges between workflows as described in Recipe 6.5. The Struts User Guide provides additional information on configuring and using global and local forwards. The relevant section can be found at http://struts.apache.org/userGuide/building_controller.html#config. |
|