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();
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();