View Javadoc

1   /*
2    * $Id: ServletActionRedirectResult.java 755605 2009-03-18 14:36:33Z musachy $
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.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   * &lt;package name="public" extends="struts-default"&gt;
77   *     &lt;action name="login" class="..."&gt;
78   *         &lt;!-- Redirect to another namespace --&gt;
79   *         &lt;result type="redirectAction"&gt;
80   *             &lt;param name="actionName"&gt;dashboard&lt;/param&gt;
81   *             &lt;param name="namespace"&gt;/secure&lt;/param&gt;
82   *         &lt;/result&gt;
83   *     &lt;/action&gt;
84   * &lt;/package&gt;
85   *
86   * &lt;package name="secure" extends="struts-default" namespace="/secure"&gt;
87   *     &lt;-- Redirect to an action in the same namespace --&gt;
88   *     &lt;action name="dashboard" class="..."&gt;
89   *         &lt;result&gt;dashboard.jsp&lt;/result&gt;
90   *         &lt;result name="error" type="redirectAction"&gt;error&lt;/result&gt;
91   *     &lt;/action&gt;
92   *
93   *     &lt;action name="error" class="..."&gt;
94   *         &lt;result&gt;error.jsp&lt;/result&gt;
95   *     &lt;/action&gt;
96   * &lt;/package&gt;
97   *
98   * &lt;package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters"&gt;
99   *    &lt;-- Pass parameters (reportType, width and height) --&gt;
100  *    &lt;!--
101  *    The redirectAction url generated will be :
102  *    /genReport/generateReport.action?reportType=pie&width=100&height=100
103  *    --&gt;
104  *    &lt;action name="gatherReportInfo" class="..."&gt;
105  *       &lt;result name="showReportResult" type="redirectAction"&gt;
106  *          &lt;param name="actionName"&gt;generateReport&lt;/param&gt;
107  *          &lt;param name="namespace"&gt;/genReport&lt;/param&gt;
108  *          &lt;param name="reportType"&gt;pie&lt;/param&gt;
109  *          &lt;param name="width"&gt;100&lt;/param&gt;
110  *          &lt;param name="height"&gt;100&lt;/param&gt;
111  *          &lt;param name="empty"&gt;&lt;/param&gt;
112  *          &lt;param name="supressEmptyParameters"&gt;true&lt;/param&gt;
113  *       &lt;/result&gt;
114  *    &lt;/action&gt;
115  * &lt;/package&gt;
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 }