<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    想飛就別怕摔

    大爺?shù)牟M罵人

    struts2學(xué)習(xí)筆記(四)--獲取Session和request方法

    action中的幾種寫法

    //第一種很少用
    public class LoginAction1 extends ActionSupport {
        
        
    private Map request;
        
    private Map session;
        
    private Map application;
        
        
    public LoginAction1() {
            request 
    = (Map)ActionContext.getContext().get("request");
            session 
    = ActionContext.getContext().getSession();
            application 
    = ActionContext.getContext().getApplication();
        }
        
        
    public String execute() {
            request.put(
    "r1""request1");
            session.put(
    "s1""session1");
            application.put(
    "a1""application1");
            
    return SUCCESS; 
        }
    }
    //第二種幾乎都用這種方式
    public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
        
        
    private Map<String, Object> request;
        
    private Map<String, Object> session;
        
    private Map<String, Object> application;
        
        
    //DI dependency injection
        
    //IoC inverse of control
        public String execute() {
            request.put(
    "r1""r1");
            session.put(
    "s1""s1");
            application.put(
    "a1""a1");
            
    return SUCCESS; 
        }

        @Override
        
    public void setRequest(Map<String, Object> request) {
            
    this.request = request;
        }

        @Override
        
    public void setSession(Map<String, Object> session) {
            
    this.session = session;
        }

        @Override
        
    public void setApplication(Map<String, Object> application) {
            
    this.application = application;
        }
        
        
    }
    //第三種很少用
    public class LoginAction3 extends ActionSupport {
        
        
    private HttpServletRequest request;
        
    private HttpSession session;
        
    private ServletContext application;
        
        
    public LoginAction3() {
            request 
    = ServletActionContext.getRequest();
            session 
    = request.getSession();
            application 
    = session.getServletContext();
        }
        
        
    public String execute() {
            request.setAttribute(
    "r1""r1");
            session.setAttribute(
    "s1""s1");
            application.setAttribute(
    "a1""a1");
            
    return SUCCESS; 
        }
        
    }
    //第四種很少用
    public class LoginAction4 extends ActionSupport implements ServletRequestAware {
        
        
    private HttpServletRequest request;
        
    private HttpSession session;
        
    private ServletContext application;
        
        
        
        
    public String execute() {
            request.setAttribute(
    "r1""r1");
            session.setAttribute(
    "s1""s1");
            application.setAttribute(
    "a1""a1");
            
    return SUCCESS; 
        }



        @Override
        
    public void setServletRequest(HttpServletRequest request) {
            
    this.request = request;
            
    this.session = request.getSession();
            
    this.application = session.getServletContext();
        }
        
    }

    jsp頁面獲取request/session/application中的值

    1 <s:property value="#request.r1"/> | <%=request.getAttribute("r1"%> <br />
    2     <s:property value="#session.s1"/> | <%=session.getAttribute("s1"%> <br />
    3     <s:property value="#application.a1"/> | <%=application.getAttribute("a1"%> <br />
    4     <s:property value="#attr.a1"/><br />
    5     <s:property value="#attr.s1"/><br />
    6     <s:property value="#attr.r1"/><br />


    一下是我看到的一篇講的比較詳細(xì)的文章:

    在Struts1.*中,要想訪問request、response以及session等Servlet對象是很方便的,因?yàn)樗鼈円恢笔亲鳛樾螀⒃诟鱾€方法之間進(jìn)行傳遞的,而在Struts2中我們就很難看到它們的芳蹤了,因?yàn)槲覀儷@得表單中的值都是通過預(yù)先設(shè)置好了的get方法來得到的,那么如果有些參數(shù)我們必須通過request.getParametre或者session.getAttribute來得到,那么應(yīng)該怎么做呢?按照Max的教程上的說法,可以分為兩種:IoC方式和非IoC方式,如何理解這兩種方式的區(qū)別呢?IoC是Spring里面的特征之一,字面意思是反轉(zhuǎn)控制,說白了就是依賴注入,比方說類A依賴類B,那么就主動的給A注入一個類B的對象,下面看一下這兩種方法的具體實(shí)現(xiàn)。
    1.非Ioc方式
    這種方式主要是利用了com.opensymphony.xwork2.ActionContext類以及org.apache.struts2.ServletActionContext類,具體的方法如下所示。
    獲得request對象:

    1. HttpServletRequest request = ServletActionContext.getRequest ();
    2. ActionContext ct= ActionContext.getContext()
    3.    HttpServletRequest request=
    4. (HttpServletRequest)ct.get(ServletActionContext.HTTP_REQUEST);

    獲得session對象:
    在Struts2中底層的session都被封裝成了Map類型,我們稱之為SessionMap,而平常我們所說的session則是指HttpSession對象,具體的獲得方法如下所示。

    1. Map session=ActionContext.getSession();
    2. Map session=(Map)ActionContext.getContext().getActionContext.SESSION);

    得到這個SessionMap之后我們就可以對session進(jìn)行讀寫了,如果我們想得到原始的HttpSession可以首先得到HttpServletRequest對象,然后通過request.getSession()來取得原始的HttpSession對象。一般情況下SessionMap已經(jīng)可以完成所有的工作,我們不必再去碰底層的session了。
    2.IoC方式
    這種方式相對來說變化就比較少了,具體流程如下所示。
    獲得request對象:
    第一步:讓action實(shí)現(xiàn)ServletRequestAware接口
    第二步:在action中聲明一個HttpServletRequest類型的實(shí)例變量
    第三步:在action中實(shí)現(xiàn)ServletRequestAware接口的setServletRequest方法,實(shí)現(xiàn)方式很簡單,如下所示。
    private HttpServletRequest request;
    public void setServletRequest(HttpServletRequest request) {
    this.request = request;
    }
    獲得Session對象(注意,此時的session是SessionMap類型):
    第一步:讓action實(shí)現(xiàn)SessionAware接口
    第二步:在action中聲明一個HttpServletRequest類型的實(shí)例變量
    第三步:在action中實(shí)現(xiàn)SessionAware接口的setSession方法,實(shí)現(xiàn)方式很簡單,如下所示。
    private Map session;
    publicvoid setSession(Map session) {
    this. session = session;
    }
    以下是另一篇關(guān)于得到Request和Session的文章:

    1. Struts2里,如果需要在Action中使用session,可以通過下面兩種方式得到
    2. 1.通過ActionContext class中的方法getSession得到
    3. 2.Action實(shí)現(xiàn)org.apache.struts2.interceptor.SessionAware接口的方式來對session進(jìn)行操作
    4.  
    5. 下面先看一個采用第一種方式,在action中得到session的例子
    6. package s2.ex.action;
    7.  
    8.  
    9. import java.util.Map;
    10.  
    11.  
    12. import com.opensymphony.xwork2.ActionContext;
    13.  
    14. import com.opensymphony.xwork2.ActionSupport;
    15.  
    16.  
    17. public class SessionTestAction extends ActionSupport { 
    18.  
    19.  
    20.     public String execute() { 
    21.  
    22.      ActionContext actionContext = ActionContext.getContext();
    23.  
    24.        Map session = actionContext.getSession();
    25.  
    26.        session.put("USER_NAME", "Test User");
    27.  
    28.        return SUCCESS;
    29.  
    30.     } 
    31.  
    32. }
    33. 在這個例子中,通過ActionContext得到session,并往session里放置一個keyUSER_NAME,值為Test User的內(nèi)容。
    34.  
    35. 下面是一個實(shí)現(xiàn)org.apache.struts2.interceptor.SessionAware接口來對session操作的例子
    36. package s2.ex.action;
    37.  
    38.  
    39. import java.util.Map;
    40.  
    41.  
    42. import org.apache.struts2.interceptor.SessionAware;
    43.  
    44.  
    45. import com.opensymphony.xwork2.ActionSupport;
    46.  
    47.  
    48. public class SessionTest1Action extends ActionSupport implementsSessionAware { 
    49.  
    50.     private Map session;
    51.  
    52.     public void setSession(Map session) { 
    53.  
    54.        this.session = session;
    55.  
    56.  
    57.     } 
    58.  
    59.     public String execute() { 
    60.  
    61.        this.session.put("USER_NAME", "Test User 1");
    62.  
    63.        return SUCCESS;
    64.  
    65.     } 
    66.  
    67. }
    68.  
    69. 在這個例子中實(shí)現(xiàn)了接口SessionAware中的setSession方法。
    70.  
    71. 上面兩種方式都可以得到session,能實(shí)現(xiàn)的功能都是一樣的。
    72. 這里推薦通過第二種方式來使用session,原因是便于做單體測試,用第二種方式,只需要構(gòu)造一個Map就可以對action class進(jìn)行單體測試了。
    73.  
    74.     在一個項目中可能會有很多action都需要用到session,如果每個action都來實(shí)現(xiàn)org.apache.struts2.interceptor.SessionAware這個接口,可能會顯得比較麻煩,所以建議作一個抽象的BaseAction類來實(shí)現(xiàn)org.apache.struts2.interceptor.SessionAware接口,以后所有的action只要繼承這個BaseAction就可以了。
    75.  
    76. 下面是一個如何在JSP中使用session的例子。
    77. <%@ page contentType="text/html; charset=UTF-8" %>
    78.  
    79. <%@page pageEncoding="utf-8" %>
    80.  
    81. <%@taglib prefix="s" uri="/struts-tags" %>
    82.  
    83. <html>
    84.  
    85. <head>
    86.  
    87.     <title>Session Test</title>
    88.  
    89. </head>
    90.  
    91.  
    92. <body>
    93.  
    94. <h1><s:property value="#session.USER_NAME"/></h1>
    95.  
    96. <h1></h1>
    97.  
    98. </body>
    99.  
    100. </html>
    101.  
    102.    一般在項目中往往會往session里放置一個Object,必如說useruser里有個boolean adminString userName,如果user里存在isAdmin的方法,在jsp中可以通過<s:if test="#session.user.admin">來判斷用戶有沒有管理權(quán)限,通過<s:propertyvalue="#session.user.userName">或者來取得用戶名。



    posted on 2009-09-09 20:14 生命的綻放 閱讀(14626) 評論(3)  編輯  收藏 所屬分類: Struts2.0

    評論

    # re: struts2獲取Session和request方法(轉(zhuǎn)) 2011-05-27 15:23 easy518網(wǎng)址導(dǎo)航

    http://www.easy518.com/  回復(fù)  更多評論   

    # re: struts2學(xué)習(xí)筆記(四)--獲取Session和request方法 2014-08-27 00:18 hetongfei

    貌似第二種方法錯了  回復(fù)  更多評論   

    # re: struts2學(xué)習(xí)筆記(四)--獲取Session和request方法 2014-08-27 00:34 hetongfei

    喔 對不起,我將RequestAware看成ServletRequestAware了  回復(fù)  更多評論   

    <2014年8月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(5)

    隨筆分類(94)

    隨筆檔案(93)

    文章分類(5)

    文章檔案(5)

    相冊

    JAVA之橋

    SQL之音

    兄弟之窗

    常用工具下載

    積分與排名

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 日本一区午夜艳熟免费| 亚洲成人在线免费观看| 在线观看永久免费视频网站| 亚洲AV无码1区2区久久| 色爽黄1000部免费软件下载| 久久久久av无码免费网| 日本亚洲欧洲免费天堂午夜看片女人员| 18禁成人网站免费观看| 亚洲色婷婷综合开心网| 亚洲大成色www永久网址| 麻豆视频免费观看| 亚洲精品人成电影网| 无码人妻精品中文字幕免费| 亚洲精品国产字幕久久不卡| 国产精品1024在线永久免费| 久久精品国产亚洲7777| 一级特级女人18毛片免费视频| 免费a级毛片视频| 国产精品亚洲精品日韩电影| 日韩中文无码有码免费视频| 亚洲一区二区三区高清在线观看 | 亚洲精品福利视频| 最近免费mv在线观看动漫| 亚洲日韩aⅴ在线视频| yellow视频免费在线观看| 亚洲日韩在线第一页| 成人嫩草影院免费观看| 亚洲人成色77777在线观看大| 亚洲女人18毛片水真多| 国产91免费视频| jlzzjlzz亚洲jzjzjz| 成人免费视频77777| ass亚洲**毛茸茸pics| 成人a视频片在线观看免费| 亚洲欧洲免费无码| 国产精品色午夜视频免费看| 理论亚洲区美一区二区三区| 亚洲国产精品综合久久一线| 一个人免费播放在线视频看片| 中文字幕在线亚洲精品 | 国产18禁黄网站免费观看|