Struts2 - Adding Properties to the pageContext

Struts2 - Adding Properties to the pageContext

In struts2, to access a property inside a JSP that we set in the overridden execute method of Action Interface, we use the following property tag:
<s:property value="results" />
where   s  is an alias for struts-tags tag library and   results  is a Collection (can be anything) that we've populated in   execute  method. Note here that it must be a variable/property in you action handler class with both of its getter and setter methods available. The property tag will call the   getResults()  getter method.
This is clean. But it should be noted that   results  is not available in the   pageContext  of the page being processed currently. So, for example, if you want to use the   results  collection inside a jsp scriptlet by doing something like
pageContext.getAttribute("results")
this will not work. You cannot even say something like
request.getAttribute("results")
Anyways, it's possible and pretty clean too :) All you've to do is to set the   pageContext  attribute by using another struts tag,
<s:set name="pageResults" value="%{results}" />
This is equivalent to
pageContext.setAttribute("pageResults",results)
but the later won't work. The name is what you want to name the   results  in the   pageContext, like a variable name; and the value is its value. So here, Set tag will call   getResults()  and assign the returned value to a pageContext  variable called   pageResults.

That's it :)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值