一、搭建Struts2的環境
1.1加入jar包
Struts2/apps/struts2-blank/WEB-INF/lib下的所有jar包到當前web應用的lib目錄下
1.2在web.xml文件中配置struts2
複製Struts2/apps/struts2-blank/WEB-INF/web.xml文件中的過濾器的配置到當前web應用的web.xml文件中
<!-- 配置Struts2的Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
1.3 在當前web應用的classpath下添加struts2的配置文件struts.xml中
複製struts/apps/struts2-blank/WEB-INF/class下的struts.xml文件到當前web應用的src下
1.4 關聯約束文檔DTD
Window——>preference——>XML Catalog——>add(KeyType選擇URI複製struts中的key)——>FileSystem(F:\研二\JAVAWEB\struts-2.3.31\src\core\src\main\resources\struts-2.3.dtd)——>struts.xml有提示
二、實現HelloWord
2.1 index.jsp
<a href=”product-input.action”>Product Input</a> |
2.2編寫struts.xml
http://localhost:8080/contextPath/namespace/actionName.action
<!-- package:包。struts2使用package來組織模塊。 name屬性:必須。方便其他的包繼承 extends:同java中的繼承。同城繼承struts-default --> |
<package name="helloWorld" extends="struts-default" namespace=”/”> <!-- 配置一個action:一個struts2的請求就是一個action --> <action name="product-input"> <result>/WEB-INF/pages/input.jsp</result> </action> <action name="product-save" class="com.atguigu.struts2.helloworld.Product" method="save"> <result name="details">/WEB-INF/pages/details.jsp</result> </action> </package> |
2). 不需要顯式的定義 Filter, 而使用的是 struts2 的配置文件. 3). details.jsp 比先前變得簡單了. ${requestScope.product.productName} -> ${productName} |
2.3 Product.java
private Integer productId; private String productName; private String productDesc; private Double productPrice; public String save() { return "details"; } |
2.4 pages中的input.jsp和details.jsp
<form action="product-save.action" method="post"> ProductName:<input type="text" name="productName"/> <br><br> ProductDesc:<input type="text" name="productDesc"/> <br><br> ProductPrice:<input type="text" name="productPrice"/> <br><br> <input type="submit" value="Submit"/> <br><br> </form> |
ProductId:${productId} <br><br> ProductName:${productName} <br><br> ProductDesc:${productDesc} <br><br> ProductPrice:${productPrice} <br><br> |
三、默認值
<package name="helloWorld" extends="struts-default"> <!-- 配置一個action:一個struts2的請求就是一個action --> <action name="product-input"> <result>/WEB-INF/pages/input.jsp</result> </action> <action name="product-save" class="com.atguigu.struts2.helloworld.Product" method="save"> <result name="details">/WEB-INF/pages/details.jsp</result> </action> </package> |
1. 默認namespace
<package name="helloWorld" extends="struts-default" namespace=”/”> |
http://localhost:8080/contextPath/namespace/actionName.action
2.默認action和result
<action name="product-input" class="com.opensymphony.xwork2.ActionSupport" method="execute"> <result name="success" type="dispatcher"> /WEB-INF/pages/input.jsp </result> </action> |
type="dispatcher" 轉發 |
四、action和 Action 類
1). action: 代表一個 Struts2 的請求.
2). Action 類: 能夠處理 Struts2 請求的類. ——>Product
> 屬性的名字必須遵守與 JavaBeans 屬性名相同的命名規則.
> 屬性的類型可以是任意類型. 從字符串到非字符串(基本數據庫類型)之間的數據轉換可以自動發生
> 必須有一個不帶參的構造器: 通過反射創建實例
> 至少有一個供 struts 在執行這個 action 時調用的方法
> 同一個 Action 類可以包含多個action 方法.
> Struts2 會爲每一個 HTTP 請求創建一個新的 Action 實例, 即 Action 不是單例的, 是線程安全的.
五、在 Action 中訪問 WEB 資源:
1). 什麼是 WEB 資源 ?
HttpServletRequest, HttpSession,ServletContext 等原生的 Servlet API。
2). 爲什麼訪問 WEB 資源?
B\S 的應用的Controller 中必然需要訪問 WEB 資源: 向域對象中讀寫屬性, 讀寫 Cookie, 獲取 realPath....
3). 如何訪問 ?
I. 和 Servlet API 解耦的方式: 只能訪問有限的 Servlet API 對象, 且只能訪問其有限的方法(讀取請求參數, 讀寫域對象的屬性, 使 session 失效...).
> 使用 ActionContext
public String execute() {
//0.獲取ActionContext對象 ActionContext actionContext = ActionContext.getContext(); //1.獲取application對應的Map Map<String, Object> applicationMap = actionContext.getApplication(); //設置屬性 applicationMap.put("applicationKey", "applicationValue"); //獲取屬性 Object date = applicationMap.get("date"); System.out.println("date="+date); //2.session Map<String, Object> sessionMap = actionContext.getSession(); sessionMap.put("sessionKey", "sessionValue"); //3.request(與其他不同) Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request"); requestMap.put("requestKey", "requestvalue"); //4.獲取請求參數對應的Map,並獲取指定參數值 //parameters只能讀不能寫,寫也不出錯 Map<String,Object> parameters = actionContext.getParameters(); System.out.println("parameters是"+((String[])parameters.get("name"))[0]); //不行 parameters.put("age", 100); return "success"; } |
> 實現 XxxAware 接口
見Struts2-3 |
> 選用的建議:若一個 Action 類中有多個 action 方法, 且多個方法都需要使用域對象的 Map 或 parameters, 則建議使用Aware 接口的方式
> session 對應的 Map 實際上是 SessionMap 類型的! 強轉後若調用其 invalidate() 方法, 可以使其 session 失效!
if(sessionMapinstanceof SessionMap){ SessionMap sMap = (SessionMap)sessionMap; sMap.invalidate(); System.out.println("session失效了"); } |
II. 和 Servlet API 耦合的方式: 可以訪問更多的 Servlet API 對象, 且可以調用其原生的方法.
> 使用 ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = ServletActionContext.getRequest().getSession(); ServletContext servletContext = ServletActionContext.getServletContext(); System.out.println("execute..."); |
> 實現 ServletXxxAware 接口.
六、關於 Struts2 請求的擴展名問題
1).org.apache.struts2 包下的default.properties 中配置了 Struts2 應用個的一些常量
2).struts.action.extension 定義了當前 Struts2 應用可以接受的請求的擴展名.
3). 可以在 struts.xml 文件中以常量配置的方式修改default.properties 所配置的常量.
<constant name="struts.action.extension" value="action,do,"></constant> |
七、ActionSupport
1).ActionSupport 是默認的 Action 類: 若某個 action 節點沒有配置 class 屬性, 則 ActionSupport 即爲待執行的 Action 類. 而 execute 方法即爲要默認執行的 action 方法
<actionname="testActionSupport">
<result>/testActionSupport.jsp</result>
</action>
等同於
<actionname="testActionSupport"
class="com.opensymphony.xwork2.ActionSupport"
method="execute">
<result>/testActionSupport.jsp</result>
</action>
2). 在手工完成字段驗證, 顯示錯誤消息, 國際化等情況下, 推薦繼承 ActionSupport.
八、result:
1). result 是 action 節點的子節點
2). result 代表 action 方法執行後, 可能去的一個目的地
3). 一個 action 節點可以配置多個 result 子節點.
4). result 的name 屬性值對應着 action 方法可能有的一個返回值.
<resultname="index">/index.jsp</result>
5). result 一共有 2 個屬性, 還有一個是 type: 表示結果的響應類型
6). result 的 type 屬性值在 struts-default 包的 result-types 節點的 name 屬性中定義.
常用的有
> dispatcher(默認的): 轉發. 同 Servlet 中的轉發.
> redirect: 重定向
>redirectAction: 重定向到一個 Action
注意: 通過redirect的響應類型也可以便捷的實現redirectAction 的功能!
<result name="index"type="redirectAction">
<paramname="actionName">testAction</param>
<paramname="namespace">/atguigu</param>
</result>
OR
<result name="index"type="redirect">/atguigu/testAction.do</result>
> chain: 轉發到一個 Action
注意: 不能通過 type=dispatcher 的方式轉發到一個 Action
只能是:
<result name="test"type="chain">
<paramname="actionName">testAction</param>
<paramname="namespace">/atguigu</param>
</result>
不能是:
<resultname="test">/atguigu/testAction.do</result>