小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

ActionContext和ServletActionContext小結(jié) - 挨踢民工 - 博客園

 小扶 2011-04-05

ActionContext和ServletActionContext小結(jié)

1. ActionContext

在Struts2開發(fā)中,除了將請(qǐng)求參數(shù)自動(dòng)設(shè)置到Action的字段中,我們往往也需要在Action里直接獲取請(qǐng)求(Request)或會(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都是通過: ActionContext context = (ActionContext) actionContext.get();來獲取的.我們?cè)賮砜纯催@里的actionContext對(duì)象的創(chuàng)建:

static ThreadLocal actionContext = new ActionContextThreadLocal();

ActionContextThreadLocal是實(shí)現(xiàn)ThreadLocal的一個(gè)內(nèi)部類.ThreadLocal可以命名為"線程局部變 量",它為每一個(gè)使用該變量的線程都提供一個(gè)變量值的副本,使每一個(gè)線程都可以獨(dú)立地改變自己的副本,而不會(huì)和其它線程的副本沖突.這樣,我們 ActionContext里的屬性只會(huì)在對(duì)應(yīng)的當(dāng)前請(qǐng)求線程中可見,從而保證它是線程安全的.

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

  2. ServletActionContext

ServletActionContext(com.opensymphony.webwork. ServletActionContext),這個(gè)類直接繼承了我們上面介紹的ActionContext,它提供了直接與Servlet相關(guān)對(duì)象訪問的 功能,它可以取得的對(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頁面上下文

如何從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盡量不要 直接去訪問Servlet的相關(guān)對(duì)象.

注意:在使用ActionContext時(shí)有一點(diǎn)要注意: 不要在Action的構(gòu)造函數(shù)里使用ActionContext.getContext(),因?yàn)檫@個(gè)時(shí)候ActionContext里的一些值也許沒有 設(shè)置,這時(shí)通過ActionContext取得的值也許是null;同樣,HttpServletRequest req = ServletActionContext.getRequest()也不要放在構(gòu)造函數(shù)中,也不要直接將req作為類變量給其賦值。至于原因,我想是因 為前面講到的static ThreadLocal actionContext = new ActionContextThreadLocal(),從這里我們可以看出ActionContext是線程安全的,而 ServletActionContext繼承自ActionContext,所以ServletActionContext也線程安全,線程安全要求每 個(gè)線程都獨(dú)立進(jìn)行,所以req的創(chuàng)建也要求獨(dú)立進(jìn)行,所以ServletActionContext.getRequest()這句話不要放在構(gòu)造函數(shù) 中,也不要直接放在類中,而應(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類,通過它的靜態(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類型. 我們可以直接操作這個(gè)Map對(duì)象進(jìn)行對(duì)session的寫入和讀取操作, 而不用去直接操作HttpSession對(duì)象.

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

public class UserAction extends ActionSupport {
    
    //其他代碼片段
    
    private HttpServletRequest req;
// private HttpServletRequest req = ServletActionContext.getRequest(); 這條語句放在這個(gè)位置是錯(cuò)誤的,同樣把這條語句放在構(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ì)象的意愿,通過實(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;
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多