View Javadoc

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