Struts 2 - 配置文件

本章将指导您完成 Struts 2 应用程序所需的基本配置.在这里,我们将看到可以使用少量重要配置文件(例如 web.xml,struts.xml,strutsconfig.xml struts.properties)配置的内容.

老实说,您可以通过使用 web.xml struts.xml 配置文件开始工作(正如您在前一章中已经看到的那样)我们的例子使用这两个文件工作的地方).但是,据您所知,我们还将解释其他文件.

web.xml文件

web.xml配置文件是J2EE配置确定servlet容器如何处理HTTP请求元素的文件.它不是严格意义上的Struts2配置文件,但它是一个需要配置为Struts2才能工作的文件.

如前所述,该文件为任何Web应用程序提供了一个入口点. Struts2应用程序的入口点将是部署描述符(web.xml)中定义的过滤器.因此,我们将在web.xml中定义 FilterDispatcher 类的条目.需要在文件夹 WebContent/WEB-INF 下创建web.xml文件.

这是您需要配置的第一个配置文件在没有生成它的模板或工具(例如Eclipse或Maven2)的帮助下启动.

以下是我们在上一个示例中使用的web.xml文件的内容.

<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "https://img01.yuandaxia.cn/Content/img/tutorials/struts_2/XMLSchema-instance"
   xmlns = "http://java.sun.com/xml/ns/javaee" 
   xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id = "WebApp_ID" version = "3.0">
   
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

</web-app>


请注意,我们将Struts 2过滤器映射到/* ,而不是/* .action 这意味着所有网址都将由struts过滤器解析.我们将通过Annotations章节介绍这一点.

Struts.xml文件

struts.xml file包含在开发操作时将要修改的配置信息.此文件可用于覆盖应用程序的默认设置,例如 struts.devMode = false 以及属性文件中定义的其他设置.该文件可以在文件夹 WEB-INF/classes 下创建.

让我们看一下我们在Hello World示例中创建的struts.xml文件在前一章中解释过.

<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "https://img01.yuandaxia.cn/Content/img/tutorials/struts_2/404.html
<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
     
      <action name = "hello" 
         class = "com.it1352.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>是根标签元素,在我们使用< p声明了不同的包ackage&GT;标签.这里< package>允许分离和模块化配置.当你有一个大型项目并且项目被分成不同的模块时,这非常有用.

例如,如果你的项目有三个域 -  business_application,customer_application和staff_application,那么你可以创建三个在相应的包中打包并存储相关的操作.

包标签具有以下属性 :

Sr.No属性&说明
1

名称(必填)

包裹的唯一标识符

2

延伸

这个包从哪个包扩展而来?默认情况下,我们使用struts-default作为基础包.

3

摘要

如果标记为true,则包不适用于最终用户消费.

4

命名空间

操作的唯一命名空间

常量标签以及名称和value属性应该用于覆盖 default.properties 中定义的任何以下属性,就像我们设置 struts.devMode 属性一样.设置 struts.devMode 属性允许我们在日志文件中查看更多调试消息.

我们定义 action 标记对应于每个URL我们想访问并使用execute()方法定义一个类,只要我们访问相应的URL就会被访问.

结果确定执行操作后返回浏览器的内容.从操作返回的字符串应该是结果的名称.结果按上述操作配置,或作为"全局"结果配置,可用于包中的每个操作.结果具有可选的名称类型属性.默认名称值为"success".

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"
   "https://img01.yuandaxia.cn/Content/img/tutorials/struts_2/404.html

<struts>
   <include file="my-struts1.xml"/>
   <include file="my-struts2.xml"/>
</struts>


我们未涉及的其他配置文件是struts-default.xml.此文件包含标准配置设置Struts,您不必为99.99%的项目触摸这些设置.因此,我们不会对此文件进行过多详细介绍.如果您有兴趣,请查看默认设置. struts2-core-2.2.3.jar文件中提供的属性文件.

Struts-config.xml文件

struts -config.xml配置文件是Web客户端中View和Model组件之间的链接,但您不必为99.99%的项目触摸这些设置.

配置文件基本上包含以下主要元素 :

Sr .No拦截器&说明
1

struts-config

这是配置文件的根节点.

2

form-beans

这是将ActionForm子类映射到名称的位置.在整个strutsconfig.xml文件的其余部分,甚至在JSP页面上,都使用此名称作为ActionForm的别名.

3

全球远期

此部分将您的webapp上的页面映射到名称.您可以使用此名称来引用实际页面.这样可以避免在网页上对网址进行硬编码.

4

动作映射

这是您声明表单处理程序的地方,它们也称为动作映射.

5

控制器

此部分配置Struts内部,在实际情况下很少使用.

6

插件

本节告诉Struts在哪里可以找到属性文件,其中包含提示和错误消息

以下是示例struts-config.xml文件 :

<?xml version = "1.0" Encoding = "ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
   "https://img01.yuandaxia.cn/Content/img/tutorials/struts_2/404.html

<struts-config>

   <!-- ========== Form Bean Definitions ============ -->
   <form-beans>
      <form-bean name = "login" type = "test.struts.LoginForm" />
   </form-beans>

   <!-- ========== Global Forward Definitions ========= -->
   <global-forwards>
   </global-forwards>

   <!-- ========== Action Mapping Definitions ======== -->
   <action-mappings>
      <action
         path = "/login"
         type = "test.struts.LoginAction" >

         <forward name = "valid" path = "/jsp/MainMenu.jsp" />
         <forward name = "invalid" path = "/jsp/LoginView.jsp" />
      </action>
   </action-mappings>

   <!-- ========== Controller Definitions ======== -->
   <controller contentType = "text/html;charset = UTF-8"
      debug = "3" maxFileSize = "1.618M" locale = "true" nocache = "true"/>

</struts-config>


有关struts-config.xml文件的更多详细信息,请查看struts文档.

Struts.properties文件

此配置文件提供了一种更改框架默认行为的机制.实际上, struts.properties 配置文件中包含的所有属性也可以使用 init-param web.xml 中配置,如好好使用 struts.xml 配置文件中的常量标记.但是,如果您希望将事物分开并且更具特定的struts,那么您可以在文件夹 WEB-INF/classes 下创建此文件.

值在此文件中配置的将覆盖在 default.properties 中配置的默认值,该默认值包含在struts2-core-xyzjar发行版中.您可以考虑使用 struts.properties 文件更改一些属性 :

### When set to true, Struts will act much more friendly for developers
struts.devMode = true

### Enables reloading of internationalization files
struts.i18n.reload = true

### Enables reloading of XML configuration files
struts.configuration.xml.reload = true

### Sets the port that the server is run on
struts.url.http.port = 8080


此处任何以 hash (#)开头的行都将被视为评论,它将被 Struts 2 忽略.