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
135 public ServletActionRedirectResult() {
136 super();
137 }
138
139 public ServletActionRedirectResult(String actionName) {
140 this(null, actionName, null);
141 }
142
143 public ServletActionRedirectResult(String actionName, String method) {
144 this(null, actionName, method);
145 }
146
147 public ServletActionRedirectResult(String namespace, String actionName, String method) {
148 super(null);
149 this.namespace = namespace;
150 this.actionName = actionName;
151 this.method = method;
152 }
153
154
155 /***
156 * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
157 */
158 public void execute(ActionInvocation invocation) throws Exception {
159 actionName = conditionalParse(actionName, invocation);
160 if (namespace == null) {
161 namespace = invocation.getProxy().getNamespace();
162 } else {
163 namespace = conditionalParse(namespace, invocation);
164 }
165 if (method == null) {
166 method = "";
167 }
168 else {
169 method = conditionalParse(method, invocation);
170 }
171
172 StringBuilder tmpLocation = new StringBuilder(actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)));
173
174 setLocation(tmpLocation.toString());
175
176 super.execute(invocation);
177 }
178
179 /***
180 * Sets the action name
181 *
182 * @param actionName The name
183 */
184 public void setActionName(String actionName) {
185 this.actionName = actionName;
186 }
187
188 /***
189 * Sets the namespace
190 *
191 * @param namespace The namespace
192 */
193 public void setNamespace(String namespace) {
194 this.namespace = namespace;
195 }
196
197 /***
198 * Sets the method
199 *
200 * @param method The method
201 */
202 public void setMethod(String method) {
203 this.method = method;
204 }
205
206 protected List<String> getProhibitedResultParams() {
207 return Arrays.asList(new String[]{
208 DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location",
209 "prependServletContext", "supressEmptyParameters"});
210 }
211 }