1. ActionContext
在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话
(Session)的一些信息,甚至需要直接对JavaServlet
Http的请求(HttpServletRequest),响应(HttpServletResponse)操作.
我们需要在Action中取得request请求参数"username"的值:
-
ActionContext context = ActionContext.getContext();
-
Map params = context.getParameters();
-
String username = (String) params.get(
"username"
);
ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器
(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过:
ActionContext context = (ActionContext) actionContext.get();
来获取的.我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线
程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们
ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.
通过ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork.
ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与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页面上下文
如何从ServletActionContext里取得Servlet的相关对象:
<1>取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext联系
ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问Servlet的相关对象.
注意:在使用ActionContext时有一点要注意: 不要在Action的构造函数里使用ActionContext.getContext(),
因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null;同样,HttpServletRequest req = ServletActionContext.getRequest()也不要放在构造函数中,也不要直接将req作为类变量给其赋值。
至
于原因,我想是因为前面讲到的static ThreadLocal actionContext = new
ActionContextThreadLocal(),从这里我们可以看出ActionContext是线程安全的,而
ServletActionContext继承自ActionContext,所以ServletActionContext也线程安全,线程安全要求每
个线程都独立进行,所以req的创建也要求独立进行,所以ServletActionContext.getRequest()这句话不要放在构造函数
中,也不要直接放在类中,而应该放在每个具体的方法体中(eg:login()、queryAll()、insert()等),这样才能保证每次产生对象
时独立的建立了一个req。
4. struts2中获得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对象。
-
ActionContext ctx = ActionContext.getContext();
-
-
ctx.put(
"liuwei"
,
"andy"
);
-
Map session = ctx.getSession();
-
-
HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
-
HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
细心的朋友可以发现这里的session是个Map对象, 在Struts2中底层的session都被封装成了Map类型. 我们可以直接操作这个Map对象进行对session的写入和读取操作, 而不用去直接操作HttpSession对象.
方法二:使用org.apache.struts2.ServletActionContext类
-
public
class
UserAction extends ActionSupport {
-
-
-
-
private
HttpServletRequest req;
-
-
-
public
String login() {
-
req = ServletActionContext.getRequest();
-
user =
new
User();
-
user.setUid(uid);
-
user.setPassword(password);
-
if
(userDAO.isLogin(user)) {
-
req.getSession().setAttribute(
"user"
, user);
-
return
SUCCESS;
-
}
-
return
LOGIN;
-
}
-
public
String queryAll() {
-
req = ServletActionContext.getRequest();
-
uList = userDAO.queryAll();
-
req.getSession().setAttribute(
"uList"
, uList);
-
return
SUCCESS;
-
}
-
-
-
}
(2)IoC方式(即使用Struts2 Aware拦截器)
要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。
-
public
class
UserAction
extends
ActionSupport
implements
SessionAware, ServletRequestAware, ServletResponseAware {
-
-
private
HttpServletRequest request;
-
private
HttpServletResponse response;
-
-
public
void
setServletRequest(HttpServletRequest request) {
-
this
.request = request;
-
}
-
-
public
void
setServletResponse(HttpServletResponse response) {
-
this
.response = response;
-
}
-
-
public
String execute() {
-
HttpSession session = request.getSession();
-
return
SUCCESS;
-
}
-
}
分享到:
相关推荐
象,如HttpServletRequest、HttpServletResponse,或者需要访问Session和Application等信息,在Struts2框架中,我们可以利用ActionContext和ServletActionContext这两个类。ActionContext是Struts2提供的一个核心上...
在选择使用ActionContext还是ServletActionContext时,优先考虑使用ActionContext,除非确实需要直接操作Servlet API的对象。这是为了保持Action的简洁性和避免对Servlet API的直接依赖,有利于代码的维护和解耦。 ...
本文将详细讨论如何在Struts2的Action中获取JSP页面的参数,以及相关的上下文类ActionContext和ServletActionContext。 1. **ActionContext获取请求参数** 当用户在JSP页面上提交表单时,表单数据会被封装到HTTP...
在Action中选择使用`ActionContext`还是`ServletActionContext`,通常遵循的原则是:如果`ActionContext`能完成所需功能,就尽量不要使用`ServletActionContext`,以减少Action直接与Servlet相关对象的交互,保持...
这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类,具体的方法如下所示。 获得request对象: A . HttpServletRequest request = ServletActionContext....
Struts2 框架提供了多种方式来访问 Servlet API,包括使用 ActionContext 类、实现接口和使用 ServletActionContext 类等。在本文中,我们将详细介绍 Struts2 访问 Servlet API 的几种方法。 一、使用 ...
struts2.0 详细介绍了struts.xml的配置,以及web.xml的配置和ActionContext,ServletActionContext的用法和区别。ActionContext主要用于设置属性,而ServletActionContext主要用来得到属性
ServletActionContext是ActionContext的一个包装类,它提供了更直接的方式获取HttpServletRequest和HttpServletResponse: ```java public class ServletContextAction extends ActionSupport { public String ...
通过`ActionContext`对象,我们可以获取到`ServletActionContext`,进而得到`ServletResponse`实例。 ```java import com.opensymphony.xwork2.ActionSupport; public class MyAction extends ActionSupport { ...
这两种方式都是通过`ActionContext`来获取Session对象,第一种直接通过`getSession()`方法得到,第二种则通过`getContext()`方法后,再根据`ActionContext.SESSION`获取。 ### 应用场景示例 1. **用户认证**:在...
通过实现特定的接口或使用`ActionContext`和`ServletActionContext`类,开发者可以轻松地获取`request`、`session`和`response`对象。这些工具极大地简化了Web应用程序的开发过程,提高了代码的可维护性和可扩展性。
2. **ServletActionContext类**:这是ActionContext的一个扩展,提供了更方便的访问Servlet相关对象的方法,如`ServletActionContext.getRequest()`和`ServletActionContext.getResponse()`分别用于获取...
在Action中,可以直接调用`ServletActionContext.getRequest()`、`ServletActionContext.getResponse()`、`ServletActionContext.getRequest().getSession()`等方法来获取这些对象。 #### 示例代码: ```java ...
Action获取Web元素: 1通过ActionContext获取 2通过ServletActionContext获取 3框架注入
总结来说,Struts2.1提供了多种方式来获取和操作Servlet对象,包括直接通过`ActionContext`、使用`ServletActionContext`以及通过IoC和`Aware`接口。这些方法各有优势,可以根据实际需求选择合适的方式。了解并熟练...
ServletActionContext是ActionContext的一个实现,它扩展了ActionContext并增加了对Servlet API的直接访问。通过ServletActionContext,你可以获取或设置Servlet请求和响应对象,以及执行其他与Servlet相关的操作。 ...
ActionContext context = ActionContext.getContext(); HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST); HttpServletResponse response = ...