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 import java.util.TreeMap;
30
31 import org.apache.struts2.ServletActionContext;
32 import org.apache.struts2.dispatcher.mapper.ActionMapping;
33
34 /***
35 * <!-- START SNIPPET: description -->
36 * This interceptor sets all parameters from the action mapping, for this request, on the value stack. It operates
37 * exactly like {@link ParametersInterceptor}, only the parameters come from the {@link ActionMapping}, not the
38 * {@link ActionContext#getParameters()} method.
39 * <!-- END SNIPPET: description -->
40 * <p/>
41 * <p/> <u>Interceptor parameters:</u>
42 * <p/>
43 * <!-- START SNIPPET: parameters -->
44 * <p/>
45 * <ul>
46 * <p/>
47 * <li>ordered - set to true if you want the top-down property setter behaviour</li>
48 * <p/>
49 * </ul>
50 * <p/>
51 * <!-- END SNIPPET: parameters -->
52 * <p/>
53 * <p/> <u>Extending the interceptor:</u>
54 * <p/>
55 * <!-- START SNIPPET: extending -->
56 * <p/>
57 * <p/> The best way to add behavior to this interceptor is to utilize the {@link com.opensymphony.xwork2.interceptor.ParameterNameAware} interface in your
58 * actions. However, if you wish to apply a global rule that isn't implemented in your action, then you could extend
59 * this interceptor and override the {@link #acceptableName(String)} method.
60 * <p/>
61 * <!-- END SNIPPET: extending -->
62 * <p/>
63 * <p/> <u>Example code:</u>
64 * <p/>
65 * <pre>
66 * <!-- START SNIPPET: example -->
67 * <action name="someAction" class="com.examples.SomeAction">
68 * <interceptor-ref name="mappingParams"/>
69 * <result name="success">good_result.ftl</result>
70 * </action>
71 * <!-- END SNIPPET: example -->
72 * </pre>
73 */
74 public class ActionMappingParametersInteceptor extends ParametersInterceptor {
75
76 /***
77 * @param ac The action context
78 * @return the parameters from the action mapping in the context. If none found, returns
79 * an empty map.
80 */
81 @Override
82 protected Map<String, Object> retrieveParameters(ActionContext ac) {
83 ActionMapping mapping = (ActionMapping) ac.get(ServletActionContext.ACTION_MAPPING);
84 if (mapping != null) {
85 return mapping.getParams();
86 } else {
87 return Collections.emptyMap();
88 }
89 }
90
91 /***
92 * Adds the parameters into context's ParameterMap
93 *
94 * @param ac The action context
95 * @param newParams The parameter map to apply
96 * <p/>
97 * In this class this is a no-op, since the parameters were fetched from the same location.
98 * In subclasses both retrieveParameters() and addParametersToContext() should be overridden.
99 */
100 @Override
101 protected void addParametersToContext(ActionContext ac, Map newParams) {
102 Map previousParams = ac.getParameters();
103 Map combinedParams = null;
104 if (previousParams != null) {
105 combinedParams = new TreeMap(previousParams);
106 } else {
107 combinedParams = new TreeMap();
108 }
109 combinedParams.putAll(newParams);
110
111 ac.setParameters(combinedParams);
112 }
113 }