Struct2入門二

Action訪問Servlet
1、 在Action 中解耦合方式 間接訪問 Servlet API ——— 使用 ActionContext 對象
在struts2 中 Action API 已經與 Servlet API 解耦合 (沒有依賴關係 )
* Servlet API 常見操作 : 表單提交請求參數獲取,向request、session、application三個範圍存取數據

actionContext = ActionContext.getContext();
1) actionContext.getParameters(); 獲得所有請求參數Map集合
2) actionContext.put(“company”, “傳智播客”); / actionContext.get(“company”) 對request範圍存取數據
3) actionContext.getSession(); 獲得session數據Map,對Session範圍存取數據
4) actionContext.getApplication(); 獲得ServletContext數據Map,對應用訪問存取數據

2、 使用接口注入的方式,操作Servlet API (耦合)
ServletContextAware : 注入ServletContext對象
ServletRequestAware :注入 request對象
ServletResponseAware : 注入response對象

  • 程序要使用哪個Servlet的對象,實現對應接口

3、 在Action中直接通過 ServletActionContext 獲得Servlet API
ServletActionContext.getRequest() : 獲得request對象 (session)
ServletActionContext.getResponse() : 獲得response 對象
ServletActionContext.getServletContext() : 獲得ServletContext對象
* 靜態方法沒有線程問題,ThreadLocal
Result結果類型
Action處理請求後, 返回字符串(邏輯視圖名), 需要在struts.xml 提供 元素定義結果頁面
1、 局部結果頁面 和 全局結果頁面


/demo6/result.jsp



/demo6/result.jsp

2、 結果頁面跳轉類型
* 在struts-default.xml 定義了 一些結果頁面類型
* 使用默認type 是 dispatcher 轉發 (request.getRequestDispatcher.forward)

1) dispatcher :Action 轉發給 JSP
2) chain :Action調用另一個Action (同一次請求)
hello hello是一個Action的name
3) redirect : Action重定向到 JSP
4) redirectAction :Action重定向到另一個Action
hello
總結
1、 struts2 環境搭建 (導入jar包、web.xml、 struts.xml )
2、 struts2 運行流程
3、 配置文件加載順序
4、 元素配置
5、 Action書寫三種方式
6、 指定method方法調用、通配符、動態方法調用
7、 Action訪問Servlet API
8.關於result標籤的 type屬性取值.
作業
登陸練習完成
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(20) NOT NULL,
password varchar(20) NOT NULL,
PRIMARY KEY (id)
)

INSERT INTO user VALUES (‘1’, ‘admin’, ‘123’);

Action處理請求參數
struts2 和 MVC 定義關係
StrutsPrepareAndExecuteFilter : 控制器
JSP : 視圖
Action : 可以作爲模型,也可以是控制器

struts2 Action 接受請求參數 :屬性驅動 和 模型驅動
Action處理請求參數三種方式
第一種 :Action 本身作爲model對象,通過成員setter封裝 (屬性驅動 )
頁面:
用戶名

Action :
public class RegistAction1 extends ActionSupport {
private String username;
public void setUsername(String username) {
this.username = username;
}
}
問題一: Action封裝數據,會不會有線程問題 ?
* struts2 Action 是多實例 ,爲了在Action封裝數據 (struts1 Action 是單例的 )
問題二: 在使用第一種數據封裝方式,數據封裝到Action屬性中,不可能將Action對象傳遞給 業務層
* 需要再定義單獨JavaBean ,將Action屬性封裝到 JavaBean

第二種 :創建獨立model對象,頁面通過ognl表達式封裝 (屬性驅動)
頁面:
用戶名
—– 基於OGNL表達式的寫法
Action:
public class RegistAction2 extends ActionSupport {
private User user;
public void setUser(User user) {
this.user = user;
}

        public User getUser() {
            return user;
        }
    }

問題: 誰來完成的參數封裝

第三種 :使用ModelDriven接口,對請求數據進行封裝 (模型驅動 ) —– 主流
頁面:
用戶名

Action :
public class RegistAction3 extends ActionSupport implements ModelDriven {
private User user = new User(); // 必須手動實例化
public User getModel() {
return user;
}
}
* struts2 有很多圍繞模型驅動的特性
* 爲模型驅動提供了更多特性

對比第二種、第三種 : 第三種只能在Action中指定一個model對象,第二種可以在Action中定義多個model對象


封裝數據到Collection和Map
1) 封裝數據到Collection 對象
頁面:
產品名稱

Action :
public class ProductAction extends ActionSupport {
private List products;

        public List<Product> getProducts() {
            return products;
        }

        public void setProducts(List<Product> products) {
            this.products = products;
        }
    }

2) 封裝數據到Map 對象
頁面:
產品名稱
======= one是map的鍵值
Action :
public class ProductAction2 extends ActionSupport {
private Map

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