SSH框架之Struts2第二篇

来源:https://www.cnblogs.com/haizai/archive/2019/09/14/11518978.html
-Advertisement-
Play Games

1.2 知識點 1.2.1 Struts2的Servlet的API的訪問 1.2.1.1 方式一 : 通過ActionContext實現 頁面: <h1>Servlet的API的訪問方式一:解耦合的方式</h1> <form action="${ pageContext.request.contex... ...


1.2 知識點
        1.2.1 Struts2的Servlet的API的訪問
            1.2.1.1 方式一 : 通過ActionContext實現
                頁面:
                <h1>Servlet的API的訪問方式一:解耦合的方式</h1>
                <form action="${ pageContext.request.contextPath }/requestDemo1.action" method="post">
                    姓名:<input type="text" name="name"/><br/>
                    年齡:<input type="text" name="age"><br/>
                    <input type="submit" value="提交">
                </form>
                編寫Action
                /**
                 * Servlet的API的訪問方式一:解耦合的方式
                 *     * 只能用於接收參數 和 操作域中的數據
                 * @author jt
                 */
                public class RequestDemo1Action extends ActionSupport{

                    @Override
                    public String execute() throws Exception {
                        // 接收數據:
                        /**
                         * 在ActionContext中提供了一些方法:操作域對象的數據
                         *     * Map<String,Object> getParameters();
                         *     * Map<String,Object> getSession();
                         *  * Map<String,Object> getApplication();
                         */
                        // 接收參數:
                        ActionContext actionContext = ActionContext.getContext();
                        Map<String,Object> map = actionContext.getParameters();
                        for (String key : map.keySet()) {
                            String[] arrs = (String[]) map.get(key);
                            System.out.println(key+"    "+Arrays.toString(arrs));
                        }
                        // 向request域中存值:
                        actionContext.put("reqName", "req如花");
                        
                        // 向session域中存值:
                        actionContext.getSession().put("sessName", "sess鳳姐");
                        
                        // 向application域中存值:
                        actionContext.getApplication().put("appName","app石榴");
                        return SUCCESS;
                    }
                }
        
            1.2.1.2 方式二: 實現特定介面的方式實現:
                在struts中提供了一些介面 : ServletRequestAware,ServletResponseAware,ServletContextAware;
                頁面:
                <h1>Servlet的API的訪問方式二:實現特定介面的方式</h1>
                <form action="${ pageContext.request.contextPath }/requestDemo2.action" method="post">
                    姓名:<input type="text" name="name"/><br/>
                    年齡:<input type="text" name="age"><br/>
                    <input type="submit" value="提交">
                </form>
        
                編寫Action:
                /**
                 * Servlet的API的訪問方式二:
                 * @author jt
                 *
                 */
                public class RequestDemo2Action extends ActionSupport implements ServletRequestAware,ServletContextAware{

                    private HttpServletRequest request;
                    private ServletContext application;

                    @Override
                    public String execute() throws Exception {
                        // 接收數據:使用request對象。
                        Map<String, String[]> parameterMap = request.getParameterMap();
                        for (String key : parameterMap.keySet()) {
                            String[] arrs = parameterMap.get(key);
                            System.out.println(key+"   "+Arrays.toString(arrs));
                            
                        }
                        // 向域中保存數據;
                //        向request域中保存數據
                        request.setAttribute("reqName", "r郝三");
                         // 向session域中保存數據
                        request.getSession().setAttribute("sessName", "s李四");
                        // 向application域中保存數據
                        application.setAttribute("appName", "a王五");
                        return NONE;
                    }

                    @Override
                    public void setServletRequest(HttpServletRequest request) {
                        this.request =  request;
                    }
                    
                    @Override
                    public void setServletContext(ServletContext application) {
                        this.application = application;
                    }
                }
            1.2.1.3方式 : 通過ServletActionContext對象的靜態方法實現 :
                頁面:
                <h1>Servlet的API的訪問方式三:通過ServletActionContext對象靜態方法獲取</h1>
                <form action="${ pageContext.request.contextPath }/requestDemo3.action" method="post">
                    姓名:<input type="text" name="name"/><br/>
                    年齡:<input type="text" name="age"><br/>
                    <input type="submit" value="提交">
                </form>
                編寫Action:
                /**
                 * Servlet的API訪問方式三:通過ServletActionContext的靜態方法訪問
                 * @author jt
                 *
                 */
                public class RequestDemo3Action extends ActionSupport {
                    public String execute() throws Exception {
                        //接收參數:
                        /*
                        ServletActionContext 在struts2的API中的 : 
                        HttpServletRequest getRequest();
                        HttpServletResponse getResponse();
                        ServletContext getServletContext();
                        
                        */
                        HttpServletRequest request = ServletActionContext.getRequest();
                        Map<String,String[]> parameterMap = request.getParameterMap();
                        for(String key : parameterMap.get()) {
                            String[] value = parameterMap.get(Key);
                            System.out.println(key + "  " + Arrays.toString(value));
                        }
                        
                        //向域中存值 : 
                        request.setAttribute("reqName" , "用request向域中存數據");
                        request.getSession.setAttribute("session","向session域中存數據");
                        ServletActionContext.getServletContext().setAttribute("ServletContext","向ServletContext域中存儲數據");
                        return super.execute();
                        
                    }
                }
        
            1.2.2.1 :結果頁面的分類 : 
                全局結果頁面 :
                    針對一個包下所有的action都生效的一個頁面.
                    <!--全局結果頁面-->
                    <global-results>
                        <result>/jsp/1.jsp</result>
                    </global-results>
                局部結果頁面 :
                    針對某一個action生效的一個頁面 : 
                    <action name="requestDemo1" class="com.baidu.struts2.demo1.ActionDemo1">
                    <result>/jsp/1.jsp</result>
                    </action>
            
            1.2.2.2 配置結果頁面 :
                <result>標簽配置:
                    name : 邏輯視圖的名稱.預設值是success.
                    type : 
                        dispatcher         : 預設值,轉發.(轉發到jsp的頁面)
                        redirect         : 重定向.(重定向到JSP的頁面),被跳轉的頁面中丟失傳遞的信息.
                        chain               : 轉發到另一個Action.
                        redirectAction  : 重定向到另一個Action.跳轉的頁面中丟失傳遞的信息
                        stream             : 文件的下載
            
            1.2.2.3 Struts2的多例性 : 
                    原來的Servlet是單例存在,多次請求,請求都是同一個Servlet的實例.Struts2中Action是多實例的.有異常請求就會有一個Action的實例.
                現在可以在Action中定義成員屬性了.
                
                大部分我們會優先使用模型驅動的方式,因為Struts2內部有很多結果是圍繞模型驅動設計的。但如果頁面向多個對象中封裝,那麼就需要使用屬性驅動的方式二了。
                這些都是像某個對象中封裝數據,

        1.2.3 Struts2的數據封裝 :
            1.2.3.1 Struts2中的數據封裝-屬性驅動 : 
                提供對應屬性的set方法的方式 :
                    頁面:
                    <h3>數據封裝的方式一:提供屬性的set方法的方式</h3>
                    <form action="${ pageContext.request.contextPath }/employee1Action.action" method="post">
                        姓名:<input type="text" name="name"/><br/>
                        年齡:<input type="text" name="age"/><br/>
                        性別:<input type="text" name="sex"/><br/>
                        工資:<input type="text" name="salary"/><br/>
                        <input type="submit" value="提交"/>
                    </form>
                    編寫Action:
                    /**
                     * 數據封裝的方式一:提供set方法的方式
                     * @author jt
                     *
                     */
                    public class Employee1Action extends ActionSupport{
                        private String name;
                        private Integer age;
                        private String sex;
                        private Double salary;
                        
                        public void setName(String name) {
                            this.name = name;
                        }

                        public void setAge(Integer age) {
                            this.age = age;
                        }

                        public void setSex(String sex) {
                            this.sex = sex;
                        }

                        public void setSalary(Double salary) {
                            this.salary = salary;
                        }

                        @Override
                        public String execute() throws Exception {
                            System.out.println("員工姓名:"+name);
                            System.out.println("員工年齡:"+age);
                            System.out.println("員工性別:"+sex);
                            System.out.println("員工工資:"+salary);
                            // 手動封裝數據:
                            Employee employee = new Employee();
                            employee.setName(name);
                            employee.setAge(age);
                            employee.setSex(sex);
                            employee.setSalary(salary);
                            
                            
                            return NONE;
                        }
                    }
                    這種方式不是特別常用,因為需要手動封裝數據,而且如果屬性很多,提供很多的set方法,導致Action類易讀性變差
        
                頁面中提供表達式的方式:
                    頁面:
                    <h3>數據封裝的方式二:頁面提供OGNL表達式的寫法</h3>
                    <form action="${ pageContext.request.contextPath }/employee2Action.action" method="post">
                        姓名:<input type="text" name="employee.name"/><br/>
                        年齡:<input type="text" name="employee.age"/><br/>
                        性別:<input type="text" name="employee.sex"/><br/>
                        工資:<input type="text" name="employee.salary"/><br/>
                        <input type="submit" value="提交"/>
                    </form>
                    編寫Action:
                    /**
                     * 數據封裝的方式二:頁面提供表達式的方式
                     * @author jt
                     *
                     */
                    public class Employee2Action extends ActionSupport{
                        // 提供成員的屬性employee,必須提供屬性的get和set方法
                        private Employee employee;
                        
                        public Employee getEmployee() {
                            return employee;
                        }
                        public void setEmployee(Employee employee) {
                            this.employee = employee;
                        }

                        @Override
                        public String execute() throws Exception {
                            System.out.println(employee);
                            return NONE;
                        }
                    }
        
            1.2.3.2 Struts2 中的數據封裝-模型驅動 :
                採用模型驅動完成數據封裝(推薦)
                頁面:
                <h3>數據封裝的方式三:採用模型驅動的方式</h3>
                <form action="${ pageContext.request.contextPath }/employee3Action.action" method="post">
                    姓名:<input type="text" name="name"/><br/>
                    年齡:<input type="text" name="age"/><br/>
                    性別:<input type="text" name="sex"/><br/>
                    工資:<input type="text" name="salary"/><br/>
                    <input type="submit" value="提交"/>
                </form>
                編寫Action:
                /**
                 * 數據封裝的方式三:採用模型驅動的方式
                 * @author jt
                 *
                 */
                public class Employee3Action extends ActionSupport implements ModelDriven<Employee>{
                    // 模型驅動使用的對象:必須手動構建對象的實例。
                    private Employee employee = new Employee();
                    @Override
                    public Employee getModel() {
                        return employee;
                    }
                    @Override
                    public String execute() throws Exception {
                        System.out.println(employee);
                        return NONE;
                    }
                }
                ***** 模型驅動只能向一個實體中封裝數據,如果需要向多個實體封裝數據優先採用第二種方式。
        1.2.4 Struts2 的複雜數據的封裝 :
            1.2.4.1 封裝到List集合 :
                頁面:
                <form action="${ pageContext.request.contextPath }/product1Action.action" method="post">
                    商品名稱:<input type="text" name="list[0].name"/><br/>
                    商品價格:<input type="text" name="list[0].price"/><br/>
                    商品名稱:<input type="text" name="list[1].name"/><br/>
                    商品價格:<input type="text" name="list[1].price"/><br/>
                    商品名稱:<input type="text" name="list[2].name"/><br/>
                    商品價格:<input type="text" name="list[2].price"/><br/>
                    <input type="submit" value="批量導入"/>
                </form>
                Action:
                /**
                 * 封裝複雜的數據到List集合中。
                 * @author jt
                 *
                 */
                public class Product1Action extends ActionSupport{
                    private List<Product> list;
                    
                    public List<Product> getList() {
                        return list;
                    }

                    public void setList(List<Product> list) {
                        this.list = list;
                    }

                    @Override
                    public String execute() throws Exception {
                        for (Product product : list) {
                            System.out.println(product);
                        }
                        return NONE;
                    }
                }
        
            1.2.4.2 封裝到Map集合
                頁面:
                <h1>批量插入商品:封裝到Map集合</h1>
                <form action="${ pageContext.request.contextPath }/product2Action.action" method="post">
                    商品名稱:<input type="text" name="map['one'].name"/><br/>
                    商品價格:<input type="text" name="map['one'].price"/><br/>
                    商品名稱:<input type="text" name="map['two'].name"/><br/>
                    商品價格:<input type="text" name="map['two'].price"/><br/>
                    商品名稱:<input type="text" name="map['three'].name"/><br/>
                    商品價格:<input type="text" name="map['three'].price"/><br/>
                    <input type="submit" value="批量導入"/>
                </form>
                Action:
                /**
                 * 複雜類型數據封裝:封裝到Map集合
                 * @author jt
                 *
                 */
                public class Product2Action extends ActionSupport {
                    private Map<String,Product> map;
                    
                    public Map<String, Product> getMap() {
                        return map;
                    }

                    public void setMap(Map<String, Product> map) {
                        this.map = map;
                    }

                    @Override
                    public String execute() throws Exception {
                        for (String key : map.keySet()) {
                            Product product = map.get(key);
                            System.out.println(key+"   "+product);
                        }
                        return NONE;
                    }
                }
        
隨堂筆記 :    
        1 strtus2對servlet API的訪問

         1  使用ServletActionContext類的靜態方法(重要)
                    
         2  使用ActionContext對象的方法(理解)往域中存儲數據,到頁面展示(ps:這個對象獲取不到域對象,只能操作域中的數據)
                    靜態方法getContext()獲取ActionContext對象

                特點: 只能接受頁面的數據,以及對域中數據的進行操作

                       獲取不response對象做不了響應,也獲取不到域對象
                        使用ActionContext對象的方法(理解)
                    // getContext()  --獲取ActionContext對象
                    // Map<String,Object> ----- getParameters() 獲取頁面的所有數據
                    // Map<String,Object> ------getSession()   獲取session域中的數據
                    // Map<String,Object> -------getApplication()  獲取servletContext域中的數據
         3  實現特定的介面來獲取的方法(瞭解)
證明Action是單實例還是多實例?
            servlet是單實例  成員屬性有線程危機
            Action是多實例   成員屬性沒有線程危機

2 結果頁面的邏輯視圖配置
        分類:
            全局配置
                針對一個包下所有的action都生效的一個頁面
            
            局部配置
                針對某一個action生效的一個頁面
        
            註意:如果全局配置和局部配置都配置了,局部配置的優先順序大於全局配置

        result標簽: 對返回的邏輯視圖進行匹配

            name:
                邏輯視圖的名稱。預設值是success。

            type:
                dispatcher  預設  請求轉發(用於轉發到jsp頁面)

                redirect   重定向(用於重定向到jsp頁面)

                chain     請求轉發(用於轉發到action)

                redirectAction  重定向(用於重定向到action)

                stream  用於下載的(CRM3天案例)

3 struts2對頁面數據的封裝(模型驅動,屬性驅動)

             1 屬性驅動---set屬性方式(例如:分頁玩法)  (重點)
                  條件:
                     1 需要在action的成員位置提供屬性,並且要有set()方法

             2 屬性驅動---頁面表達式的方式  (理解)
                 條件:
                    1 需要在action的成員位置上申明javabean對象,且提供get/set方法
                            例如:
                                private User user;
                                get()/set()

                    2 在頁面上的參數name屬性需要:
                            name=javabean對象.屬性

                            例如: user.username;
                                  uiser.age;


             3 模型驅動(單一的對象)  (掌握)
                    條件:
                        1 讓action實現一個介面: ModelDriven

                        2 需要在action的成員位置上申明javabean對象,並且這個對象得實例化 private User user=new User;

                        3 需要 ModelDriven 這個介面中的getModel()方法

                        4 需要在getModel()方法裡面將我們的javabean對象以返回值的形式返回回去

                        主頁:頁面還是正常的寫法
                     
             4 批量方式(針對的是多個對象數據的封裝)
                    條件:
                        需要在action中提供集合,且提供get()/set()方法
                
                list封裝:

                      在action中集合名稱:
                                ll

                      在頁面需要:
                            ll[0].username
                            ll[0].age

                            ll[1].usrname
                            ll[1].age

                map封裝:
                     在action中集合名稱:
                                mm

                    在頁面需要:
                            
                            mm[鍵名].屬性

總結:
     1 struts2怎麼樣獲取到servlet的API(request,response,session,ServletContext,以及接受頁面的數據)

            1 通過ServletActionContext類的靜態方法

            2 通過ActionContext對象的方法(理解)
                        getActionContext()--對象的獲取
                        註意:只能接受操作頁面參數,以及操作域中的數據
                             不能獲取到域對象,也不能做響應(response對象也獲取不到)

            3 通過特定的介面來實現
                      ServletRequestAware,ServletResponseAware,ServletContextAware,SessionAware

    2 結果邏輯視圖的配置
            全局配置 針對的是一個包下的所有action
            局部配置 針對的是指定的action
            2個都存在的情況下優先順序:局部>全局
            result標簽:
            name: 邏輯視圖的名稱。預設值是success。
            type:
                dispatcher    :預設值,請求轉發。(請求轉發到JSP的頁面)
                  redirect    :重定向。(重定向到JSP的頁面)
                  chain        :轉發到另一個Action。
                  redirectAction:重定向到另一個Action。
                  stream    :文件的下載。

    3 struts2對頁面數據封裝的方式: 4種  ps:action多實例的,每訪問一次就創建一個action對象,所以它成員位置的屬性不存線上程危機
            1 屬性驅動---set方式
                條件: action提供相應的成員屬性,必須得有set方法
                
            2 屬性驅動---頁面表達式的方式
                條件:
                    1 需要在action的成員位置上申明javabean對象,且提供get/set方法
                    2 在頁面上的參數name屬性需要:
                            name=javabean對象.屬性

                            例如: user.username;
                                  uiser.age;

            3 模型驅動
                條件:
                        1 讓action實現一個介面: ModelDriven

                        2 需要在action的成員位置上申明javabean對象,並且這個對象得實例化 private User user=new User;

                        3 需要 ModelDriven 這個介面中的getModel()方法

                        4 需要在getModel()方法裡面將我們的javabean對象以返回值的形式返回回去

                        主頁:頁面還是正常的寫法
            
            
            4 批量封裝
                條件: 提供集合且提供set/get方法

                list:
                    在action中: ll

                    在頁面: ll[索引].屬性
                
                map:
                    在action中: mm
                    在頁面: mm['鍵名'].屬性

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 二叉堆因為實現簡單,因此在需要優先隊列的時候幾乎總是使用二叉堆。d 堆是二叉堆的簡單推廣,它恰像一個二叉堆,只是所有的節點都有d個兒子(因此,二叉堆又叫2 堆)。下圖表示的是一個3 堆。註意,d 堆要比二叉堆淺得多,它將Insert操作的運行時間改進為。然而,對於大的d,DeleteMin操作費時得 ...
  • 本文針對window操作系統與oracle12C的版本。 1.sqlplus執行單個sql文件 1.執行sqlplus登陸命令:sqlplus username/password@host:port/service_name as sysdba (其中普通用戶可以不加後面的 as sysdba) 2 ...
  • Gradle 本身只提供基本框架和核心概念,幾乎所有的功能都是以插件的方式提供的。 例如構建 Java 應用的功能就是通過 Java 插件實現的。 ...
  • 什麼是jsonp : Jsonp(JSON with Padding) 是 json 的一種"使用模式",可以讓網頁從別的功能變數名稱(網站)那獲取資料,即跨域讀取數據。 為什麼我們從不同的域(網站)訪問數據需要一個特殊的技術( JSONP )呢?這是因為同源策略。 同源策略,它是由 Netscape 提出 ...
  • 1.類選擇器(通過類名進行選擇) 效果圖: 2.id選擇器(通過id進行選擇) 效果圖: 3.標簽選擇器(通過id進行選擇) 效果圖: 4.分組選擇器(可一次選擇多個標簽以設置相同樣式) 效果圖: 5.後代選擇器(選擇某個標簽的所有後代以設置相同樣式) 效果圖: 6.屬性選擇器(通過屬性(如name ...
  • jQuery jQuery介紹 1.jQuery是一個輕量級的、相容多瀏覽器的JavaScript庫。 2.jQuery使用戶能夠更方便地處理HTML Document、Events、實現動畫效果、方便地進行Ajax交互,能夠極大地簡化JavaScript編程。它的宗旨就是:“Write less, ...
  • 使用的原因 在前端開發當中有一部分的用戶行為會頻繁操作觸發事件執行,而對於DOM操作,資源載入等耗費性能的處理,很可能導致頁面卡頓,甚至瀏覽器崩潰,函數節流和防抖就是解決類似需求應運而生的 節流 預定一個函數只有在大於等於執行周期時才執行,周期內調用不執行 ,就像水滴攢到一定重量會下落一樣 運用場景 ...
  • 同步、非同步、阻塞、非阻塞 [TOC] 1、同步 所謂同步,就是發出一個功能調用時,在沒有得到結果之前,該調用就不返回或繼續執行後續操作。 簡單來說,同步就是必須一件一件事做,等前一件做完了才能做下一件事。 例如:B/S模式中的表單提交,具體過程是:客戶端提交請求 等待伺服器處理 處理完畢返回,在這個 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...