struts 2中如何通過action手動擷取參數

來源:互聯網
上載者:User

struts2中action手動擷取Session,jsp頁面參數

1. ActionContext

在Struts2開發中,除了將請求參數自動化佈建到Action的欄位中,我們往往也需要在Action裡直接擷取請求(Request)或會話(Session)的一些資訊,

甚至需要直接對JavaServlet Http的請求(HttpServletRequest),響應(HttpServletResponse)操作.

我們需要在Action中取得request請求參數"username"的值:

ActionContext context = ActionContext.getContext();

Map params = context.getParameters();

String username = (String) params.get("username");

在執行時的上下文,上下文可以看作是一個容器(其實我們這裡的容器就是一個Map而已),它存放的是Action在執行時需要用到的對象.

一般情況, 我們的ActionContext都是通過: ActionContext context = (ActionContext) actionContext.get();來擷取的.

我們再來看看這裡的actionContext對象的建立:

static ThreadLocal actionContext = new ActionContextThreadLocal();

ActionContextThreadLocal是實現ThreadLocal的一個內部類.

ThreadLocal可以命名為"線程局部變數",它為每一個使用該變數的線程都提供一個變數值的副本,使每一個線程都可以獨立地改變自己的副本,

而不會和其它線程的副本衝突.這樣,我們ActionContext裡的屬性只會在對應的當前請求線程中可見,從而保證它是安全執行緒的.

通過ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();

2. ServletActionContext

ServletActionContext(com.opensymphony.webwork. ServletActionContext),這個類直接繼承了我們上面介紹的ActionContext,

它提供了直接與Servlet相關對象訪問的功能,它可以取得的對象有:

(1)javax.servlet.http.HttpServletRequest : HTTPservlet請求對象

(2)javax.servlet.http.HttpServletResponse : HTTPservlet相應對象

(3)javax.servlet.ServletContext : Servlet上下文資訊

(4)javax.servlet.ServletConfig : Servlet設定物件

(5)javax.servlet.jsp.PageContext : Http頁面上下文

如何從ServletActionContext裡取得Servlet的相關對象:

<1>取得HttpServletRequest對象: HttpServletRequest request = ServletActionContext. getRequest();

<2>取得HttpSession對象: HttpSession session = ServletActionContext. getRequest().getSession();

3. ServletActionContext和ActionContext聯絡

ServletActionContext和ActionContext有著一些重複的功能,在我們的Action中,該如何去抉擇呢?

我們遵循的原則是:如果ActionContext能夠實現我們的功能,那最好就不要使用ServletActionContext,讓我們的Action盡量不要直接去訪問Servlet的相關對象.

更多精彩內容:http://www.bianceng.cn/Programming/Java/

注意:在使用ActionContext時有一點要注意: 不要在Action的建構函式裡使用ActionContext.getContext(),因為這個時候ActionContext裡的一些值也許沒有設定,

這時通過ActionContext取得的值也許是null;

同樣,HttpServletRequest req = ServletActionContext.getRequest()也不要放在建構函式中,也不要直接將req作為類變數給其賦值。

至於原因,我想是因為前面講到的static ThreadLocal actionContext = new ActionContextThreadLocal(),從這裡我們可以看出ActionContext是安全執行緒的,

而ServletActionContext繼承自ActionContext,所以ServletActionContext也安全執行緒,安全執行緒要求每個線程都獨立進行,所以req的建立也要求獨立進行,

所以ServletActionContext.getRequest()這句話不要放在建構函式中,也不要直接放在類中,

而應該放在每個具體的方法體中(eg:login()、queryAll()、insert()等),這樣才能保證每次產生對象時獨立的建立了一個req。

4. struts2中獲得request、response和session

(1)非IoC方式

方法一:使用org.apache.struts2.ActionContext類,通過它的靜態方法getContext()擷取當前Action的內容物件。

ActionContext ctx = ActionContext.getContext();

ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");

Map session = ctx.getSession(); //session

HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);

細心的朋友可以發現這裡的session是個Map對象, 在Struts2中底層的session都被封裝成了Map類型.

我們可以直接操作這個Map對象進行對session的寫入和讀取操作, 而不用去直接操作HttpSession對象.

方法二:使用org.apache.struts2.ServletActionContext類

public class UserAction extends ActionSupport {                //其他程式碼片段                private HttpServletRequest req;      // private HttpServletRequest req = ServletActionContext.getRequest(); 這條語句放在這個位置是錯誤的,同樣把這條語句放在構造方法中也是錯誤的。            public String login() {          req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實現          user = new User();          user.setUid(uid);          user.setPassword(password);          if (userDAO.isLogin(user)) {              req.getSession().setAttribute("user", user);              return SUCCESS;          }          return LOGIN;      }     public String queryAll() {          req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實現          uList = userDAO.queryAll();          req.getSession().setAttribute("uList", uList);          return SUCCESS;      }                //其他程式碼片段  }

(2)IoC方式(即使用Struts2 Aware攔截器)

要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個對象的意願,通過實現相應的介面做到這點。

public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {            private HttpServletRequest request;      private HttpServletResponse response;            public void setServletRequest(HttpServletRequest request) {          this.request = request;      }            public void setServletResponse(HttpServletResponse response) {          this.response = response;      }            public String execute() {          HttpSession session = request.getSession();          return SUCCESS;      }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.