在struts2的action中,如何获取session

[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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts2是一个基于Java的开源Web应用框架,它提供了强大的MVC架构以及丰富的特性。在Struts2,我们可以通过使用登录和注册功能来实现用户身份验证和管理。 首先,我们需要在项目导入Struts2的相关依赖包,并在web.xml配置Struts2过滤器,以便拦截所有的请求。 在实现登录功能时,我们需要创建一个Action类来处理用户登录请求。该类需要实现Action接口,并重写execute()方法。在该方法,我们可以获取用户输入的用户名和密码,然后与数据库存储的用户信息进行比较,以验证用户的身份。如果登录成功,我们可以将用户信息保存到Session,以供其他功能使用。 在实现注册功能时,我们同样需要创建一个Action类来处理用户注册请求。该类需要实现Action接口,并在execute()方法获取用户输入的注册信息。在注册之前,我们需要对用户输入的信息进行验证,例如检查用户名是否已存在、密码是否符合要求等。如果验证通过,我们可以将用户信息保存到数据库,并根据需要进行其他操作。 在前端页面,我们需要设计登录和注册的表单,以便用户输入相关信息。通过使用Struts2的标签库,我们可以方便地将表单与后端Action类进行绑定,实现数据的传递和验证。 最后,我们可以通过配置Struts2Action映射,将登录和注册功能与相应的URL进行关联,使用户可以通过访问这些URL来进行登录和注册操作。 综上所述,通过Struts2框架的登录注册实例,我们可以实现用户身份验证和管理的功能,提高了Web应用的安全性和用户体验。同时,Struts2框架的使用也简化了开发过程,提高了开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值