View Javadoc

1   /*
2    * $Id: ActionMappingParametersInteceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.interceptor;
23  
24  import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
25  import com.opensymphony.xwork2.ActionContext;
26  
27  import java.util.Map;
28  import java.util.Collections;
29  
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.dispatcher.mapper.ActionMapping;
32  
33  /***
34   * <!-- START SNIPPET: description -->
35   * This interceptor sets all parameters from the action mapping, for this request, on the value stack.  It operates
36   * exactly like {@link ParametersInterceptor}, only the parameters come from the {@link ActionMapping}, not the
37   * {@link ActionContext#getParameters()} method.
38   * <!-- END SNIPPET: description -->
39   *
40   * <p/> <u>Interceptor parameters:</u>
41   *
42   * <!-- START SNIPPET: parameters -->
43   *
44   * <ul>
45   *
46   * <li>ordered - set to true if you want the top-down property setter behaviour</li>
47   *
48   * </ul>
49   *
50   * <!-- END SNIPPET: parameters -->
51   *
52   * <p/> <u>Extending the interceptor:</u>
53   *
54   * <!-- START SNIPPET: extending -->
55   *
56   * <p/> The best way to add behavior to this interceptor is to utilize the {@link com.opensymphony.xwork2.interceptor.ParameterNameAware} interface in your
57   * actions. However, if you wish to apply a global rule that isn't implemented in your action, then you could extend
58   * this interceptor and override the {@link #acceptableName(String)} method.
59   *
60   * <!-- END SNIPPET: extending -->
61   *
62   * <p/> <u>Example code:</u>
63   *
64   * <pre>
65   * <!-- START SNIPPET: example -->
66   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
67   *     &lt;interceptor-ref name="mappingParams"/&gt;
68   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
69   * &lt;/action&gt;
70   * <!-- END SNIPPET: example -->
71   * </pre>
72   */
73  public class ActionMappingParametersInteceptor extends ParametersInterceptor {
74  
75      /***
76       * @param ac The action context
77       * @return the parameters from the action mapping in the context.  If none found, returns
78       * an empty map.
79       */
80      protected Map retrieveParametersFromContext(ActionContext ac) {
81          ActionMapping mapping = (ActionMapping) ac.get(ServletActionContext.ACTION_MAPPING);
82          if (mapping != null) {
83              return mapping.getParams();
84          } else {
85              return Collections.EMPTY_MAP;
86          }
87      }
88  }