KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > actions > IncludeAction


1 /*
2  * $Id: IncludeAction.java 54929 2004-10-16 16:38:42Z germuska $
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  
19 package org.apache.struts.actions;
20
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.struts.action.Action;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.upload.MultipartRequestWrapper;
31 import org.apache.struts.util.MessageResources;
32
33 /**
34  * <p>An <strong>Action</strong> that includes the context-relative
35  * URI specified by the <code>parameter</code> property of our associated
36  * <code>ActionMapping</code>. This can be used to integrate Struts with
37  * other business logic components that are implemented as servlets (or JSP
38  * pages), but still take advantage of the Struts controller servlet's
39  * functionality (such as processing of form beans).</p>
40  *
41  * <p>To configure the use of this Action in your
42  * <code>struts-config.xml</code> file, create an entry like this:</p>
43  *
44  * <code>
45  * &lt;action path="/saveSubscription"
46  * type="org.apache.struts.actions.IncludeAction"
47  * name="subscriptionForm"
48  * scope="request"
49  * input="/subscription.jsp"
50  * parameter="/path/to/processing/servlet"&gt;
51  * </code>
52  *
53  * <p>which will include the context-relative URI specified by the
54  * <code>parameter</code> attribute.</p>
55  *
56  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
57  */

58 public class IncludeAction extends Action {
59
60
61     // ----------------------------------------------------- Instance Variables
62

63
64     /**
65      * The message resources for this package.
66      */

67     protected static MessageResources messages =
68      MessageResources.getMessageResources
69         ("org.apache.struts.actions.LocalStrings");
70
71
72     // --------------------------------------------------------- Public Methods
73

74
75     /**
76      * Process the specified HTTP request, and create the corresponding HTTP
77      * response (or forward to another web component that will create it).
78      * Return an <code>ActionForward</code> instance describing where and how
79      * control should be forwarded, or <code>null</code> if the response has
80      * already been completed.
81      *
82      * @param mapping The ActionMapping used to select this instance
83      * @param form The optional ActionForm bean for this request (if any)
84      * @param request The HTTP request we are processing
85      * @param response The HTTP response we are creating
86      *
87      * @exception Exception if an error occurs
88      */

89     public ActionForward execute(
90         ActionMapping mapping,
91         ActionForm form,
92         HttpServletRequest JavaDoc request,
93         HttpServletResponse JavaDoc response)
94         throws Exception JavaDoc {
95
96         // Create a RequestDispatcher the corresponding resource
97
String JavaDoc path = mapping.getParameter();
98         if (path == null) {
99             throw new ServletException JavaDoc(messages.getMessage("include.path"));
100         }
101
102         RequestDispatcher JavaDoc rd =
103             servlet.getServletContext().getRequestDispatcher(path);
104
105         if (rd == null) {
106             throw new ServletException JavaDoc(messages.getMessage("include.rd", path));
107         }
108
109         // Unwrap the multipart request, if there is one.
110
if (request instanceof MultipartRequestWrapper) {
111             request = ((MultipartRequestWrapper) request).getRequest();
112         }
113
114         // Forward control to the specified resource
115
rd.include(request, response);
116
117         // Tell the controller servlet that the response has been created
118
return (null);
119
120     }
121
122
123 }
124
125
Popular Tags