在框架中获取会话session的三种方式

 第一种:耦合性最强方式,获取的原始的三个对象(步骤如下)

//1.获取请求
HttpServletRequest request = ServletActionContext.getRequest();
//2.获取会话
HttpSession session = request.getSession();
//3.获取应用程序
ServletContext application = session.getServletContext();

第二种:解耦合方式 
//1.获取请求
Map<String, Object> request = 
(Map<String, Object>) 
ActionContext.getContext().get("request");
//2.获取会话
Map<String, Object> session =
ActionContext.getContext().getSession();
//3.获取应用程序
Map<String, Object> application =
ActionContext.getContext().getApplication();

第三种:声明属性, 实现对应的接口, 进行自动装配
public class UserAction extends ActionSupport 
implements RequestAware, SessionAware, 
ApplicationAware{

private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;

猜你喜欢

转载自blog.csdn.net/qq_36093146/article/details/53940941
今日推荐