一 通过ActionContext类获取
下面获取的request和session都是map对象
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> request = actionContext.getContextMap();
Map<String, Object> session = actionContext.getSession();
或者还有一种方式
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
HttpSession session = request.getSession();
该类提供了如下几种方法:
Object get(Object key)
等同于调用HttpServletRequest的getAttribute(String name)方法
Map getApplication()
返回一个Map对象,该对象模拟了该应用的ServletContext实例。
static ActionContext getContext()
这是个静态方法 获取系统的ActionContext实例
Map getParameters()
获取所有的请求参数,类似于调用HttpServletRequest对象的getParameterMap()方法
Map getSession()
返回一个Map对象,该对象模拟了HttpSession 实例
void setApplication(Map application)
传入一个Map实例,将Map实例的key-value转换成application的属性名-值。
void setSession(Map session)
传入一个Map实例,将Map实例的key-value转换成application的属性名-值。
二 通过ServletActionContext类获取
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
该类提供了如下几种方法:
static PageContext getPageContext()
获取PageContext对象。
static HttpServletRequest getRequest()
获取HttpServletRequest对象。
static HttpServletResponse getResponse()
获取HttpServletResponse对象。
static ServletContext getServletContext()
获取ServletContext对象。
三 通过实现相应接口获取
Struts2提供了如下几个接口用于获取Servlet API:
ServletContextAware
实现该接口的Action可以直接访问Web应用的ServletContext实例。
ServletRequestAware
实现该接口的Action可以直接访问Web应用的ServletRequest实例。
ServletResponseAware
实现该接口的Action可以直接访问Web应用的ServletRsponse实例。
SessionAware,RequestAware,ApplicationAware
获取的session、request、application都是map对象。
package cn.iborder.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import cn.iborder.entity.User;
public class UserAction2 extends ActionSupport implements ServletRequestAware{
private User user;
//获取servlet api
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
System.out.println("用户:"+user.getUserName());
System.out.println("密码:"+user.getPassword());
session.setAttribute("userName", user.getUserName());
return "success";
}
public String logout() {
System.out.println(session.getAttribute("userName")+"成功注销");
session.removeAttribute("userName");
return "logout";
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
this.session=request.getSession();
this.application=session.getServletContext();
}
}
或者
package cn.iborder.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import cn.iborder.entity.User;
/**
* Struts2提供了如下几个接口使Action可以直接访问到Servlet API:
* 1:ServletContextAware:实现该接口的Action可以直接访问Web应用的ServletContext实例
* 2:ServletRequestAware:实现该接口的Action可以直接访问Web应用的ServletRequest实例.
* 3:ServletResponseAware:实现该接口的Action可以直接访问Web应用的ServletRsponse实例.
*/
public class UserAction4 extends ActionSupport implements SessionAware,RequestAware,ApplicationAware{
private User user;
//获取servlet api
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
System.out.println(this.getClass().getSimpleName());
System.out.println("用户:"+user.getUserName());
System.out.println("密码:"+user.getPassword());
session.put("userName", user.getUserName());
return "success";
}
public String logout() {
System.out.println(session.get("userName")+"成功注销");
session.remove("userName");
return "logout";
}
@Override
public void setApplication(Map<String, Object> applicate) {
// TODO Auto-generated method stub
this.application = applicate;
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
}