关于ActionContext.getContext().getSession()返回null

最近学习webwork,写代码的过程中遇到了ActionContext.getContext().getSession()结果返回null的问题,在网上搜索了一些网页,总结如下:

 

1.xwork(1.X-2.05)-ActionContext

这是最开始出现null的代码,其实这段代码并没有什么问题,主要的是xwork的版本特性问题:

在xwork2.05之前使用这段代码是没有错误的,但在xwork2.1.2之后中就会出现null错误了,那么应该怎么办呢?如下

 

2.xwork2.X-ActionContext

在网上看了个英文文档http://osdir.com/ml/user-struts.apache.org/2009-08/msg00028.html,里面有这样一段:

But if you really need to set up the ActionContext you
> have to create one first and the "activate" it. If you look at the
> xwork source code for 2.0.5 you can see that the ActionContext is
> lazily created for you when getContext() is invoked, whereas in 2.1.2,
> the context is no longer being created if it does not exist.

稍微看一下就明白了,在xwork2.05中,ActionContext是在getContext()方法被调用是被自动延迟创建的,但在2.1.2中,就必须自己去创建了;

 

3.当然,还有一种选择就是使用ServletActionContext:

不过ServletActionContext和ActionContext还是有区别的,如何选择就看自己的需要了

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值