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)中一样了。
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)中一样了。
相关文章推荐
- struts1及struts2中action和jsp之间数据和参数传输传递
- struts2的action与jsp之间传递参数
- 请求Action的路径、参数设置及Struts2的Action向JSP传输List集合并且将数据显示在页面上
- struts2的action与jsp之间传递参数
- struts2的action与jsp之间传递参数
- struts2的action之间和action向jsp的参数传递
- struts2的action与jsp之间传递参数
- struts2的action与jsp之间传递参数
- Struts中的action传递参数到下个action,jsp时是使用get方法还是set方法
- Struts2 action之间相互跳转传递参数
- Struts2 JSP ACTION 数据传递总结!-->自己总结精辟!
- struts2的Aciton与jsp之间传递参数(总结)
- Struts中Action与视图之间的数据传递方式
- Struts中jsp和action中参数传递总结
- struts2的action之间的参数传递和结果类型(转)
- [struts] struts2中Java类与jsp显示页面之间的值传递
- struts2 两个action之间传递参数
- struts和jsp页面之间传递参数
- struts2 action之间传递参数
- Struts2中Action向JSP传递数据