struts2中几种取得request,session等对象的方法
1、ActionContext来得到举例代码以下:
情况1:
ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
情况2:
ActionContext actionContext = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) actionContext
.get(ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession();
ServletContext context = (ServletContext) actionContext
.get(ServletActionContext.SERVLET_CONTEXT);
2、通过实现 RequestAware, SessionAware,ApplicationAware等接口来得到,举例代码如下:
public class LoginAction2 implements Action, RequestAware, SessionAware,
ApplicationAware {
private Map request;
private Map session;
private Map application;
public void setRequest(Map request) {
this.request = request;
}
public void setSession(Map session) {
this.session = session;
}
public void setApplication(Map application) {
this.application = application;
}
}
3、经过ServletActionContext,举例代码以下:
// ServletActionContext获得request
HttpServletRequest request = ServletActionContext.getRequest();
// request获得session
HttpSession session = request.getSession();
// ServletActionContext获得servletContext
ServletContext context = ServletActionContext.getServletContext();