Struts2 <include>元素:包含其他的配置文件

  • 内容
  • 评论
  • 相关

在 Web 项目开发中,如果业务逻辑非常复杂,就避免不了编写大量的 Action 对象来处理请求,同时,配置文件 struts.xml 也需要配置大量的 Action 信息,那么配置文件的代码就过于庞大。Struts2 针对这一问题,提供了一种分解的配置方法,其原理是将配置文件分解成多个配置文件,再将其整合到一个配置文件中。Struts2 配置文件提供了 <include> 元素,用于包含其他的配置文件,但这些文件必须是标准的 Struts2 配置文件。

语法:

<struts>
  <include file="file"/>
  ……..
  <include file="file"/>
</struts>

参数说明:

  • file属性的值就是被包含的文件的路径。

技巧:如果被包含的配置文件在一个包或目录中,而不在  classpath 的根目录下,可以使用“/”进行导航。

示例

本示例应用 <include> 元素中的属性 file 将 user.xml、book.xml、manager.xml 等配置文件包含到 Struts2 配置文件,关键代码如下:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <include file="user.xml"/>
  <include file="book.xml"/>
  <include file="manager.xml"/>
  <include file="/util/pojo.xml"/>
  <include file="/com/lyq/admin/admin.xml"/>
</struts>

本文标题:Struts2 <include>元素:包含其他的配置文件

本文地址:http://www.hosteonscn.com/6399.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注