struts2中 ServletActionContext与ActionContext区别
1. ActionContext
在Struts2開發(fā)中,除了將請求參數(shù)自動設(shè)置到Action的字段中,我們往往也需要在Action里直接獲取請求(Request)或會話(Session)的一些信息,甚至需要直接對JavaServlet Http的請求(HttpServletRequest),響應(yīng)(HttpServletResponse)操作. 我們需要在Action中取得request請求參數(shù)"username"的值:
- ActionContext?context?=?ActionContext.getContext();
- Map?params?=?context.getParameters();
- String?username?=?(String)?params.get("username");
ActionContext(com.opensymphony.xwork.ActionContext)是Action執(zhí)行時的上下文,上下文可以看作是一個容器(其實我們這里的容器就是一個Map而已),它存放的是Action在執(zhí)行時需要用到的對象. 一般情況, 我們的ActionContext都是通過:?ActionContext context = (ActionContext) actionContext.get();來獲取的.我們再來看看這里的actionContext對象的創(chuàng)建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是實現(xiàn)ThreadLocal的一個內(nèi)部類.ThreadLocal可以命名為"線程局部變量",它為每一個使用該變量的線程都提供一個變量值的副本,使每一個線程都可以獨立地改變自己的副本,而不會和其它線程的副本沖突.這樣,我們ActionContext里的屬性只會在對應(yīng)的當(dāng)前請求線程中可見,從而保證它是線程安全的.
通過ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),這個類直接繼承了我們上面介紹的ActionContext,它提供了直接與Servlet相關(guān)對象訪問的功能,它可以取得的對象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet請求對象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相應(yīng)對象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置對象
(5)javax.servlet.jsp.PageContext : Http頁面上下文
如何從ServletActionContext里取得Servlet的相關(guān)對象:
<1>取得HttpServletRequest對象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession對象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext聯(lián)系
ServletActionContext和ActionContext有著一些重復(fù)的功能,在我們的Action中,該如何去抉擇呢?我們遵循的原則是:如果ActionContext能夠?qū)崿F(xiàn)我們的功能,那最好就不要使用ServletActionContext,讓我們的Action盡量不要直接去訪問Servlet的相關(guān)對象.
注意:在使用ActionContext時有一點要注意:?不要在Action的構(gòu)造函數(shù)里使用ActionContext.getContext(),因為這個時候ActionContext里的一些值也許沒有設(shè)置,這時通過ActionContext取得的值也許是null;同樣,HttpServletRequest req = ServletActionContext.getRequest()也不要放在構(gòu)造函數(shù)中,也不要直接將req作為類變量給其賦值。至于原因,我想是因為前面講到的static ThreadLocal actionContext = new ActionContextThreadLocal(),從這里我們可以看出ActionContext是線程安全的,而ServletActionContext繼承自ActionContext,所以ServletActionContext也線程安全,線程安全要求每個線程都獨立進(jìn)行,所以req的創(chuàng)建也要求獨立進(jìn)行,所以ServletActionContext.getRequest()這句話不要放在構(gòu)造函數(shù)中,也不要直接放在類中,而應(yīng)該放在每個具體的方法體中(eg:login()、queryAll()、insert()等),這樣才能保證每次產(chǎn)生對象時獨立的建立了一個req。
4. struts2中獲得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext類,通過它的靜態(tài)方法getContext()獲取當(dāng)前Action的上下文對象。
?
?
[java]?view plain?copy- ????<pre?name="code"?class="java">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);</pre><br>??
- <p></p>??
- <pre></pre>??
- <br>??
- <br>??
- <p></p>??
- <p><span?style="font-size:16px">細(xì)心的朋友可以發(fā)現(xiàn)這里的session是個Map對象,?在Struts2中底層的session都被封裝成了Map類型.??
- <br>??
- </span></p>??
- <p><span?style="font-size:16px">我們可以直接操作這個Map對象進(jìn)行對session的寫入和讀取操作,?而不用去直接操作HttpSession對象.</span></p>??
- <p><span?style="font-size:16px">方法二:使用org.apache.struts2.ServletActionContext類</span></p>??
- <p><strong></strong></p><pre?name="code"?class="java">????public?class?UserAction?extends?ActionSupport?{??
- ??????????
- ????????//其他代碼片段??
- ??????????
- ????????private?HttpServletRequest?req;???
- ????//??private?HttpServletRequest?req?=?ServletActionContext.getRequest();?這條語句放在這個位置是錯誤的,同樣把這條語句放在構(gòu)造方法中也是錯誤的。??
- ????????public?String?login()?{??
- ????????????req?=?ServletActionContext.getRequest();?//req的獲得必須在具體的方法中實現(xiàn)??
- ????????????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的獲得必須在具體的方法中實現(xiàn)??
- ????????????uList?=?userDAO.queryAll();??
- ????????????req.getSession().setAttribute("uList",?uList);??
- ????????????return?SUCCESS;??
- ????????}??
- ??????????
- ????????//其他代碼片段??
- ????}</pre><br>??
- <br>??
- <p></p>??
- <p><span?style="font-size:16px"><strong>(2)IoC方式(即使用Struts2?Aware攔截器)</strong></span></p>??
- <p>要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個對象的意愿,通過實現(xiàn)相應(yīng)的接口做到這點。</p>??
- <pre?name="code"?class="java">????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;???
- ????????}??
- ????}</pre><br>??
- <br>??
- <link?rel="stylesheet"?href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=2.0"> ?
轉(zhuǎn)載于:https://www.cnblogs.com/shixf/p/7789082.html
總結(jié)
以上是生活随笔為你收集整理的struts2中 ServletActionContext与ActionContext区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你爱的那么认真是什么歌啊?
- 下一篇: 台式acer电脑多少钱