structs2如何获取session

本文介绍了在Struts框架中如何通过两种方式获取会话属性:一是直接使用ActionContext来获取,二是通过实现SessionAware接口并在运行时调用getSession方法来获取。这两种方法都可以有效地访问和操作会话中的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How do we get access to the session
You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.

1.Ask the ActionContext
The session attributes are available on the ActionContext instance, which is made available via ThreadLocal.

Map attibutes = ActionContext.getContext().getSession();

2.Implement SessionAware

Ensure that servlet-config Interceptor is included in the Action's stack.
The default stack already includes servlet-config.
Edit the Action so that it implements the SessionAware interface.
The SessionAware interface expects a setSession method. You may wish to include a companion getSession method.
At runtime, call getSession to obtain a Map representing the session attributes.
Any changes made to the session Map are reflected in the actual HttpSessionRequest. You may insert and remove session attributes as needed.
Map parameters = this.getSession();
When the servlet-config Interceptor sees that an Action implements ParameterAware, it passes a Map of the session attributes to the Action's setParameters method. Changes made to the Map are reflected in the runtime HttpSessionRequest.

1.直接使用Map attibutes = ActionContext.getContext().getSession();
2.实现SessionAware接口,然后加一个setSession方法,将session置入到你需要的对象中。
推荐用第二种。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值