比如struts.xml文件里包含两个XML文件:global.xml和zhangsan.xml
struts.xml里:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <include file="global.xml"></include> 5 <include file="zhangsan.xml"></include> 6 </struts>
global.xml里:(global.xml继承struts-default)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <package name="global" namespace="/" extends="struts-default"> 5 6 <!-- 全局异常的处理 --> 7 <global-results> 8 <result name="error">/error.jsp</result> 9 </global-results> 10 <global-exception-mappings> 11 <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> 12 </global-exception-mappings> 13 14 </package> 15 </struts>
zhangsan.xml里:(zhangsan.xml继承global)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <package name="xsl" namespace="/" extends="global"> 5 6 <action name="testAction1" class="com.xsl.action.TestAction1"> 7 <!-- 8 type属性不写,默认则为type="dispatcher" 9 tyoe="redirect"表示重定向到jsp 10 type="chain"表示请求转发至另一个action 11 --> 12 <result name="test1" type="dispatcher">/success.jsp</result> 13 <result name="test2" type="redirect">/success.jsp</result> 14 <result name="test3" type="chain">testAction2_test1</result> 15 <result name="test4" type="redirectAction">testAction2_test2</result> 16 </action> 17 <!-- struts2.1这个版本要求action在跳转action时,不能写死action的名字 --> 18 <action name="testAction2_*" class="com.xsl.action.TestAction2" method="{1}"> 19 <result>/success.jsp</result> 20 </action> 21 22 </package> 23 </struts>