且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Struts2中的ActionContext.getContext().getParameters()

更新时间:2022-06-04 16:35:27

Struts2中的action已经脱离的request,是用getXxx()来取提交过来的参数,如果在写程序的时候特定需要自己来取Parameter可以通过以下两种方法实现。第一种用ActionContext类,所有的参数都从这里ActionContext.getContext().getParameters()取他返回的是一个Map类型。

Map param= ActionContext.getContext().getParameters();

若有一个提交过来的username,那么通过param.get("username")可以取值。值得注意的是param.get("username")是一个String数组,Struts就是这样设计的。既然是String数组就需要这样取值:

String value[] = (String[])param.get("username");
String username = "";
for(int i=0;i<value.length;i++)
{
 username +=value[i];
}


第二种方法是直接把request引用进来

ServletActionContext.getRequest().getParameter("username")

 

原帖地址:http://www.jspcn.net/htmlnews/11500549785621702.html