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

structs2怎么获取session

2012-09-16 
structs2如何获取sessionHow do we get access to the sessionYou can obtain the session attributes by

structs2如何获取session
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置入到你需要的对象中。
推荐用第二种。

热点排行