struts2获取request,session,application4种方式

第一种,依赖容器获取:

                               private Map request; private Map session; private Map application; public ActionTest() { request=(Map)ActionContext.getContext().get("request"); session=ActionContext.getContext().getSession(); application=ActionContext.getContext().getApplication(); }

 

第二种,IoC方式,是通过依赖注入方式获取到对象,需要实现相应的接口为 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; }

 

第三种,获取的是原型:

                               private HttpServletRequest request; private HttpSession session; private ServletContext application; public ActionTest() { request=ServletActionContext.getRequest(); session=request.getSession(); application=session.getServletContext(); }

 

第四种,通过IoC获取原型,需要实现ServletRequestAware,并且要实现setServletRequest方法

                               private HttpServletRequest request; private HttpSession session; private ServletContext application; public void setServletRequest(HttpServletRequest request) { this.request=request; this.session=request.getSession(); this.application=session.getServletContext(); }

你可能感兴趣的:(struts2)