View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.dispatcher.servlet;
22  
23  import org.apache.struts.action.ActionMapping;
24  import org.apache.struts.chain.contexts.ActionContext;
25  import org.apache.struts.chain.contexts.ServletActionContext;
26  import org.apache.struts.dispatcher.AbstractEventMappingDispatcher;
27  
28  import jakarta.servlet.http.HttpServletRequest;
29  
30  /**
31   * This servlet-based dispatcher chooses the target method based on the
32   * <code>parameter</code> attribute of the {@link ActionMapping} that matches
33   * a submission parameter. This is useful for developers who prefer to use many
34   * submit buttons, images, or submit links on a single form and whose related
35   * actions exist in a single action.
36   * <p>
37   * To configure the use of this action in your configuration, create an entry
38   * like this:
39   *
40   * <pre><code>
41   *   &lt;action path=&quot;/saveSubscription&quot;
42   *           type=&quot;org.example.SubscriptionAction&quot;
43   *           dispatcher=&quot;org.apache.struts.dispatcher.servlet.ServletEventMappingDispatcher&quot;/&gt;
44   *           parameter=&quot;save,back,recalc=recalculate,default=save&quot;/&gt;
45   *           name=&quot;subscriptionForm&quot;
46   *           scope=&quot;request&quot;
47   *           input=&quot;/subscription.jsp&quot;
48   * </code></pre>
49   *
50   * where <code>parameter</code> contains three possible methods and one
51   * default method if nothing matches (such as the user pressing the enter key).
52   *
53   * @version $Rev$
54   * @since Struts 1.4
55   */
56  public class ServletEventMappingDispatcher extends AbstractEventMappingDispatcher {
57  
58      private static final long serialVersionUID = 1L;
59  
60      /**
61       * Constructs a new servlet event mapping dispatcher.
62       */
63      public ServletEventMappingDispatcher() {
64          super(new ServletMethodResolver());
65      }
66  
67      /**
68       * If the method key exists as a standalone parameter or with the image
69       * suffixes (.x/.y), the method name has been found.
70       */
71      protected boolean isSubmissionParameter(ActionContext context, String methodKey) {
72          HttpServletRequest request = ((ServletActionContext) context).getRequest();
73          return (request.getParameter(methodKey) != null) || (request.getParameter(methodKey + ".x") != null);
74      }
75  
76  }