在傳統的Web開發中,常常會用到Servlet API中的HttpServletRequest、HttpSession和ServletContext。Struts 2框架讓咱們能夠直接訪問和設置action及模型對象的數據,這下降了對HttpServletRequest對象的使用需求,但在某些應用中,咱們可能會須要在action中去訪問HttpServletRequest對象以及其餘兩種對象,例如,用戶登陸成功後,咱們應該將用戶信息保存到Session中。Struts 2提供了多種方式來訪問上述的三種對象,歸結起來,能夠劃分爲兩大類:與Servlet API解耦的訪問方式和與Servlet API耦合的訪問方式.java
方式1、與Servlet API解耦的訪問方式
爲了不與Servlet API耦合在一塊兒,方便Action類作單元測試,Struts HttpServletRequest、HttpSession和ServletContext進行了封裝,構造了三個Map對象來替代這三種對象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext對應的Map對象來保存和讀取數據。
要獲取這三個Map對象,可使用com.opensymphony.xwork2.ActionContext類。
ActionContext是action執行的上下文,在ActionContext中保存了action執行所需的一組對象,包括parameters、request、session、application和locale等。ActionContext類定義了以下方法,用於獲取HttpServletRequest、HttpSession和ServletContext對應的Map對象。
? public Object get(Object key)ActionContext類沒有提供相似getRequest()這樣的方法來獲取封裝了HttpServletRequest的Map對象。要獲得請求Map對象,你須要爲get()方法傳遞參數「request」。
? public Map getSession()
獲取封裝了HttpSession的Map對象。
? public Map getApplication()
獲取封裝了ServletContext的Map對象。程序員
private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @SuppressWarnings("unchecked") public String execute() { ActionContext context = ActionContext.getContext(); Map request = (Map) context.get("request"); Map session = context.getSession(); Map application = context.getApplication(); // 在請求中放置歡迎信息。 request.put("greeting", "歡迎您來到程序員之家"); // 在session中保存password屬性 session.put("password", password); // 統計用戶訪問量,在application中保存用戶訪問量數據 Integer count = (Integer) application.get("counter"); if (null == count) count = 1; else count++; application.put("counter", count); return "success"; }
方式二。與Servlet API耦合的訪問方式
直接訪問Servlet API將使你的Action與Servlet環境耦合在一塊兒,咱們知道對於HttpServletRequest、HttpServletResponse和ServletContext這些對象,它們都是由Servlet容器來構造的,與這些對象綁定在一塊兒,測試時就須要有Servlet容器,不便於Action的單元測試。但有時候,咱們又確實須要直接訪問這些對象,那麼固然是以完成任務需求爲主。
要直接獲取HttpServletRequest和ServletContext對象,可使用org.apache.struts2. ServletActionContext類,該類是ActionContext的子類,在這個類中定義下面兩個靜態方法:
? public static HttpServletRequest getRequest()
獲得HttpServletRequest對象。
? public static ServletContext getServletContext()
獲得ServletContext對象。
此外,ServletActionContext類還給出了獲取HttpServletResponse對象的方法,以下:
? public static HttpServletResponse getResponse()
注意:ServletActionContext類並無給出直接獲得HttpSession對象的方法,HttpSession對象能夠經過HttpServletRequest對象來獲得。
除了上述的方法調用獲得HttpServletRequest和ServletContext對象外,還能夠調用ActionContext對象的get()方法,傳遞ServletActionContext.HTTP_REQUEST和ServletActionContext.SERVLET_CONTEXT鍵值來獲得HttpServletRequest和ServletContext對象,以下所示:
? ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
獲得與ServletActionContext.HTTP_REQUEST鍵值綁定的HttpServletRequest對象。
? ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
獲得與ServletActionContext.SERVLET_CONTEXT鍵值綁定的ServletContext對象。
一樣的,也能夠向ActionContext的get()方法傳遞ServletActionContext.HTTP_ RESPONSE鍵值來獲得HttpServletResponse對象,以下:
? ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
建議讀者採用第一種方式來獲取HttpServletRequest和ServletContext對象,這樣簡單而又清晰。
經過ServletActionContext來獲取HttpServletRequest和ServletContext對象的LoginAction:apache
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class LoginAction { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); ServletContext context = ServletActionContext.getServletContext(); // 在請求中放置歡迎信息。 request.setAttribute("greeting", "歡迎您來到程序員之家"); // 在session中保存user對象 session.setAttribute("password", password); // 統計用戶訪問量,在application中保存用戶訪問量數據 Integer count = (Integer) context.getAttribute("counter"); if (null == count) count = 1; else count++; context.setAttribute("counter", count); return "success"; } }