1.struts.xml文件配置:
包配置<package>標籤、常量配置<constant>標籤、包含配置<include>標籤
①<package> :主要對action配置,package的屬性值一般固定寫法
<!--配置Struts2的包
package :
name :包名 方便管理action,不能與項目中的包名一樣
extends :繼承哪個包,值通常不修改......struts-default(裏面實現了各種攔截器)
namespace :名稱空間,與<action>標籤中的name屬性共同決定訪問路勁
三種寫法: 帶名稱的名稱空間 :namespace="/aaa" 先訪問
根名稱空間 :namespace="/"
默認名稱空間 :namespace=""
abstract :抽象的包,用於其他包的繼承
-->
<package name="hello" extends="struts-default" namespace="/">
<!--配置Action
name :與namespace共同決定訪問路勁
class :Action的全路徑
method :執行Action的那個方法名的名稱: 默認值.....execute()
converter :自定義一個類型轉化器
-->
<action name="hello" class="com.jjxy.struts2.HelloAction" method="execute">
<!--配置頁面的跳轉-->
<result name="success">success.jsp</result>
</action>
②<constant> : 常量配置,Struts2內置非常多的常量.....在struts2-core-2.3.24.jar 中的 default.properties文件中
<!--struts2常量配置
常量修改 :配置文件的加載順序 1->2->3 後加載的配置文件會覆蓋前面的配置文件
1.struts.xml constant標籤(常量配置首選位置)
2.在src下新建一個struts.properties文件 只能修改常量
3.在web.xml文件 初始化參數 filter 標籤中 <init-param>
<param-name>struts.action.extension</param-name>
<param-value>action</param-value>
</init-param>
-->
<constant name="struts.action.extension" value="action"/><!--修改structs2 的擴展名-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/><!--動態方法訪問打開-->
③<include> : 引入其他路徑下的配置文件....分模塊開發
<include file="com/jjxy/struts2/demo1/pojo_struts.xml"/>
<include file="com/jjxy/struts2/demo2/struts.xml"/>
2.Action控制類的三種方式 :
pojo(簡單的java對象)、實現Action接口、繼承ActionSupport類(開發常用的方式)
①pojo ................只需在控制類中定義一個返回值爲String類型的execute()方法 :如下
public class HelloAction {
public String execute(){
System.out.println("HelloWorld!!!");
return null;
}
}
注:execute()是受核心配置文件struts.xml,包配置的中的action標籤中的method屬性指定的默認值。如果要更改方法名,只需自己自定義即可。
②實現Action接口 :Action接口中定義了五個常量和一個默認的execute()方法
* Action接口:提供了五個常量(五個邏輯接口的名稱)
* SUCCESS :成功
* ERROR :失敗
* LOGIN :登錄出錯頁面跳轉
* INPUT :表單校驗、類型轉換出現錯誤跳轉
* NONE :頁面不跳轉
*
* */
public class ActionImpl implements Action {
@Override
public String execute() throws Exception {
System.out.println("ActionImpl執行.....");
return NONE;
}
}
注: 返回的常量值要與struts.xml文件中<action>標籤的裏的<result>的name屬性值對應,這樣就可關聯跳轉
③繼承ActionSupport類 :它實現了Action接口和其他一系列的接口,功能強大,擼它就沒錯了。