01.Struts 2基本結構
使用Struts2框架實現用登錄的功能,使用struts2標簽和ognl表達式簡化了試圖的開發,並且利用struts2提供的特性對輸入的數據進行驗證,以及訪問ServletAPI時實現用戶會話跟蹤,其簡單的程序運行流程圖如下
Struts2框架是基於MVC模式。基於MVC模式框架的核心就是控制器對所有請求進行統一處理.Struts2的控制器StrutsPrepareAndExecuteFilter由ServletAPI中的Filter充當,當web容器的接收到登錄請求后,將請求交由在web.xml中配置的過濾器StrutsPrepareAndExecuteFilter.
1.web.xml
Struts2框架需要在web.xml中配置其核心控制器——StrutsPrepareAndExecuteFilter,用於對框架進行初始化,以及處理所有的請求.
如何搜索xml文件加載順序?
1.找到項目的web.xml找到核心過濾器
2.找到init方法
Action中動態方法調用<Dynamic Method Invocation> DMI
第一種方式:
自定義DMIAction類,使它繼承ActionSupport類,該類無需手動重寫execute(),底層有默認實現。因此我們也可以自定義方法list。
struts.xml中的action元素植入method調用前台返回的方法list
若一個類中有多個方法,在struts.xml中需植入多個action元素,因此該方法的安全性低
第二種方式:
在struts.xml中開啟動態方法調用,即可使用一個action,並通過在Action的名稱中使用感嘆號(!)來標識要調用的方法名稱
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
|
/*
* 添加圖書
*/
public
String add()
throws
Exception {
System.out.println(
"======add===="
);
return
"add"
;
}
/*
* 刪除圖書
*/
public
String del()
throws
Exception {
System.out.println(
"======del===="
);
return
"del"
;
}
/*
* 修改圖書
*/
public
String edit()
throws
Exception {
System.out.println(
"======edit===="
);
return
"edit"
;
}
|
執行效果:
四、Action中通配符的使用
通配符用星號(*)表示,用於配置0個或多個字符串,在配置Action時,可以在action元素的name屬性中使用星號來匹配任意的字符串
實現效果: