您的位置:首页 > 编程语言 > Java开发

struts1及struts2中action和jsp之间数据和参数传输传递

2009-08-19 17:49 453 查看
1,jsp中提交数据就不用说了,会自动执行对应的setter,然后action就可以再execute中执行相应的操作了,比如save等等。

2,action处理完数据怎么在jsp中显示呢?

如果是在struts1中的话,execute方法是这样的:

ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

LoginForm form=(LoginForm)form;

String username=form.getUsername;

HttpSession session=request.getSession();

session.setAttribute("username",username)

}

然后在jsp中直接session.getAttribute("username")就ok了。

那么在struts2中是怎么做的呢,看这里:

(1)Map request=(Map)ActionContext.getContext().get("request");

request.put("booklist",this.service.findAll());

这样就把所有数据保存到request中了。

然后在jsp中,这样:

<table>

<tr>

<td>编号</td>

<td>书名</td>

<td>作者</td>

<td>出版社</td>

<td>价格</td>

</tr>

<iterator value="#request.booklist" id="bl">

<tr>

<td>${id}</td>

<td>${name}</td>

<td>${auth}</td>

<td>${press}</td>

<td>${price}</td>

</tr>

</iterator>

</table>

(2).

action中:HttpServletRequest request =ServletActionContext.getRequest();

// HttpSession session=request.getSession();

request.setAttribute("booklist",this.service.findAll());

//session.setAttribute("booklist",this.service.findAll());

自己决定用request还是session。

此方法和struts1中思想是一样的,只是需要建一个HttpServletRequest对象而已,struts1中函数参数已经有了。

struts2中是不能直接把HttpServletRequest对象写在参数中的,因为execute方法是继承自ActionSupport只能重写无参方法,否则会出错。

jsp中就和(1)中一样了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: