Date: 2004-06-17T14:58:11
Editor: MarkDiggory <[EMAIL PROTECTED]>
Wiki: Apache Struts Wiki
Page: ForwardingWithDifferentParameter
URL: http://wiki.apache.org/struts/ForwardingWithDifferentParameter
no comment
Change Log:
------------------------------------------------------------------------------
@@ -96,3 +96,75 @@
+How about a Wrapper class like below that provideds Parameter copying functionality?
- Mark Diggory
+
+{{{
+
+package edu.harvard.hmdc.curate.study;
+
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.net.URLEncoder;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.struts.action.ActionForward;
+
+public class MyActionForward extends ActionForward {
+
+ private Hashtable parameters = new Hashtable();
+
+ public MyActionForward(ActionForward forward) {
+
super(forward.getName(),forward.getPath(),forward.getRedirect(),forward.getContextRelative());
+ }
+
+ public MyActionForward(ActionForward forward, HttpServletRequest request) {
+
super(forward.getName(),forward.getPath(),forward.getRedirect(),forward.getContextRelative());
+ parameters.putAll(request.getParameterMap());
+ }
+
+ public void addParameter(String name, String value) {
+ String[] newValues = null;
+ String[] oldValues = (String[]) parameters.get(name);
+ if (oldValues == null) {
+ newValues = new String[1];
+ newValues[0] = value;
+ } else {
+ newValues = new String[oldValues.length + 1];
+ System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
+ newValues[oldValues.length] = value;
+ }
+ parameters.put(name, newValues);
+ }
+
+ public String getPath() {
+ String result = super.getPath();
+
+ if(!parameters.isEmpty())
+ result += "?";
+
+ for(Enumeration enum = parameters.keys();enum.hasMoreElements();){
+ String next = (String)enum.nextElement();
+ String[] vals = (String[])parameters.get(next);
+
+ for(int i = 0; i < vals.length;i++){
+ if(vals[i] != null){
+ result += next;
+ result += "=";
+ result += URLEncoder.encode(vals[i]);
+
+ if(i <= vals.length)
+ result += "&";
+ }
+ }
+
+ if(enum.hasMoreElements())
+ result += "&";
+ }
+
+ return result;
+ }
+
+}
+
+
+}}}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]