`

在struts2的action中,如何获取session

阅读更多
下面的文字是从struts2官网拷过来的,英文好点的可以看看
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.

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

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

Implement SessionAware
_Preferred_

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.

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.

注意执行顺序:
先由 servlet-config Interceptor 调用 Action的 setParameters方法,传值。


官网地址:
http://struts.apache.org/2.0.14/docs/how-do-we-get-access-to-the-session.html
分享到:
评论

相关推荐

    Struts2的Action中获得request response session几种方法

    Struts2的Action中获得request response session几种方法

    Struts2 的Action使用session的方法

    记录了如何在struts2的action中使用session,方便以后直接引用

    struts1和struts2的区别

    另外,按照惯例,在Struts1.x中只有“execute”方法能调用Action, 但在Struts2中并非必要,任何声明为public String methodName() 方法,都能通过配置来调用Action。 最后,和Struts1.x最大的革命性的不同是,...

    struts1&struts2

    2.Struts2和Struts1.x的差别, 最明显的就是Struts2是一个pull-MVC架构。 这是什么意思呢?从开发者角度看, ... Struts 2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过Getter和Setter获取。

    ActionContext介绍(在Struts2中)

    在Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话 (Session)的一些信息, 甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应...

    struts2传值

    2.加#号的放置获取,放置在制定域中,需要理解 jsp 九大对象四个作用域 a.例如放置在session域或者request域中的数据,必须先指定作用域,然后再指定保存的对象. b.页面struts2标签保存的数据也需要使用#来获取

    jsp中Action使用session方法实例分析

    在Struts2里,如果需要在Action中使用session,可以通过下面两种方式得到  1.通过ActionContext class中的方法getSession得到  2.Action实现org.apache.struts2.interceptor.SessionAware接口的方式来对session...

    深入浅出Struts2(附源码)

    作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...

    Struts2实战总结

    3:在action类中取得request和session对象的方法 Map session = ActionContext.getContext().getSession(); HttpServletRequest request = ServletActionContext.getRequest (); 设置它们的值的方法

    POI导入导出EXCEL文件.(struts 1.x and struts2.x).doc

    struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet....

    JSP Struts配置文件详解

    @nochache:true时,在相应结果中加入特定的头参数:Pragma ,Cache-Control,Expires防止页面被存储在可数浏览器的缓存中,默认为false --> contentType="text/html;charset=UTF-8" locale="true" processorClass=...

    Spring struts文档上传代码

    action: 是struts中action层 主要处理表现层的东西 facade:只是一个门面模式(不起多大的作用)主要是处理耦合性问题 session:处理业务逻辑的 dao:数据控制层 调用过程是:action -->facade-->session-->dao ...

    Struts2注解+JDBC+MySQL小项目应用

    将用户信息保存到Session中。 Map, Object> session = ActionContext.getContext().getSession(); session.put("userName", userName); session.put("passWord", passWord); // 2. taglib标识传递 resultMsg =...

    fullCalendar+fancybox+struts2实现的日程安排效果

    后台服务器端采用struts2实现日程安排信息的保存,这里没有连接的数据库,只是将这些信息保存到session里,主要是为了实现保存日程安排的数据而添加的功能,要改成数据库的话只需要在action调用相应的底层代码就ok了...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    6.3.6 在代码中使用Log4j 6.4 改进Log4j 6.5 小结 第七章 Ant使用指南 7.1 Ant介绍 7.1.1 Ant简介 7.1.2 为什么要使用Ant 7.2 建立Ant的开发环境 7.2.1 下载Ant 7.2.2 配置Ant 7.3 Ant的使用方法 7.3.1 Ant能完成的...

    Struts Spring Hibernate 整合 OpenSessionInView 例子

    2.通过spring注入dao到 action 3.通过 open session in view filter 支持 延迟加载 4.在页面上通过 jstl 很优雅的获取数据 5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。书中...

    jsp+struts权限管理

    import org.apache.struts2.ServletActionContext; import sunyang.functions.domain.Function; import sunyang.functions.service.IFunctionFacade; import sunyang.module.domain.Module; import ...

Global site tag (gtag.js) - Google Analytics