View Javadoc

1   /*
2    * $Id: IncludeAction.java 421158 2006-07-12 06:13:54Z wsmoak $
3    *
4    * Copyright 2001-2005 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.RequestDispatcher;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /***
30   * <p>An <strong>Action</strong> that includes the context-relative URI
31   * specified by the <code>parameter</code> property of our associated
32   * <code>ActionMapping</code>.  This can be used to integrate Struts with
33   * other business logic components that are implemented as servlets (or JSP
34   * pages), but still take advantage of the Struts controller servlet's
35   * functionality (such as processing of form beans).</p>
36   *
37   * <p>To configure the use of this Action in your <code>struts-config.xml</code>
38   * file, create an entry like this:</p>
39   *
40   * <code> &lt;action path="/saveSubscription"
41   * type="org.apache.struts.actions.IncludeAction"
42   * name="subscriptionForm" scope="request" input="/subscription.jsp"
43   * parameter="/path/to/processing/servlet"&gt; </code>
44   *
45   * <p>which will include the context-relative URI specified by the
46   * <code>parameter</code> attribute.</p>
47   *
48   * @version $Rev: 421158 $ $Date: 2005-11-09 00:11:45 -0500 (Wed, 09 Nov 2005)
49   *          $
50   */
51  public class IncludeAction extends BaseAction {
52      // ----------------------------------------------------- Instance Variables
53  
54      /***
55       * Process the specified HTTP request, and create the corresponding HTTP
56       * response (or forward to another web component that will create it).
57       * Return an <code>ActionForward</code> instance describing where and how
58       * control should be forwarded, or <code>null</code> if the response has
59       * already been completed.
60       *
61       * @param mapping  The ActionMapping used to select this instance
62       * @param form     The optional ActionForm bean for this request (if any)
63       * @param request  The HTTP request we are processing
64       * @param response The HTTP response we are creating
65       * @return The forward to which control should be transferred, or
66       *         <code>null</code> if the response has been completed.
67       * @throws Exception if an error occurs
68       */
69      public ActionForward execute(ActionMapping mapping, ActionForm form,
70          HttpServletRequest request, HttpServletResponse response)
71          throws Exception {
72          // Create a RequestDispatcher the corresponding resource
73          String path = mapping.getParameter();
74  
75          if (path == null) {
76              throw new ServletException(messages.getMessage("include.path"));
77          }
78  
79          RequestDispatcher rd =
80              servlet.getServletContext().getRequestDispatcher(path);
81  
82          if (rd == null) {
83              throw new ServletException(messages.getMessage("include.rd", path));
84          }
85  
86          // Forward control to the specified resource
87          rd.include(request, response);
88  
89          // Tell the controller servlet that the response has been created
90          return (null);
91      }
92  }