struts.xml文件:

struts.xml文件中包含的配置信息,你将修改所采取的措施的开发。这个文件可以被用来覆盖默认设置的应用程序,例如struts.devMode=false和其他设置中定义的属性文件。这个文件可以创建文件夹下的WEB-INF/classes。

让我们一起来看看我们在struts.xml文件中创建Hello World的例子在前面的章节中解释。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">
     
      <action name="hello" 
            class="com.yiibai.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
      <-- more actions can be listed here -->

   </package>
   <-- more packages can be listed here -->

</struts>

首先要注意的是DOCTYPE。所有的Struts配置文件中需要有正确的DOCTYPE,在我们的小例子所示。<struts>是根标记的元素,我们声明使用不同的包<package>标签。其中,<package>允许分离和模块化的配置。这是非常有用的,当你有一个大的项目,项目被划分成不同的模块。

再说了,如果项目有三个域名 - business_applicaiton,customer_application和staff_application,可以创建三个包,在适当的包装和存储相关的行动。包装标签具有以下属性:

属性描述
name (required) The unique identifier for the package
extends Which package does this package extend from? By default, we use struts-default as the base package.
abstract If marked true, the package is not available for end user consumption.
namesapce Unique namespace for the actions

常量标签name和value属性将被用来覆盖default.properties中定义的属性,就像我们刚刚成立struts.devModeproperty。设置struts.devMode属性,让我们看到了更多的调试信息,日志文件中。

我们定义动作标记对应的每一个URL,我们想访问我们定义了一个类的execute()方法,将访问时,我们将访问相应的URL。
结果确定什么被返回到浏览器的一个动作后执行。从操作返回的字符串应该是一个结果的名称。结果如上配置的每次动作,或作为一个“全局”的结果,在包中的每一个动作可。结果有可选的名称和类型的属性。默认名称值是“成功”。

随着时间的推移,struts.xml文件可以做大,打破它包的模块化是一种方式,但Struts提供了另一种模块化的struts.xml文件。你可以将档案分割成多个XML文件,并将其导入以下方式。

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

我们还没有涉及到的其他配置文件是struts-default.xml中。这个文件包含了Struts标准配置设置,在你的项目的99.99%就不会碰这些设置。出于这个原因,我们不打算在这个文件太多的细节。如果有兴趣,不妨看看在default.properties文件中struts2的核心-2.2.3.jar文件。

原文地址:https://www.cnblogs.com/borter/p/9502134.html