ActionForward动态转发

本文详细介绍了ActionForward组件在Struts框架中的作用及应用方法,包括如何从一个页面跳转到另一个页面,设置转发路径以及在Action类中添加参数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

在<html:link>标记中的使用:
在配置文件中定义一个forward:
[code]
<forward name="article" path="/do/article?dispatch=view"/>
[/code]
<html:link>中:
[code]
<html:link forward="article" paramName="articleForm" paramProperty="articleKey" paramId="key">News from the edge</html:link>
[/code]
运行结果:
[code]<a href="http://localhost/artimus/do/article?dispatch=view&key=17">News from the edge</a>
[/code]
注意这边,如果已经存在一个参数,标签会很聪明的帮你添加一个连接符号,之后再添加参数。
添加自己要转发的路径:
[code]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;
}[/code]
在Action 类中添加参数:
[code]
ActionForward forward = mapping.findForward("article");
StringBuffer path = new StringBuffer(forward.getPath());
boolean isQuery = (path.indexOf("?")>=0);
if (isQuery)
path.append("&dispatch=view");
else
path.append("?dispatch=view");
return new ActionForward(path.toString());
[/code]
如果ActionForward 没有设置为重定向,新的参数将被合并到当前request的参数中。如果新的参数的名称和原参数名称相同,在新的转发期内将使用新的参数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值