首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2标签 应用pageContext.getAttribute("myAtt")返回null

2012-09-29 
Struts2标签 使用pageContext.getAttribute(myAtt)返回null在Struts2 API帮助文档中IteratorGeneratorTa

Struts2标签 使用pageContext.getAttribute("myAtt")返回null
在Struts2 API帮助文档中IteratorGeneratorTag类的说明找到如下例子
Example Three:
Generate an iterator with var attribute


This generates an iterator and put it in the PageContext under the key as specified by the var attribute.

后copy上面整段代码,执行后,页面显示错误信息,报java.lang.NullPointerException。

改代码,如下:

再次执行,结果显示:OH,My God!
从结果可知,pageContext.getAttribute("myAtt");获取不到对象,把它改为pageContext.getAttribute("myAtt",PageContext.REQUEST_SCOPE);即可,结果显示:
aaa
bbb
ccc
ddd

PS:
1.在Debug的过程中也未发现pageContext的attributes下有myAtt的key,而在value stack中可以找到。
2.Struts2中的request是org.apache.struts2.dispatcher.StrutsRequestWrapper。
Method Summary
Object getAttribute(String s)
          Gets the object, looking in the value stack if not found

上面不知官方的例子为何不行?如果你知道,请不吝赐教!谢谢!
有空的话,欢迎到红番薯逛逛

不过我好好思考了下,就像JSP中一样,你若是generate一个Collection或者Iterator,存放用户的购物车,总要把这些信息传出去的,服务器跳转的情况下在下一个页面能访问到。而session域的话,有点不合情理,不安全,life-cycle也太长,application就更不可能了。
因此将var属性定义的集合存入request域中应该是理想的选择。

最后,谢谢你。

热点排行