双重人格电影介绍:在Struts开发中,给ActionForward动态添加参数

来源:百度文库 编辑:偶看新闻 时间:2024/04/30 04:54:48

ActionForward是Struts框架的Action中使用的一个对象。它指向一个具体的path。但是这个path一般是写死在struts-config.xml中的,那么怎么给ActionForward对象动态添加参数呢?

 

这里总结了几种方法。

 

1. 最直接的方式:使用request或者session传递。

request.setAttribute()

request.getSession.setAttribute()

 

例:

request.setAttribute(name, value);
return mapping.findForward("success");

 

 

2. 直接使用ActionForward构建一个新的包含参数的path。

例:

String userId = request.getParameter("user_Id");
ActionForward af = new ActionForward("/xxx/xxx/xxx.do?userId=" + userId);
return af;

 

 

3. 先使用mapping.findForward()的方式获得ActionForward对象后, 然后构建一个新的ActionForwad对象并添加参数

 

例:

String userId = request.getParameter("user_Id");ActionForward af = mapping.findForward("edit");
ActionForward af2 = new ActionForward(af.getPath + "?userId=" + userId);
return af;

当然,这只是一个简单的例子来说明如何添加参数的,真正使用的时候,name为edit的path可能本身已经包含?了。这个时候就要先判断是否含有?,如果有的话,就直接在后面添加"&userId=xx",如果没有的话,就如例子中所示,直接添加。

 

总结:

以上三种方式,建议使用第一种。不建议使用第二种。

因为我们之所以使用struts-config.xml文件来配置相关的url,就是不想把这个东东硬编码到class文件中,因为在开发过程中,处于一些模块化的考虑,url还是比较容易变化的。第二种就是一种恶劣的使用方式。第三种方式也还可以使用。