Struts的扩展:添加可以传参数的ActionForward(一)

一。添加一个新的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;
    }
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值