Struts2 HTTP物件傳遞
阿新 • • 發佈:2019-01-27
1、 HTTP物件傳遞:
(1)ActionContext物件獲取常用HTTP物件:
ActionContext ac = ActionContext.getContext(); //上下文物件相當於request範圍
HttpServletRequest request =(HttpServletRequest)ac.get(StrutsStatics.HTTP_REQUEST);
HttpSession session = request.getSession(false);
ServletContext application = session.getServletContext();
ServletContext application = ac.get(StrutsStatics.SERVLET_CONTEXT);
HttpServletRequest request = ServletActionContext.getRequest(); (推薦使用)
HttpServletResponse response =ServletActionContext.getResponse();
(2)獲取Map集合:
ActionContext ac = ActionContext.getContext();
Mapsession = ac.getSession();
Mapsession2 =(Map)ac.get("session");
Mapsession3=(Map)ac.get(ActionContext.SESSION);
(3)獲取值棧物件和引數集合物件:
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
Map paramts = ac.getParameters();
2、 實現介面獲取HTTP物件:
ServletContextAware 實現此介面後,可以取得ServletContext
ServletRequestAware 實現此介面後,可以取得HttpServletRequest
ServletResponseAware 實現此介面後,可以取得HttpServletResponse
SessionAware 實現此介面後,可以取得HttpSession,注意,這裡有點特殊,取得的是一個Map<String,Object> session,攔截器負責將session中儲存的鍵值進行解析,並一一對應.
(1)ActionContext物件獲取常用HTTP物件:
ActionContext ac = ActionContext.getContext(); //上下文物件相當於request範圍
HttpServletRequest request =(HttpServletRequest)ac.get(StrutsStatics.HTTP_REQUEST);
HttpSession session = request.getSession(false);
ServletContext application = session.getServletContext();
ServletContext application = ac.get(StrutsStatics.SERVLET_CONTEXT);
HttpServletRequest request = ServletActionContext.getRequest(); (推薦使用)
HttpServletResponse response =ServletActionContext.getResponse();
(2)獲取Map集合:
ActionContext ac = ActionContext.getContext();
Mapsession = ac.getSession();
Mapsession2 =(Map)ac.get("session");
Mapsession3=(Map)ac.get(ActionContext.SESSION);
(3)獲取值棧物件和引數集合物件:
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
Map paramts = ac.getParameters();
2、 實現介面獲取HTTP物件:
ServletContextAware 實現此介面後,可以取得ServletContext
ServletRequestAware 實現此介面後,可以取得HttpServletRequest
ServletResponseAware 實現此介面後,可以取得HttpServletResponse
SessionAware 實現此介面後,可以取得HttpSession,注意,這裡有點特殊,取得的是一個Map<String,Object> session,攔截器負責將session中儲存的鍵值進行解析,並一一對應.