ActionForward-动态转发

ActionForward的作用就是从一个页面链接到另一个页面,最简单的应用就是mapping.findForward("success")。

属性:name-逻辑名称;path-URL;redirect-当设置为true是,重定向,默认为false;className(可选)-默认为ActionForward。

一般的调用过程: 当ActionServlet调用Action时,Action 把Mapping的findForward()传递给它。findForward()先在局部转发列表中寻找key,如果没找到,再从全局转发列表中寻 找。如果都没找到,此方法将返回null,浏览器报错。

在<html:link>标记中的使用:
在配置文件中定义一个forward:
 <forward name="article" path="/do/article?dispatch=view"/>

<html:link>中:
 <html:link forward="article" paramName="articleForm" paramProperty="articleKey" paramId="key">
   News from the edge
 </html:link>

运行结果:
 <a href="http://localhost/artimus/do/article?dispatch=view&key=17">News from the edge</a>
注意这边,如果已经存在一个参数,标签会很聪明的帮你添加一个连接符号,之后再添加参数。
 
添加自己要转发的路径:
 public ActionForward execute( ActionMapping mapping,ActionForm form, HttpServletRequest request,
                                              HttpServletResponse response ) throws Exception{
        ActionForward forward = new ActionForward();
        String loginid = ((LoginForm)form).getLoginid();
        String password = ((LoginForm)form).getPassword();
        if(...){
             ......  // business code
            forward.setPath("welcome.jsp");
        }else{
            ActionMessages errors = new ActionMessages();
            .......
            forward = mapping.findForward("Failure");
        }
        return forward;
    }
 
在Action 类中添加参数:
 ActionForward forward = mapping.findForward("article");
 StringBuffer path = new StringBuffer(forward.getPath());
 boolean isQuery = (path.indexOf("?")>=0);
 if (isQuery)
 path.append("&amp;dispatch=view");
 else
 path.append("?dispatch=view");
 return new ActionForward(path.toString());
 
如果ActionForward 没有设置为重定向,新的参数将被合并到当前request的参数中。如果新的参数的名称和原参数名称相同,在新的转发期内将使用新的参数值。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值