ITeye博客

struts.xml 模块化管理

hdxiong · 2009-03-09 10:57 · SSH · Struts · freemarker · XML · JSP · 配置管理 · Markdown

先贴两段代码,在慢慢解释 (1)struts-user.xml [code="java"] UserAction_queryAll /user/user_list.jsp [/code] (2)struts.xml(引入了struts-user.xml) [code="java"] [/code] 1. 使用标签重用配置文件 (1)在Struts2中提供了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用标签引用这些配置文件。如上面的代码。 [b] 注意:用引用的xml文件也必须是完成的struts2的配置。实际上在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。 注意:struts.xml和struts-user.xml中标签中的name属性不能相同。道理很简单,标签中可以有多个标签,要通过name属性以示区别。 [/b] (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: .... 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 elements in your struts.xml interchangeably with 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)有很多时候一个可供很多使用,这时可以使用标签来定义全局的,代码见struts-user.xml。执行顺序:当一个Action返回的String没有相应的与之对应,Struts2就会查找全局的。 (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. /Error.jsp /Error.jsp Logon!input 3. 标签和标签结合 [b] (1)标签的作用域只是当前,也可以说是当前的xml文件;struts2不允许把struts-user.xml(通过引入到struts.xml)中的标签写在struts.xml中。 (2)如果struts-user.xml中的package继承自struts.xml中的package,则可以将struts-user.xml中的放在struts.xml中。然后struts-user.xml将此从struts.xml中继承过来。例如(将上面的两段代码简单修改): [/b] (1)struts-user.xml [code="java"] /user/user_list.jsp [/code] (2)struts.xml(引入了struts-user.xml) [code="java"] UserAction_queryAll [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。