1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <action name="someAction" class="com.examples.SomeAction">
67 * <interceptor-ref name="mappingParams"/>
68 * <result name="success">good_result.ftl</result>
69 * </action>
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 }