KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MappingDispatchAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2003,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.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30
31 /**
32  * <p>An abstract <strong>Action</strong> that dispatches to a public
33  * method that is named by the <code>parameter</code> attribute of
34  * the corresponding ActionMapping. This is useful for developers who prefer
35  * to combine many related actions into a single Action class.</p>
36  *
37  * <p>To configure the use of this action in your
38  * <code>struts-config.xml</code> file, create an entry like this:</p>
39  *
40  * <pre><code>
41  * &lt;action path="/saveSubscription"
42  * type="org.example.SubscriptionAction"
43  * name="subscriptionForm"
44  * scope="request"
45  * input="/subscription.jsp"
46  * parameter="method"/&gt;
47  * </code></pre>
48  *
49  * <p>where 'method' is the name of a method in your subclass of
50  * MappingDispatchAction that has the same signature (other than method
51  * name) of the standard Action.execute method. For example, you might combine
52  * the methods for managing a subscription into a single
53  * MappingDispatchAction class using the following methods:</p>
54  * <ul>
55  * <li>public ActionForward create(ActionMapping mapping, ActionForm form,
56  * HttpServletRequest request, HttpServletResponse response)
57  * throws Exception</li>
58  * <li>public ActionForward edit(ActionMapping mapping, ActionForm form,
59  * HttpServletRequest request, HttpServletResponse response)
60  * throws Exception</li>
61  * <li>public ActionForward save(ActionMapping mapping, ActionForm form,
62  * HttpServletRequest request, HttpServletResponse response)
63  * throws Exception</li>
64  * <li>public ActionForward delete(ActionMapping mapping, ActionForm form,
65  * HttpServletRequest request, HttpServletResponse response)
66  * throws Exception</li>
67  * <li>public ActionForward list(ActionMapping mapping, ActionForm form,
68  * HttpServletRequest request, HttpServletResponse response)
69  * throws Exception</li>
70  * </ul>
71  * <p>for which you would create corresponding &lt;action&gt; configurations
72  * that reference this class:</p>
73  *
74  * <pre><code>
75  * &lt;action path="/createSubscription"
76  * type="org.example.SubscriptionAction"
77  * parameter="create"&gt;
78  * &lt;forward name="success" path="/editSubscription.jsp"/&gt;
79  * &lt;/action&gt;
80  *
81  * &lt;action path="/editSubscription"
82  * type="org.example.SubscriptionAction"
83  * parameter="edit"&gt;
84  * &lt;forward name="success" path="/editSubscription.jsp"/&gt;
85  * &lt;/action&gt;
86  *
87  * &lt;action path="/saveSubscription"
88  * type="org.example.SubscriptionAction"
89  * parameter="save"
90  * name="subscriptionForm"
91  * validate="true"
92  * input="/editSubscription.jsp"
93  * scope="request"&gt;
94  * &lt;forward name="success" path="/savedSubscription.jsp"/&gt;
95  * &lt;/action&gt;
96  *
97  * &lt;action path="/deleteSubscription"
98  * type="org.example.SubscriptionAction"
99  * name="subscriptionForm"
100  * scope="request"
101  * input="/subscription.jsp"
102  * parameter="delete"&gt;
103  * &lt;forward name="success" path="/deletedSubscription.jsp"/&gt;
104  * &lt;/action&gt;
105  *
106  * &lt;action path="/listSubscriptions"
107  * type="org.example.SubscriptionAction"
108  * parameter="list"&gt;
109  * &lt;forward name="success" path="/subscriptionList.jsp"/&gt;
110  * &lt;/action&gt;
111  * </code></pre>
112  *
113  * <p><strong>NOTE</strong> - Unlike DispatchAction, mapping characteristics
114  * may differ between the various handlers, so you can combine actions in the
115  * same class that, for example, differ in their use of forms or validation.
116  * Also, a request parameter, which would be visible to the application user,
117  * is not required to enable selection of the handler method.
118  * </p>
119  *
120  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
121  * @since Struts 1.2
122  */

123 public class MappingDispatchAction extends DispatchAction {
124
125
126     // -------------------------------------------------------- Class Variables
127

128
129     /**
130      * Commons Logging instance.
131      */

132     private static Log log =
133         LogFactory.getLog(MappingDispatchAction.class);
134
135
136     // --------------------------------------------------------- Public Methods
137

138
139     /**
140      * Process the specified HTTP request, and create the corresponding HTTP
141      * response (or forward to another web component that will create it).
142      * Return an <code>ActionForward</code> instance describing where and how
143      * control should be forwarded, or <code>null</code> if the response has
144      * already been completed.
145      *
146      * This method dispatches the request to other methods of
147      * <code>MappingDispatchAction</code> using the 'parameter' attribute of
148      * <code>ActionMapping</code> and Java Introspection.
149      *
150      * @param mapping The ActionMapping used to select this instance
151      * @param form The optional ActionForm bean for this request (if any)
152      * @param request The HTTP request we are processing
153      * @param response The HTTP response we are creating
154      *
155      * @return Return an <code>ActionForward</code> instance describing where
156      * and how control should be forwarded, or <code>null</code> if
157      * the response has already been completed.
158      *
159      * @exception Exception if an exception occurs
160      */

161     public ActionForward execute(
162         ActionMapping mapping,
163         ActionForm form,
164         HttpServletRequest JavaDoc request,
165         HttpServletResponse JavaDoc response)
166         throws Exception JavaDoc {
167         
168         // Use the overridden getMethodName.
169
return super.execute(mapping, form, request, response);
170     }
171
172
173     /**
174      * Method which is dispatched to when there is no value for the
175      * parameter in the ActionMapping. Subclasses of
176      * <code>MappingDispatchAction</code> should override this method
177      * if they wish to provide default behavior different than throwing a
178      * ServletException.
179      *
180      * @param mapping The ActionMapping used to select this instance
181      * @param form The optional ActionForm bean for this request (if any)
182      * @param request The HTTP request we are processing
183      * @param response The HTTP response we are creating
184      *
185      * @return Return an <code>ActionForward</code> instance describing where
186      * and how control should be forwarded, or <code>null</code> if
187      * the response has already been completed.
188      *
189      * @exception Exception if an exception occurs
190      */

191     protected ActionForward unspecified(
192         ActionMapping mapping,
193         ActionForm form,
194         HttpServletRequest JavaDoc request,
195         HttpServletResponse JavaDoc response)
196         throws Exception JavaDoc {
197
198         String JavaDoc message =
199             messages.getMessage("mapping.parameter", mapping.getPath());
200
201         log.error(message);
202
203         throw new ServletException JavaDoc(message);
204     }
205
206     /**
207      * Returns the method name, given a parameter's value.
208      *
209      * @param mapping The ActionMapping used to select this instance
210      * @param form The optional ActionForm bean for this request (if any)
211      * @param request The HTTP request we are processing
212      * @param response The HTTP response we are creating
213      * @param parameter The <code>ActionMapping</code> parameter's name
214      *
215      * @return The method's name.
216      * @since Struts 1.2.0
217      */

218     protected String JavaDoc getMethodName(
219         ActionMapping mapping,
220         ActionForm form,
221         HttpServletRequest JavaDoc request,
222         HttpServletResponse JavaDoc response,
223         String JavaDoc parameter)
224         throws Exception JavaDoc {
225         
226         // Return the unresolved mapping parameter.
227
return parameter;
228     }
229
230 }
231
Popular Tags