ActionContext与ServletActionContext的区别及获取request、session等对象
来源:互联网 发布:矩阵计算 线性代数 编辑:程序博客网 时间:2024/05/17 07:01
我们知道struts2接受客户端请求,在Action中进行处理后,将视图结果返回。struts2容器自身不依赖于web容器,不用和servlet对象中的请求(request)、响应(response)进行关联,对于请求的参数,通过paramerInterceptor将参数封装在Action中,然后通过调用get、set方法将参数值设置进Action之中。如果仅仅获取参数,可能有时候满足不了开发的需求,有时我们要获取request或者response中的信息,要对其进行设置、处理。
一、ActionContext
用ActionContext得到Map类型的Request、Session、Application。
例子:
得到session、request有点区别,得到request用的是get("reqeust"),得到session用的是getSession()
也可以直接对Java Servlet Http的请求(HttpServletRequest)、响应(HttpServletResponse)操作,和上面的例子有点区别,注意区别。
ActionContext ctx = ActionContext.getContext();
用法就和Servlet中的request、response用法一样
二、ServletActionContext
它继承ActionContext,所以ServletActionContext也可以得到HttpServetRequest、HttpServletResponse,,它也提供了直接与Servlet相关对象访问的功能,它可以取得的对象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相应对象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置对象
(5)javax.servlet.jsp.PageContext : Http页面上下文
如何获取HttpRequest、HttpResponse
例子
然后就可以用request.setAttribute("name", value)方法了。
总结:不难看出,两者之间还是存在很多共同功能,那么我们还是根据自己的需求进行选择,能用ActionContext对象满足就尽量使用ActionContext,避免让我们直接去访问Servlet对象。另外,不要在Action还没实例化的时候去通过ActionContext调用方法,因为Action实例在ActionContext实例之前创建,ActionContext中一些值还没有设置,会返回null。
- ActionContext与ServletActionContext的区别及获取request、session等对象
- ActionContext与ServletActionContext的区别及获取request、session等对象
- ActionContext与ServletActionContext的区别及获取request、session等对象
- ActionContext与ServletActionContext的区别及获取request、session等对象
- ActionContext 与 ServletActionContext获取Session的异同
- ActionContext与ServletActionContext的区别及认识
- struts2中ServletActionContext与ActionContext区别及获取Servlet对象方法
- ActionContext与ServletActionContext的区别
- ActionContext与ServletActionContext区别
- ActionContext类与ServletActionContext类操作request域、session域内的属性
- ServletContext、ActionContext与ServletActionContext的区别
- ActionContext获取request、session和application对象
- ActionContext和ServletActionContext的区别
- ActionContext和ServletActionContext的区别
- actionContext和ServletActionContext的区别:
- actionContext和ServletActionContext的区别:
- Struts2的 ActionContext && ServletActionContext 区别
- Actioncontext和ServletActioncontext的区别
- There was an unexpected error (type=Not Found, status=404). /kind/WEB-INF/page//kindEditor.jsp
- JVM调优之jstack找出最耗cpu的线程并定位代码
- 爬虫入门:爬取CSDN每天都的访问量并发送邮件到邮箱
- 企业网站后台功能
- Java键盘录入对象
- ActionContext与ServletActionContext的区别及获取request、session等对象
- Ubuntu upgrade错误的处理方法
- 数字序列
- 设计模式(Design Patterns)--java23种
- Java键盘录入三个数比较大小
- turtlebot3 Slam+nvigation仿真 ROS-lunar
- Java录入两个数据,比较这两个数是否相等
- java性能调优的一些方法
- Linux下tar解压命令