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

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)中一样了。

==================================

本项目中maintenanceCode为要传到值

(1)

MaintenanceAction

        Map<String, String> request=(Map<String, String>) ActionContext.getContext().get("request");
        request.put("maintenanceCode", maintenanceCode);

succese_01.jsp

  <h1>注册号:  ${maintenanceCode } </h1>

(2)更简单的

        ActionContext.getContext().getSession().put("maintenanceCode", maintenanceCode);

succese_01.jsp

  <h1>注册号:  ${maintenanceCode } </h1>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值