001: package com.opensymphony.webwork.dispatcher;
002:
003: import java.util.Arrays;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import com.opensymphony.webwork.dispatcher.mapper.ActionMapper;
010: import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
011: import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
012: import com.opensymphony.webwork.views.util.UrlHelper;
013:
014: import com.opensymphony.xwork.ActionInvocation;
015: import com.opensymphony.xwork.config.entities.ActionConfig;
016: import com.opensymphony.xwork.config.entities.ResultConfig;
017:
018: /**
019: * <!-- START SNIPPET: description -->
020: *
021: * This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to redirect the browser to a
022: * URL that invokes the specified action and (optional) namespace. This is better than the {@link ServletRedirectResult}
023: * because it does not require you to encode the URL patterns processed by the {@link ActionMapper} in to your xwork.xml
024: * configuration files. This means you can change your URL patterns at any point and your application will still work.
025: * It is strongly recommended that if you are redirecting to another action, you use this result rather than the
026: * standard redirect result.
027: *
028: * <p/>
029: *
030: * To pass parameters, the <param> ... </param> tag. The following parameters will not be
031: * passable becuase they are part of the config param for this particular result.
032: *
033: * <ul>
034: * <li>actionName</li>
035: * <li>namespace</li>
036: * <li>method</li>
037: * <li>encode</li>
038: * <li>parse</li>
039: * <li>location</li>
040: * <li>prependServletContext</li>
041: * </ul>
042: *
043: * See examples below for an example of how request parameters could be passed in.
044: *
045: * <!-- END SNIPPET: description -->
046: *
047: * <b>This result type takes the following parameters:</b>
048: *
049: * <!-- START SNIPPET: params -->
050: *
051: * <ul>
052: *
053: * <li><b>actionName (default)</b> - the name of the action that will be redirect to</li>
054: *
055: * <li><b>namespace</b> - used to determine which namespace the action is in that we're redirecting to . If namespace is
056: * null, this defaults to the current namespace</li>
057: *
058: * </ul>
059: *
060: * <!-- END SNIPPET: params -->
061: *
062: * <b>Example:</b>
063: *
064: * <pre><!-- START SNIPPET: example -->
065: * <package name="public" extends="webwork-default">
066: * <action name="login" class="...">
067: * <!-- Redirect to another namespace -->
068: * <result type="redirect-action">
069: * <param name="actionName">dashboard</param>
070: * <param name="namespace">/secure</param>
071: * </result>
072: * </action>
073: * </package>
074: *
075: * <package name="secure" extends="webwork-default" namespace="/secure">
076: * <-- Redirect to an action in the same namespace -->
077: * <action name="dashboard" class="...">
078: * <result>dashboard.jsp</result>
079: * <result name="error" type="redirect-action>error</result>
080: * </action>
081: *
082: * <action name="error" class="...">
083: * <result>error.jsp</result>
084: * </action>
085: * </package>
086: *
087: * <package name="passingRequestParameters" extends="webwork-default" namespace="/passingRequestParameters">
088: * <-- Pass parameters (reportType, width and height) -->
089: * <!--
090: * The redirect-action url generated will be :
091: * /genReport/generateReport.action?reportType=pie&width=100&height=100
092: * -->
093: * <action name="gatherReportInfo" class="...">
094: * <result name="showReportResult" type="redirect-action">
095: * <param name="actionName">generateReport</param>
096: * <param name="namespace=">/genReport</param>
097: * <param name="reportType">pie</param>
098: * <param name="width">100</param>
099: * <param name="height">100</param>
100: * </result>
101: * </action>
102: * </package>
103: *
104: * <!-- END SNIPPET: example --></pre>
105: *
106: * @see ActionMapper
107: */
108: public class ServletActionRedirectResult extends ServletRedirectResult {
109:
110: private static final long serialVersionUID = -6126889518132056284L;
111:
112: public static final String DEFAULT_PARAM = "actionName";
113:
114: protected String actionName;
115: protected String namespace;
116: protected String method;
117:
118: protected List prohibitedResultParam = Arrays.asList(new String[] {
119: DEFAULT_PARAM, "namespace", "method", "encode", "parse",
120: "location", "prependServletContext" });
121:
122: public void execute(ActionInvocation invocation) throws Exception {
123: actionName = conditionalParse(actionName, invocation);
124: if (namespace == null) {
125: namespace = invocation.getProxy().getNamespace();
126: } else {
127: namespace = conditionalParse(namespace, invocation);
128: }
129: if (method == null) {
130: method = "";
131: } else {
132: method = conditionalParse(method, invocation);
133: }
134:
135: Map requestParameters = new HashMap();
136: ResultConfig resultConfig = (ResultConfig) invocation
137: .getProxy().getConfig().getResults().get(
138: invocation.getResultCode());
139: Map resultConfigParams = resultConfig.getParams();
140: for (Iterator i = resultConfigParams.entrySet().iterator(); i
141: .hasNext();) {
142: Map.Entry e = (Map.Entry) i.next();
143: if (!prohibitedResultParam.contains(e.getKey())) {
144: requestParameters.put(e.getKey().toString(), e
145: .getValue() == null ? "" : conditionalParse(e
146: .getValue().toString(), invocation));
147: }
148: }
149:
150: ActionMapper mapper = ActionMapperFactory.getMapper();
151: StringBuffer tmpLocation = new StringBuffer(mapper
152: .getUriFromActionMapping(new ActionMapping(actionName,
153: namespace, method, null)));
154: UrlHelper.buildParametersString(requestParameters, tmpLocation,
155: "&");
156:
157: location = tmpLocation.toString();
158:
159: super .execute(invocation);
160: }
161:
162: public void setActionName(String actionName) {
163: this .actionName = actionName;
164: }
165:
166: public void setNamespace(String namespace) {
167: this .namespace = namespace;
168: }
169:
170: public void setMethod(String method) {
171: this.method = method;
172: }
173: }
|