剛開(kāi)始學(xué)struts2? 遇到點(diǎn)理論問(wèn)題 ,google下 搞定?
?
申明: 本文章轉(zhuǎn)載自CSDN ?
?
1. ActionContext
在Struts2開(kāi)發(fā)中,除了將請(qǐng)求參數(shù)自動(dòng)設(shè)置到Action的字段中,我們往往也需要在Action里直接獲取請(qǐng)求(Request)或會(huì)話(huà)(Session)的一些信息,甚至需要直接對(duì)JavaServlet Http的請(qǐng)求(HttpServletRequest),響應(yīng)(HttpServletResponse)操作. 我們需要在Action中取得request請(qǐng)求參數(shù)"username"的值:
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String username = (String) params.get("username");
ActionContext(com.opensymphony.xwork.ActionContext)是Action執(zhí)行時(shí)的上下文,上下文可以看作是一個(gè)容器(其實(shí)我們這里的容器就是一個(gè)Map而已),它存放的是Action在執(zhí)行時(shí)需要用到的對(duì)象. 一般情況, 我們的ActionContext都是通過(guò): ActionContext context = (ActionContext) actionContext.get();來(lái)獲取的.我們?cè)賮?lái)看看這里的actionContext對(duì)象的創(chuàng)建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是實(shí)現(xiàn)ThreadLocal的一個(gè)內(nèi)部類(lèi).ThreadLocal可以命名為"線(xiàn)程局部變量",它為每一個(gè)使用該變量的線(xiàn)程都提供一個(gè)變量值的副本,使每一個(gè)線(xiàn)程都可以獨(dú)立地改變自己的副本,而不會(huì)和其它線(xiàn)程的副本沖突.這樣,我們ActionContext里的屬性只會(huì)在對(duì)應(yīng)的當(dāng)前請(qǐng)求線(xiàn)程中可見(jiàn),從而保證它是線(xiàn)程安全的.
通過(guò)ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),這個(gè)類(lèi)直接繼承了我們上面介紹的ActionContext,它提供了直接與Servlet相關(guān)對(duì)象訪問(wèn)的功能,它可以取得的對(duì)象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet請(qǐng)求對(duì)象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相應(yīng)對(duì)象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置對(duì)象
(5)javax.servlet.jsp.PageContext : Http頁(yè)面上下文
如何從ServletActionContext里取得Servlet的相關(guān)對(duì)象:
<1>取得HttpServletRequest對(duì)象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession對(duì)象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext聯(lián)系
ServletActionContext和ActionContext有著一些重復(fù)的功能,在我們的Action中,該如何去抉擇呢?我們遵循的原則是:如果ActionContext能夠?qū)崿F(xiàn)我們的功能,那最好就不要使用ServletActionContext,讓我們的Action盡量不要直接去訪問(wèn)Servlet的相關(guān)對(duì)象.
注意:在使用ActionContext時(shí)有一點(diǎn)要注意: 不要在Action的構(gòu)造函數(shù)里使用ActionContext.getContext(),因?yàn)檫@個(gè)時(shí)候ActionContext里的一些值也許沒(méi)有設(shè)置,這時(shí)通過(guò)ActionContext取得的值也許是null;同樣,HttpServletRequest req = ServletActionContext.getRequest()也不要放在構(gòu)造函數(shù)中,也不要直接將req作為類(lèi)變量給其賦值。至于原因,我想是因?yàn)榍懊嬷v到的static ThreadLocal actionContext = new ActionContextThreadLocal(),從這里我們可以看出ActionContext是線(xiàn)程安全的,而ServletActionContext繼承自ActionContext,所以ServletActionContext也線(xiàn)程安全,線(xiàn)程安全要求每個(gè)線(xiàn)程都獨(dú)立進(jìn)行,所以req的創(chuàng)建也要求獨(dú)立進(jìn)行,所以ServletActionContext.getRequest()這句話(huà)不要放在構(gòu)造函數(shù)中,也不要直接放在類(lèi)中,而應(yīng)該放在每個(gè)具體的方法體中(eg:login()、queryAll()、insert()等),這樣才能保證每次產(chǎn)生對(duì)象時(shí)獨(dú)立的建立了一個(gè)req。
4. struts2中獲得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext類(lèi),通過(guò)它的靜態(tài)方法getContext()獲取當(dāng)前Action的上下文對(duì)象。
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);
細(xì)心的朋友可以發(fā)現(xiàn)這里的session是個(gè)Map對(duì)象, 在Struts2中底層的session都被封裝成了Map類(lèi)型. 我們可以直接操作這個(gè)Map對(duì)象進(jìn)行對(duì)session的寫(xiě)入和讀取操作, 而不用去直接操作HttpSession對(duì)象.
方法二:使用org.apache.struts2.ServletActionContext類(lèi)
public class UserAction extends ActionSupport {
????
??? //其他代碼片段
????
??? private HttpServletRequest req;?
//? private HttpServletRequest req = ServletActionContext.getRequest(); 這條語(yǔ)句放在這個(gè)位置是錯(cuò)誤的,同樣把這條語(yǔ)句放在構(gòu)造方法中也是錯(cuò)誤的。
??? public String login() {
??????? req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實(shí)現(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的獲得必須在具體的方法中實(shí)現(xiàn)
??????? uList = userDAO.queryAll();
??????? req.getSession().setAttribute("uList", uList);
??????? return SUCCESS;
??? }
????
??? //其他代碼片段
}
(2)IoC方式(即使用Struts2 Aware攔截器)
要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個(gè)對(duì)象的意愿,通過(guò)實(shí)現(xiàn)相應(yīng)的接口做到這點(diǎn)。
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;?
??? }
}
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/tryfirst/archive/2009/05/18/4199015.aspx
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
