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.dispatcher;
23
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.struts2.dispatcher.mapper.ActionMapper;
31 import org.apache.struts2.dispatcher.mapper.ActionMapping;
32 import org.apache.struts2.views.util.UrlHelper;
33
34 import com.opensymphony.xwork2.ActionInvocation;
35 import com.opensymphony.xwork2.config.entities.ResultConfig;
36 import com.opensymphony.xwork2.util.logging.Logger;
37 import com.opensymphony.xwork2.util.logging.LoggerFactory;
38 import com.opensymphony.xwork2.util.reflection.ReflectionException;
39 import com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler;
40
41 /***
42 * <!-- START SNIPPET: description -->
43 *
44 * This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to redirect the browser to a
45 * URL that invokes the specified action and (optional) namespace. This is better than the {@link ServletRedirectResult}
46 * because it does not require you to encode the URL patterns processed by the {@link ActionMapper} in to your struts.xml
47 * configuration files. This means you can change your URL patterns at any point and your application will still work.
48 * It is strongly recommended that if you are redirecting to another action, you use this result rather than the
49 * standard redirect result.
50 *
51 * See examples below for an example of how request parameters could be passed in.
52 *
53 * <!-- END SNIPPET: description -->
54 *
55 * <b>This result type takes the following parameters:</b>
56 *
57 * <!-- START SNIPPET: params -->
58 *
59 * <ul>
60 *
61 * <li><b>actionName (default)</b> - the name of the action that will be redirect to</li>
62 *
63 * <li><b>namespace</b> - used to determine which namespace the action is in that we're redirecting to . If namespace is
64 * null, this defaults to the current namespace</li>
65 *
66 * <li><b>supressEmptyParameters</b> - optional boolean (defaults to false) that can prevent parameters with no values
67 * from being included in the redirect URL.</li>
68 *
69 * </ul>
70 *
71 * <!-- END SNIPPET: params -->
72 *
73 * <b>Example:</b>
74 *
75 * <pre><!-- START SNIPPET: example -->
76 * <package name="public" extends="struts-default">
77 * <action name="login" class="...">
78 * <!-- Redirect to another namespace -->
79 * <result type="redirectAction">
80 * <param name="actionName">dashboard</param>
81 * <param name="namespace">/secure</param>
82 * </result>
83 * </action>
84 * </package>
85 *
86 * <package name="secure" extends="struts-default" namespace="/secure">
87 * <-- Redirect to an action in the same namespace -->
88 * <action name="dashboard" class="...">
89 * <result>dashboard.jsp</result>
90 * <result name="error" type="redirectAction">error</result>
91 * </action>
92 *
93 * <action name="error" class="...">
94 * <result>error.jsp</result>
95 * </action>
96 * </package>
97 *
98 * <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
99 * <-- Pass parameters (reportType, width and height) -->
100 * <!--
101 * The redirectAction url generated will be :
102 * /genReport/generateReport.action?reportType=pie&width=100&height=100
103 * -->
104 * <action name="gatherReportInfo" class="...">
105 * <result name="showReportResult" type="redirectAction">
106 * <param name="actionName">generateReport</param>
107 * <param name="namespace">/genReport</param>
108 * <param name="reportType">pie</param>
109 * <param name="width">100</param>
110 * <param name="height">100</param>
111 * <param name="empty"></param>
112 * <param name="supressEmptyParameters">true</param>
113 * </result>
114 * </action>
115 * </package>
116 *
117 *
118 * <!-- END SNIPPET: example --></pre>
119 *
120 * @see ActionMapper
121 */
122 public class ServletActionRedirectResult extends ServletRedirectResult implements ReflectionExceptionHandler {
123
124 private static final long serialVersionUID = -9042425229314584066L;
125
126 /*** The default parameter */
127 public static final String DEFAULT_PARAM = "actionName";
128
129 private static final Logger LOG = LoggerFactory.getLogger(ServletActionRedirectResult.class);
130
131 protected String actionName;
132 protected String namespace;
133 protected String method;
134 protected boolean supressEmptyParameters = false;
135
136 private Map<String, String> requestParameters = new LinkedHashMap<String, String>();
137
138 public ServletActionRedirectResult() {
139 super();
140 }
141
142 public ServletActionRedirectResult(String actionName) {
143 this(null, actionName, null);
144 }
145
146 public ServletActionRedirectResult(String actionName, String method) {
147 this(null, actionName, method);
148 }
149
150 public ServletActionRedirectResult(String namespace, String actionName, String method) {
151 super(null);
152 this.namespace = namespace;
153 this.actionName = actionName;
154 this.method = method;
155 }
156
157 protected List<String> prohibitedResultParam = Arrays.asList(new String[] {
158 DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location",
159 "prependServletContext", "supressEmptyParameters" });
160
161 /***
162 * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
163 */
164 public void execute(ActionInvocation invocation) throws Exception {
165 actionName = conditionalParse(actionName, invocation);
166 if (namespace == null) {
167 namespace = invocation.getProxy().getNamespace();
168 } else {
169 namespace = conditionalParse(namespace, invocation);
170 }
171 if (method == null) {
172 method = "";
173 }
174 else {
175 method = conditionalParse(method, invocation);
176 }
177
178 String resultCode = invocation.getResultCode();
179 if (resultCode != null) {
180 ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(
181 resultCode);
182 Map resultConfigParams = resultConfig.getParams();
183 for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) {
184 Map.Entry e = (Map.Entry) i.next();
185 if (! prohibitedResultParam.contains(e.getKey())) {
186 requestParameters.put(e.getKey().toString(),
187 e.getValue() == null ? "":
188 conditionalParse(e.getValue().toString(), invocation));
189 String potentialValue = e.getValue() == null ? "": conditionalParse(e.getValue().toString(), invocation);
190 if (!supressEmptyParameters || ((potentialValue != null) && (potentialValue.length() > 0))) {
191 requestParameters.put(e.getKey().toString(), potentialValue);
192 }
193 }
194 }
195 }
196
197 StringBuffer tmpLocation = new StringBuffer(actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)));
198 UrlHelper.buildParametersString(requestParameters, tmpLocation, "&");
199
200 setLocation(tmpLocation.toString());
201
202 super.execute(invocation);
203 }
204
205 /***
206 * Sets the action name
207 *
208 * @param actionName The name
209 */
210 public void setActionName(String actionName) {
211 this.actionName = actionName;
212 }
213
214 /***
215 * Sets the namespace
216 *
217 * @param namespace The namespace
218 */
219 public void setNamespace(String namespace) {
220 this.namespace = namespace;
221 }
222
223 /***
224 * Sets the method
225 *
226 * @param method The method
227 */
228 public void setMethod(String method) {
229 this.method = method;
230 }
231
232 /***
233 * Sets the supressEmptyParameters option
234 *
235 * @param suppress The new value for this option
236 */
237 public void setSupressEmptyParameters(boolean supressEmptyParameters) {
238 this.supressEmptyParameters = supressEmptyParameters;
239 }
240
241 /***
242 * Adds a request parameter to be added to the redirect url
243 *
244 * @param key The parameter name
245 * @param value The parameter value
246 */
247 public ServletActionRedirectResult addParameter(String key, Object value) {
248 requestParameters.put(key, String.valueOf(value));
249 return this;
250 }
251
252 public void handle(ReflectionException ex) {
253
254 LOG.debug(ex.getMessage(), ex);
255 }
256
257 }