Struts2 提供了三种方式访问本地Servlet API
1. ActionContext【推荐、非耦合】
2. XxxAware接口【推荐、非耦合】
3. ServletActionContext【不推荐、耦合】
1. ActionContext
ActionContext是Action执行的上下文对象,在ActionContext中保存了Action执行所需要的所有对象,包括parameters,request,session,application等。
方法声明 | 方法描述 |
void put(String key,Object object) | 将key-value键值对存放在ActionContext种。模拟Servlet API种HttpServletRequst种的setAttribute()方法。 |
Object get(String key) | 通过key来查找当前ActionContext中的值 |
static ActionContext getContext() | 获取当前线程的ActionContext对象 |
voide setSession(Map<String,Object> session) | 设置一个Map类型的Session值 |
Map<String Object> getParamters() | 返回一个包含所有HttpServletRequest参数信息的Map对象 |
Map<String,Object> getSession() | 返回一个Map类型类型的HttpSession对象 |
Map<String,Object> getApplication() | 返回一个Application级的Map对象 |
void setApplication(Map<String,Object> application) | 设置Applicaton上下文 |
ActionContext类访问Servlet API的常用方法,要访问Servlet API,可以通过如下方式进行,具体示例代码如下:
ActionContext context = ActionContext.getContext();
context.put("name", "张三");
context.getApplication().put("name", "李四");
context.getSession().put("name", "王五");
2. XxxAware 接口
XxxAware 接口获取的对象分两种,一种Servlet和非Servlet
2.1 非耦合Servlet
- RequestAware 接口
- SessionAware 接口
- ApplicationAware 接口
实现RequestAware,SessionAware,ApplicationAware 三个接口需要重写下面三个方法:
setRequest(Map<String, Object> request)
setSession(Map<String, Object> session)
setApplication(Map<String, Object> application)
这三个方法给我们注入了一个Map 集合,我们只需要把数据存储在对应Map集合中就可以了。
public class UserinfoAction implements RequestAware, SessionAware, ApplicationAware {
Map<String, Object> application;
Map<String, Object> session;
Map<String, Object> request;
public String userLogin() {
request.put("awareRequest", "赵云");
session.put("awareSession", "黄盖");
application.put("awareApplaction", "周瑜");
return "success";
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
${requestScope.awareRequest }<hr/>
${sessionScope.awareSession }<hr/>
${applicationScope.awareApplaction }<hr/>
2.2 耦合方式访问Servlet
Struts2 虽然提供了 RequestAware,SessionAwareApplicationAware 接口来访问 Servlet API。虽然这种方法可以访问Servlet API,但是无法直接获得Servlet API实例。为了在Action中直接访问Servlet API,Struts2还提供了下面三个接口:
- ServletRequestAware:实现该接口的Action可以直接访问Web应用的HttpServletRequest实例。
- ServletResponseAware:实现该接口的Action可以直接访问Web应用的HttpServletResponse实例。
- SessionAware:实现该接口的Action可以直接访问Web应用的HttpSession实例。
- ServletContextAware:实现该接口的Action可以直接访问Web应用的ServletContext实例。
public class UserinfoAction implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware {
HttpServletRequest request;
Map<String, Object> session;
ServletContext context;
HttpServletResponse response;
public String userLogin() {
request.setAttribute("servletResponseAware", "貂蝉");
request.getSession().setAttribute("sessionKey", "吕布");
session.put("sessionMap", "董卓");
context.setAttribute("contextMap", "项羽");
response.setCharacterEncoding("UTF-8");
return "success";
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setServletContext(ServletContext context) {
this.context = context;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
3. ServletActionContext
为了直接访问Servlet API,Struts2框架还提供了ServletActionContext类,该类包含了几个常用的静态方法,具体如下:
- static HttpServletRequest getRequest():获取Web应用的HttpServletRequest对象
- static HttpServletResponse getResponse():获取Web应用的HttpServletResponse对象
- static ServletContext getServletContext():获取Web应用的ServletContext对象
- static PageContext getPageContext():获取Web应用的PageContext对象
3.1 HttpServletRequest
HttpServletRequest request = ServletActionContext.getRequest();
3.2 HttpServletResponse
HttpServletResponse response = ServletActionContext.getResponse();
3.3 ServletContext
ServletContext context = ServletActionContext.getServletContext();
3.4 PageContext
PageContext pageContext = ServletActionContext.getPageContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
request.setAttribute("requesttKey","千年等一回")
session.setAttribute("userName", "赵雅芝");