Struts2第四篇【請求數據自動封裝、Action得到域對象】

前言

前三篇的Struts博文基本把Struts的配置信息講解完了…..本博文主要講解Struts對數據的處理

一般地,我們使用Servlet的時候都是分爲幾個步驟的:

  1. 得到web層的數據、封裝數據
  2. 調用service層的邏輯業務代碼
  3. 將數據保存在域對象中,跳轉到對應的JSP頁面

現在問題來了,我們自己編寫的Action類是沒有request、response、Session、application之類的對象的….我們是怎麼得到web層的數據、再將數據存到域對象中的呢??

請求數據封裝

前面已經說過了,Struts預先幫我們完成了對數據封裝的功能,它是通過params攔截器來實現數據封裝的

            <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
  • 1

register.jsp

首先,我們填寫表單頁面的數據,請求Action處理數據


<form action="${pageContext.request.contextPath}/date01" method="post">
    用戶名:<input type="text" name="username"><br>
    密碼:<input type="text" name="psd"><br>
    年齡:<input type="text" name="age"><br>
    生日:<input type="text" name="birthday"><br>
    <input type="submit" value="註冊"><br>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Action封裝基本信息

在Action設置與JSP頁面相同的屬性,併爲它們編寫setter方法


    private String username;
    private String psd;
    private int  age;
    private Date birthday;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }

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

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

我們直接在業務方法中訪問這些變量,看是否能得到表單的值。

這裏寫圖片描述


Action封裝對象

一般地,我們註冊的時候,都是在Servlet上把基本信息封裝到對象上…那麼在Struts怎麼做呢?

  • 創建一個User類,基本的信息和JSP頁面是相同的。
package qwer;

import java.util.Date;

/**
 * Created by ozc on 2017/4/27.
 */
public class User {

    private String username;
    private String psd;
    private int  age;
    private Date birthday;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPsd() {
        return psd;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }

    public int getAge() {
        return age;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
  • 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
  • 在Action中定義User對象出來,並給出setter和getter方法….值得注意的是:基本信息只要setter就夠了,封裝到對象的話,需要setter和getter
public class ccAction extends ActionSupport {

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String register() {

        System.out.println(user.getUsername());
        System.out.println(user.getPsd());
        System.out.println(user.getAge());
        System.out.println(user.getBirthday());

        return "success";
    }


}

  • 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
  • 在JSP頁面,提交的name要寫成user.username之類的
<form action="${pageContext.request.contextPath}/register" method="post">
    用戶名:<input type="text" name="user.username"><br>
    密碼:<input type="text" name="user.psd"><br>
    年齡:<input type="text" name="user.age"><br>
    生日:<input type="text" name="user.birthday"><br>
    <input type="submit" value="註冊"><br>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

這裏寫圖片描述

得到域對象

Struts怎麼把數據保存在域對象中呢???Struts提供了三種方式

一、得到Servlet API

我們可以通過ServletActionContext得到Servlet API

由於每個用戶擁有一個Action對象,那麼底層爲了維護用戶拿到的是當前線程的request等對象,使用ThreadLocal來維護當前線程下的request、response等對象…



        //通過ServletActionContext得到Servlet API
        javax.servlet.ServletContext context = ServletActionContext.getServletContext();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        HttpServletResponse response = ServletActionContext.getResponse();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、ActionContext類

我們還可以通過ActionContext類來得到request、response、session、application被Struts封裝的Map集合

        //得到ActionContext 對象
        ActionContext context = ActionContext.getContext();
        Map<String, Object> session = context.getSession();
        Map<String, Object> application = context.getApplication();

        //這是request的Map
        Map<String, Object> request = context.getContextMap();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

三、實現接口

當web容器發現該Action實現了Aware接口,會把相對應的資源通過Aware接口注射進去,實際上就是一種IOC。

Aware實際就是一種攔截器,攔截代碼在執行Action之前執行、將資源注射到Action中

實現SessionAware, RequestAware, ApplicationAware接口,它就要在程序中實現三個方法:



    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;


    @Override
    public void setApplication(Map<String, Object> map) {
        this.application = map;
    }

    @Override
    public void setRequest(Map<String, Object> map) {

        this.request = map;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

通過這些方法,我們就可以得到對應的Map對象…..

小總結

那麼,我們有三種方法可以得到Servlet對應的對象,那麼該使用哪一種呢???

分析:

  • 第一種方法:需要導入Servlet的包,與Struts耦合了
  • 第二種方法:只能在業務方法中使用ActionContext類得到對應的Map對象,如果有多個方法,那麼每個方法都需要寫類似的代碼
  • 第三種方法:可以在類上定義成員變量,以至於整個類都能使用。但是需要實現類、實現對應的方法

如果我們需要使用到對象的其他方法,類似getContextPath()之類的,那麼只能使用第一種

如果我們就按照平常的開發,我們就使用第二種【獲取簡單,沒有耦合】

至於第三種,當我們將來可能開發BaseAction的時候,就使用它!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章