ActionForward-动态转发

来源:互联网 发布:php获取淘宝商品详情 编辑:程序博客网 时间:2024/04/29 16:00
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的参数中。如果新的参数的名称和原参数名称相同,在新的转发期内将使用新的参数值。
 
原创粉丝点击