View Javadoc

1   /*
2    * $Id: ForwardAction.java 376812 2006-02-10 19:42:38Z husted $
3    *
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.actions;
19  
20  import org.apache.struts.action.ActionForm;
21  import org.apache.struts.action.ActionForward;
22  import org.apache.struts.action.ActionMapping;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  /***
29   * <p>An <strong>Action</strong> that forwards to the context-relative URI
30   * specified by the <code>parameter</code> property of our associated
31   * <code>ActionMapping</code>.  This can be used to integrate Struts with
32   * other business logic components that are implemented as servlets (or JSP
33   * pages), but still take advantage of the Struts controller servlet's
34   * functionality (such as processing of form beans).</p>
35   *
36   * <p>To configure the use of this Action in your <code>struts-config.xml</code>
37   * file, create an entry like this:</p>
38   *
39   * <code> &lt;action path="/saveSubscription" type="org.apache.struts.actions.ForwardAction"
40   * name="subscriptionForm" scope="request" input="/subscription.jsp"
41   * parameter="/path/to/processing/servlet"/&gt; </code>
42   *
43   * <p>which will forward control to the context-relative URI specified by the
44   * <code>parameter</code> attribute.</p>
45   *
46   * @version $Rev: 376812 $ $Date: 2005-08-14 17:24:39 -0400 (Sun, 14 Aug 2005)
47   *          $
48   */
49  public class ForwardAction extends BaseAction {
50      // ----------------------------------------------------- Instance Variables
51  
52      /***
53       * Process the specified HTTP request, and create the corresponding HTTP
54       * response (or forward to another web component that will create it).
55       * Return an <code>ActionForward</code> instance describing where and how
56       * control should be forwarded, or <code>null</code> if the response has
57       * already been completed.
58       *
59       * @param mapping  The ActionMapping used to select this instance
60       * @param form     The optional ActionForm bean for this request (if any)
61       * @param request  The HTTP request we are processing
62       * @param response The HTTP response we are creating
63       * @return The forward to which control should be transferred, or
64       *         <code>null</code> if the response has been completed.
65       * @throws Exception if an error occurs
66       */
67      public ActionForward execute(ActionMapping mapping, ActionForm form,
68          HttpServletRequest request, HttpServletResponse response)
69          throws Exception {
70          // Create a RequestDispatcher the corresponding resource
71          String path = mapping.getParameter();
72  
73          if (path == null) {
74              throw new ServletException(messages.getMessage("forward.path"));
75          }
76  
77          // Let the controller handle the request
78          ActionForward retVal = new ActionForward(path);
79  
80          return retVal;
81      }
82  }