posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2006年12月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 860072
          • 排名 - 44

          最新評論

          閱讀排行榜

          ActionContext 介紹
          Web 應用程序開發中,除了將請求參數自動設置到 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”);
          ActionContext com.opensymphony.xwork.ActionContext )是 Action 執行時的上下文,上下文可以看作是一個容器(其實我們這里的容器就是一個 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()
           
          ServletActionContext
          ServletActionContext com.opensymphony.webwork. ServletActionContext ),這個類直接繼承了我們上面介紹的 ActionContext ,它提供了直接與 JavaServlet 相關對象訪問的功能,它可以取得的對象有:
          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 里取得 JavaServlet 的相關對象:
          1、   取得 HttpServletRequest 對象:
          HttpServletRequest request = ServletActionContext. getRequest();
          2、   取得 HttpSession 對象:
          HttpSession session = ServletActionContext. getRequest().getSession();
           
          ServletActionContext ActionContext 有著一些重復的功能,在我們的 Action 中,該如何去抉擇呢?我們遵循的原則是:如果 ActionContext 能夠實現我們的功能,那最好就不要使用 ServletActionContext ,讓我們的 Action 盡量不要直接去訪問 JavaServlet 的相關對象。在使用 ActionContext 時有一點要注意:不要在 Action 的構造函數里使用 ActionContext.getContext() ,因為這個時候 ActionContext 里的一些值也許沒有設置,這時通過 ActionContext 取得的值也許是 null
          posted on 2006-12-13 19:44 周銳 閱讀(666) 評論(0)  編輯  收藏 所屬分類: Webwork
          主站蜘蛛池模板: 巴中市| 和平区| 贵溪市| 安国市| 花莲县| 邢台县| 湾仔区| 射洪县| 瓦房店市| 奈曼旗| 宁都县| 和静县| 邹城市| 九龙城区| 南昌县| 云阳县| 城固县| 项城市| 靖西县| 白银市| 沂源县| 兴国县| 偃师市| 项城市| 呼玛县| 闽侯县| 门头沟区| 博湖县| 富源县| 西平县| 天镇县| 徐闻县| 浦城县| 勃利县| 边坝县| 获嘉县| 屏东市| 大庆市| 班戈县| 广平县| 黑山县|