Struts.xml文件模块化管理 配置多个struts.xml文件

来源:http://blog.sina.com.cn/s/blog_6419bc670100i6kc.html

 

 

Struts.xml文件模块化管理 配置多个struts.xml文件(2010-04-19 14:47:33)

标签:

杂谈

 

struts2 多个配置文件
先贴两段代码,在慢慢解释

(1) struts-user.xml
Java代码
  1. <struts   
  2.     <package name="struts-user" extends="struts-default"   
  3.         <global-results>    
  4.             <result type="redirect-action">UserAction_queryAll</result>    
  5.         </global-results>    
  6.         <action name="UserAction_login" class="userAction" method="login"></action>    
  7.         <action name="UserAction_insert" class="userAction" method="insert"></action>    
  8.         <action name="UserAction_update" class="userAction" method="update"></action>    
  9.         <action name="UserAction_delete" class="userAction" method="delete"></action>    
  10.         <action name="UserAction_queryById" class="userAction" method="queryById"></action>     
  11.         <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action>    
  12.         <action name="UserAction_queryAll" class="userAction" method="queryAll"   
  13.             <result>/user/user_list.jsp</result>    
  14.         </action>    
  15.           </package   
  16. </struts 
<<span style="font-family:Courier New;"><span><span style="background-color: rgb(255, 255, 0);">struts</span></span>> <<span><span style="background-color: rgb(85, 255, 85);">package</span></span> name="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>-user" <span><span style="background-color: rgb(170, 255, 170);">extends</span></span>="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>-default"> <global-results> <result type="redirect-action">UserAction_queryAll</result> </global-results> <action name="UserAction_login" class="userAction" method="login"></action> <action name="UserAction_insert" class="userAction" method="insert"></action> <action name="UserAction_update" class="userAction" method="update"></action> <action name="UserAction_delete" class="userAction" method="delete"></action> <action name="UserAction_queryById" class="userAction" method="queryById"></action> <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action> <action name="UserAction_queryAll" class="userAction" method="queryAll"> <result>/user/user_list.jsp</result> </action> </<span><span style="background-color: rgb(85, 255, 85);">package</span></span>> </<span><span style="background-color: rgb(255, 255, 0);">struts</span></span></span>>


(2) struts.xml(引入了 struts-user.xml)
Java代码
  1. <struts   
  2.     <include file="struts-user.xml"></include>    
  3.     <package name="struts" extends="struts-default"></package   
  4. </struts 
<<span style="font-family:Courier New;"><span><span style="background-color: rgb(255, 255, 0);">struts</span></span>> <include file="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>-user.xml"></include> <<span><span style="background-color: rgb(85, 255, 85);">package</span></span> name="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>" <span><span style="background-color: rgb(170, 255, 170);">extends</span></span>="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>-default"></<span><span style="background-color: rgb(85, 255, 85);">package</span></span>> </<span><span style="background-color: rgb(255, 255, 0);">struts</span></span></span>>

1. 使用<include>标签重用配置文件

(1)在Struts2中提供了一个默认的 struts.xml文件,但如果 package、action、interceptors等配置比较多时,都放到一个 struts.xml文件不太容易维护。因此,就需要将 struts.xml文件分成多个配置文件,然后在 struts.xml文件中使用<include>标签引用这些配置文件。如上面的代码。

注意:用<include>引用的xml文件也必须是完成的struts2的配置。实际上<include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。

注意:struts.xml和struts-user.xml中<package></package>标签中的name属性不能相同。道理很简单,<struts></struts>标签中可以有多个<package></package>标签,要通过name属性以示区别。

(2)Apache Struts 2 Documentation: Can we break up a large struts.xml file into smaller pieces --> Yes, there are two approaches. We can include other struts.xml file from a bootstrap, or we can package a struts.xml files in a JAR. Or both.

<1>By Include:A typical struts.xml files will have one or more include elements:

< struts>
      <include file=" struts-default.xml"/>
      <include file="config-browser.xml"/>
      < package name="default" extends=" struts-default">
      ....
      </ package>
      <include file="other.xml"/>
</ struts>
The first include element tells the framework to load the struts-default.xml, which it will find in the struts2.jar file. The struts-default.xml file defines the "standard" interceptor and result definitions. You can put your own <include> elements in your struts.xml interchangeably with < package> elements. The configuration objects will be loaded in the order of appearance. The framework reads the configuration from top to bottom and adds objects as they are referenced.

<2>By JAR

A "module" can be added to an application by placing a struts.xml and related classes into a JAR on the classpath. FreeMarker and Velocity templates can also be provided by JAR, making it possible to distribution a module in a single, self-contained JAR that is automatically configured on startup.

2. 全局result(global-results)

(1)有很多时候一个<result>可供很多<action>使用,这时可以使用<global-results>标签来定义全局的<result>,代码见 struts-user.xml。执行顺序:当一个Action返回的String没有相应的<result>与之对应,Struts2就会查找全局的<result>。

(2)Apache Struts 2 Documentation: Global Results

Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result. If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.

<!-- Defining global results -->
<global-results>
      <result name="error">/Error.jsp</result>
      <result name="invalid.token">/Error.jsp</result>
      <result name="login" type="redirectAction">Logon!input</result>
</global-results>
3. <include>标签和<global-results>标签结合

(1)<global-results>标签的作用域只是当前<struts></struts>,也可以说是当前的xml文件;struts2不允许把struts-user.xml(通过<include>引入到struts.xml)中的<global-results>标签写在struts.xml中。
(2)如果struts-user.xml中的package继承自struts.xml中的package,则可以将struts-user.xml中的<global-results>放在struts.xml中。然后struts-user.xml将此<global-results>从struts.xml中继承过来。例如(将上面的两段代码简单修改):

(1) struts-user.xml
Java代码
  1. <struts   
  2.     <!-- 这里struts-user继承(extends)的是strutsstruts.xml中package的name属性值 -->    
  3.     <package name="struts-user" extends="struts"   
  4.             
  5.         <action name="UserAction_login" class="userAction" method="login"></action>    
  6.         <action name="UserAction_insert" class="userAction" method="insert"></action>    
  7.         <action name="UserAction_update" class="userAction" method="update"></action>    
  8.         <action name="UserAction_delete" class="userAction" method="delete"></action>    
  9.         <action name="UserAction_queryById" class="userAction" method="queryById"></action>     
  10.         <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action>    
  11.         <action name="UserAction_queryAll" class="userAction" method="queryAll"   
  12.             <result>/user/user_list.jsp</result>    
  13.         </action>    
  14.     </package   
  15. </struts 
<<span style="font-family:Courier New;"><span><span style="background-color: rgb(255, 255, 0);">struts</span></span>> <!-- 这里<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>-user继承(<span><span style="background-color: rgb(170, 255, 170);">extends</span></span>)的是<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>, 即<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>.xml中<span><span style="background-color: rgb(85, 255, 85);">package</span></span>的name属性值 --> <<span><span style="background-color: rgb(85, 255, 85);">package</span></span> name="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>-user" <span><span style="background-color: rgb(170, 255, 170);">extends</span></span>="<span><span style="background-color: rgb(255, 255, 0);">struts</span></span>"> <action name="UserAction_login" class="userAction" method="login"></action> <action name="UserAction_insert" class="userAction" method="insert"></action> <action name="UserAction_update" class="userAction" method="update"></action> <action name="UserAction_delete" class="userAction" method="delete"></action> <action name="UserAction_queryById" class="userAction" method="queryById"></action> <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action> <action name="UserAction_queryAll" class="userAction" method="queryAll"> <result>/user/user_list.jsp</result> </action> </<span><span style="background-color: rgb(85, 255, 85);">package</span></span>> </<span><span style="background-color: rgb(255, 255, 0);">struts</span></span></span>>

(2) struts.xml(引入了 struts-user.xml)
Java代码
  1. <struts   
  2.     <include file="struts-user.xml"></include>    
  3.     <package name="struts" extends="struts-default"   
  4.         <global-results>    
  5.             <result type="redirect-action">UserAction_queryAll</result>    
  6.         </global-results>    
  7.     </package   
  8. </struts 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值