J2EE社區

          茍有恒,何必三更起五更眠;
          最無益,只怕一日曝十日寒.
          posts - 241, comments - 318, trackbacks - 0, articles - 16

          Struts2操作request、session和application對象

          Posted on 2009-10-23 01:46 xcp 閱讀(5051) 評論(0)  編輯  收藏 所屬分類: struts2

          Struts 2提供了多種方式來訪問上述的三種對象,歸結起來,可以劃分為兩大類:與Servlet API解耦的訪問方式和與Servlet API耦合的訪問方式。

          與Servlet API解耦的訪問方式(IoC方式)

          為了避免與Servlet API耦合在一起,方便Action類做單元測試,Struts 2對HttpServletRequest、HttpSession和ServletContext進行了封裝,構造了三個Map對象來替代這三種對象, 在Action中,直接使用HttpServletRequest、HttpSession和ServletContext對應的Map對象來保存和讀取 數據。

          要獲取這三個Map對象,可以使用com.opensymphony.xwork2.ActionContext類,ActionContext 是action執行的上下文,在ActionContext中保存了action執行所需的一組對象,包括parameters、request、 session、application和locale等。ActionContext類定義了如下方法,用于獲取 HttpServletRequest、HttpSession和ServletContext對應的Map對象。

          Ø public Object get(Object key)

          ActionContext類沒有提供類似getRequest()這樣的方法來獲取封裝了HttpServletRequest的Map對象。要得到請求Map對象,你需要為get()方法傳遞參數“request”。

          Ø public Map getSession()

          獲取封裝了HttpSession的Map對象。

          Ø public Map getApplication()

          獲取封裝了ServletContext的Map對象。

          我們看 通過ActionContext來獲取request、session和application對象的LoginAction1

          1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      10
                      11
                      12
                      13
                      14
                      15
                      16
                      17
                      18
                      19
                      20
                      21
                      22
                      23
                      24
                      25
                      26
                      27
                      28
                      29
                      30
                      31
                      32
                      33
                      34
                      35
                      36
                      37
                      38
                      39
                      40
                      41
                      42
                      43
                      44
                      45
                      46
                      47
                      48
                      49
                      50
                      51
                      52
                      53
                      54
                      55
                      56
                      57
                      58
                      59
                      60
                      61
                      62
                      63
                      64
                      65
                      66
                      67
                      68
                      69
                      
          package action;
                       
                      import java.util.Map;
                       
                      import dbPackage.User;
                       
                      import com.opensymphony.xwork2.Action;
                       
                      import com.opensymphony.xwork2.ActionContext;
                       
                      public class LoginAction1 implements Action
                       
                      {   private User user;
                       
                      public User getUser()
                       
                      { return user;
                       
                      }
                       
                      public void setUser(User user)
                       
                      { this.user = user;
                       
                      }
                       
                      @SuppressWarnings("unchecked")
                       
                      @Override
                       
                      public String execute() throws Exception
                       
                      {
                       
                      if("zhangsan".equals(user.getUsername())&& "1234".equals(user.getPassword()))
                       
                      { ActionContext context = ActionContext.getContext();
                       
                      Map request = (Map)context.get("request");
                       
                      Map session = context.getSession();
                       
                      Map application = context.getApplication();
                       
                      //在請求中放置歡迎信息。
                       
                      request.put("greeting", "歡迎您來到程序員之家");
                       
                      //在session中保存user對象
                       
                      session.put("user", user);
                       
                      //統計用戶訪問量,在application中保存用戶訪問量數據
                       
                      Integer count = (Integer)application.get("counter");
                       
                      if(null == count) count=1;
                       
                      else count++;
                       
                      application.put("counter", count);
                       
                      return SUCCESS;
                       
                      }else {
                       
                      return ERROR;
                       
                      } }}

          在成功頁面中,可以使用JSP內置的表達式語言來訪問request、session和application范圍的數據,代碼如例3-12所示。

          例3-12 success.jsp

          1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      10
                      11
                      12
                      13
                      14
                      15
                      
          <%@ page contentType="text/html;charset=GBK" %>
                       
                      <html>
                       
                      <head><title>歡迎頁面</title></head>
                       
                      <body>
                       
                      <h3>${sessionScope.user.username},${requestScope.greeting}<br>
                       
                      本站的訪問量是:${applicationScope.counter}</h3>
                       
                      </body>
                       
                      </html>

          與Servlet API耦合的訪問方式(非IoC方式)

          要獲得上述對象,關鍵Struts 2.0中com.opensymphony.xwork2.ActionContext類。我們可以通過它的靜態方法getContext()獲取當前 Action的上下文對象。 另外,org.apache.struts2.ServletActionContext作為輔助類(Helper Class),可以幫助您快捷地獲得這幾個對象。

          HttpServletRequest request = ServletActionContext.getRequest();
          HttpServletResponse response = ServletActionContext.getResponse();
          HttpSession session = request.getSession();

          如果你只是想訪問session的屬性(Attribute),你也可以通過ActionContext.getContext().getSession()獲取或添加session范圍(Scoped)的對象。



          本文轉載于:http://www.emool.cn/archives/y2009/318.html




          名稱: ?4C.ESL | .↗Evon
          口號: 遇到新問題?先要尋找一個方案乄而不是創造一個方案こ
          mail: 聯系我


          主站蜘蛛池模板: 饶平县| 上犹县| 湛江市| 金门县| 宁乡县| 苏尼特左旗| 广州市| 定南县| 抚松县| 五指山市| 大同县| 东阿县| 宜君县| 贵南县| 牡丹江市| 蓝山县| 新邵县| 增城市| 桂林市| 江安县| 乐昌市| 衡阳县| 宁化县| 阿荣旗| 华亭县| 聂荣县| 富源县| 英德市| 苍南县| 清涧县| 麻城市| 乾安县| 麻栗坡县| 德阳市| 台州市| 蓬溪县| 新源县| 诏安县| 安乡县| 黑水县| 敖汉旗|