struts2(基礎)--struts.xml配置文件詳解(多個package和自定義攔截器)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<package name="inter" namespace="/" extends="struts-default" >
	<interceptors>
	<!-- 1.註冊攔截器 -->
		<interceptor name="myInter3" class="cn.itcast.a_interceptor.MyInterceptor3"></interceptor>
	<!-- 2.註冊攔截器棧 -->
		<interceptor-stack name="myStack">
			<!-- 自定義攔截器引入(建議放在20個攔截器之前) -->
			<interceptor-ref name="myInter3">
				<!-- 指定哪些方法不攔截
				 <param name="excludeMethods">add,delete</param> -->
				 <!-- 指定哪些方法需要攔截 -->
				 <param name="includeMethods">add,delete</param>
			</interceptor-ref>
			<!-- 引用默認的攔截器棧(20個) -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</interceptor-stack>	
	</interceptors>
	<!-- 3.指定包中的默認攔截器棧 -->
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		<action name="Demo1Action_*" class="cn.itcast.a_interceptor.Demo1Action" method="{1}" >
			<!-- 爲Action單獨指定走哪個攔截器(棧) 
			<interceptor-ref name="myStack"></interceptor-ref>-->
			<result name="success" type="dispatcher" >/index.jsp</result>
		</action>
	</package>
    <!-- 配置多個package-->
	<package name="tag" namespace="/" extends="struts-default" >
		<action name="Demo2Action" class="cn.itcast.b_tag.Demo2Action" method="execute" >
			<result name="success" type="dispatcher" >/tag1.jsp</result>
		</action>
		<action name="Demo3Action" class="cn.itcast.b_tag.Demo3Action" method="execute" >
			<result name="success" type="dispatcher" >/tag2.jsp</result>
		</action>
	</package>

	
	<!-- i18n:國際化. 解決post提交亂碼 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<!-- 指定訪問action時的後綴名 
		http://localhost:8080/Struts2/hello/HelloAction.do
	-->
	<constant name="struts.action.extension" value="action"></constant>
	<!-- 指定struts2是否以開發模式運行
			1.熱加載主配置.(不需要重啓即可生效)
			2.提供更多錯誤信息輸出,方便開發時的調試
	 -->
	<constant name="struts.devMode" value="true"></constant>
	
	

	<!-- package:將Action配置封裝.就是可以在Package中配置很多action.
			name屬性: 給包起個名字,起到標識作用.隨便起.不能其他包名重複.
			namespace屬性:給action的訪問路徑中定義一個命名空間
			extends屬性: 繼承一個 指定包
			abstract屬性:包是否爲抽象的; 標識性屬性.標識該包不能獨立運行.專門被繼承
	  -->
	<package name="hello" namespace="/hello" extends="struts-default" >
		<!-- action元素:配置action類
				name屬性: 決定了Action訪問資源名.
				class屬性: action的完整類名
				method屬性: 指定調用Action中的哪個方法(配置方法)來處理請求
		 -->
		<action name="HelloAction" class="cn.huyouni.HelloAction" method="fun1" >
			<!-- result元素:結果配置 
					name屬性: 標識結果處理的名稱.與action方法的返回值對應.
					type屬性: 指定調用哪一個result類來處理結果,默認使用轉發.
					標籤體:填寫頁面的相對路徑
			-->
			<result name="success" type="dispatcher" >/hello.jsp</result>
		</action>
	</package>
	<!-- 引入其他struts配置文件 -->
	<include file="com/huyouni/struts.xml"></include>
</struts>

 

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