一。添加一个新的ActionForward 扩展,以支持在Redirect时传递参数
package org.apache.struts.action;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A subclass of {@link ActionForward} which is designed for use in redirecting
* requests, with support for adding parameters at runtime.
*
* @author luoyonghong
*
*/
public class ParameterForward extends ActionForward {
private Map parameters = new HashMap();
public void setParameter(String name, Object value) {
parameters.put(name, value);
}
public void setParameter(String name, long value) {
setParameter(name, Long.toString(value));
}
public String getPath() {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
String oldPath = getPath();
StringBuffer sb = new StringBuffer(oldPath);
Iterator keys = parameters.keySet().iterator();
for (int i = 0; keys.hasNext(); i++) {
String name = (String) keys.next();
Object value = parameters.get(name);
if (name != null && value != null) {
if (i == 0 && (oldPath.indexOf("?") < 0)) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(name).append("=").append(value);
}
}
setPath(sb.toString());
return super.getPath();
}
/**
* @param copyMe
*/
public ParameterForward(ActionForward copyMe) {
super(copyMe);
}
}
二。修改原ActionMapping 类,添加一个取得ParameterForward 的方法(如红色所示):
package org.apache.struts.action;
import java.util.ArrayList;
import org.apache.struts.action.ActionForward;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ForwardConfig;
public class ActionMapping extends ActionConfig {
//省略原有的代码
......
/**
* add by redbeans (2002-12-24)
* @param name
* @return
*/
public ParameterForward copyForward(String name) {
return new ParameterForward(findForward(name));
}
}
三。在Action中使用此Forward:
....
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//你自已的操作
....
ParameterForward forward = mapping .copyForward("success");
forward.setParameter("section_id", sectionId);
return forward;
}
...