You can't do it with a redirectAction
, where parameters names and values can be dynamic but the number of parameters must be hard-coded, like
<result name="success" type="redirectAction">
<param name="actionName">hierUpdateMDA</param>
<param name="${paramName1}">${paramValue1}</param>
<param name="${paramName2}">${paramValue2}</param>
<param name="${paramName3}">${paramValue3}</param>
But you can do it with a redirect
result (that is generally used to redirect to non-action URLs).
Basically, you need to specify only the namespace and the action name (and they could be dynamic too, TBH) in Struts configuration, and a dynamic parameter representing the QueryString.
Then in the first Action (or in a BaseAction), you need a method to get the Parameter Map, loop through each parameter (and each one of its values), URLEncode them and return the mounted QueryString. That's it.
This will work with form data (POST), query parameters (generally GET) or both (POST with form data and QueryString), and it's URL safe.
Struts config
<package name="requestGrabber" namespace="cool" extends="struts-default">
<action name="source" class="org.foo.bar.cool.RequestGrabberAction"
method="source">
<result type="redirect">
<param name="location">/cool/target.action${queryParameters}</param>
</result>
</action>
<action name="target" class="org.foo.bar.cool.RequestGrabberAction"
method="target">
<result name="success">/cool/requestGrabber.jsp</result>
</action>
</package>
org.foo.bar.cool.RequestGrabberAction.java (Action classes)
package org.foo.bar.cool;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class RequestGrabberAction extends ActionSupport
implements ServletRequestAware {
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request){
this.request = request;
}
public String source(){
System.out.println("Source Action executed");
return SUCCESS;
}
public String target(){
System.out.println("Target Action executed");
return SUCCESS;
}
public String getQueryParameters() throws UnsupportedEncodingException {
String queryString = "";
// Get parameters, both POST and GET data
Enumeration<String> parameterNames = request.getParameterNames();
// Loop through names
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
// Loop through each value for a single parameter name
for (String paramValue : request.getParameterValues(paramName)) {
// Add the URLEncoded pair
queryString += URLEncoder.encode(paramName, "UTF-8") + "="
+ URLEncoder.encode(paramValue,"UTF-8") + "&";
}
}
// If not empty, prepend "?" and remove last "&"
if (queryString.length()>0){
queryString = "?"
+ (queryString.substring(0,queryString.length()-1));
}
return queryString;
}
}
/cool/requestGrabber.jsp
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!doctype html>
<html>
<head>
<title>Request Grabber</title>
</head>
<body>
QueryString = <s:property value="queryParameters" />
<s:form action="source" namespace="/cool">
<s:textfield name="foo" value="%{#parameters.foo}" />
<s:textfield name="bar" value="%{#parameters.bar}" />
<s:submit />
</s:form>
</body>
</html>
Enjoy