[size=x-large][b][color=indigo]下面的文字是从struts2官网拷过来的,英文好点的可以看看[/color][/b][/size]
[size=x-large][b]How do we get access to the session[/b][/size]
[size=medium]You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.[/size]
[size=large][b]Ask the ActionContext[/b][/size]
[size=medium]The session attributes are available on the ActionContext instance, which is made available via ThreadLocal.[/size]
[color=green][size=medium]Map attibutes = ActionContext.getContext().getSession();[/size][/color]
[size=large][b]Implement SessionAware[/b][/size]
[b][color=blue] _Preferred_[/color][/b]
[color=indigo][size=medium]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.[/size]
[/color]
[color=green][size=medium]Map parameters = this.getSession();[/size][/color]
[size=medium][color=indigo]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.[/color][/size]
[size=medium][color=indigo]To unit test a SessionAware Action, create your own Map with the pertinent session attributes and call setSession as part of the test's setUp method.[/color][/size]
[size=medium][color=olive]注意执行顺序:
先由 servlet-config Interceptor 调用 Action的 setParameters方法,传值。[/color][/size]
官网地址:
http://struts.apache.org/2.0.14/docs/how-do-we-get-access-to-the-session.html
[size=x-large][b]How do we get access to the session[/b][/size]
[size=medium]You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.[/size]
[size=large][b]Ask the ActionContext[/b][/size]
[size=medium]The session attributes are available on the ActionContext instance, which is made available via ThreadLocal.[/size]
[color=green][size=medium]Map attibutes = ActionContext.getContext().getSession();[/size][/color]
[size=large][b]Implement SessionAware[/b][/size]
[b][color=blue] _Preferred_[/color][/b]
[color=indigo][size=medium]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.[/size]
[/color]
[color=green][size=medium]Map parameters = this.getSession();[/size][/color]
[size=medium][color=indigo]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.[/color][/size]
[size=medium][color=indigo]To unit test a SessionAware Action, create your own Map with the pertinent session attributes and call setSession as part of the test's setUp method.[/color][/size]
[size=medium][color=olive]注意执行顺序:
先由 servlet-config Interceptor 调用 Action的 setParameters方法,传值。[/color][/size]
官网地址:
http://struts.apache.org/2.0.14/docs/how-do-we-get-access-to-the-session.html