目录

Struts2 - 配置( Configuration)

本章将向您介绍Struts 2应用程序所需的基本配置。 在这里,我们将看到可以使用web.xml, struts.xml, strutsconfig.xmlstruts.properties等少数重要配置文件进行配置的内容。

老实说,您可以通过使用web.xmlstruts.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 = "http://www.w3.org/2001/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/*.action ,这意味着struts过滤器将解析所有URL。 当我们通过Annotations章节时,我们将介绍这一点。

Struts.xml文件

struts.xml文件包含在开发操作时将要修改的配置信息。 此文件可用于覆盖应用程序的默认设置,例如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"
   "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.iowiki.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”标记声明不同的包。 这里“包”允许配置的分离和模块化。 当您拥有一个大型项目并将项目划分为不同的模块时,这非常有用。

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

包标签具有以下属性 -

Sr.No 属性和描述
1

name (required)

包的唯一标识符

2

extends

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

3

abstract

如果标记为true,则该程序包不可供最终用户使用。

4

namespace

操作的唯一命名空间

应使用constant标记以及name和value属性来覆盖default.properties定义的任何以下属性,就像我们设置struts.devMode属性一样。 设置struts.devMode属性允许我们在日志文件中查看更多调试消息。

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

结果确定执行操作后返回浏览器的内容。 从操作返回的字符串应该是结果的名称。 结果按上述操作配置,或作为“全局”结果配置,可用于包中的每个操作。 结果具有可选的nametype属性。 默认名称值为“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"
   "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%的项目触摸这些设置。 出于这个原因,我们不会对此文件进行太多细节处理。 如果您有兴趣,请查看struts2-core-2.2.3.jar文件中的default.properties文件。

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

global forwards

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

4

action-mappings

这是您声明表单处理程序的位置,它们也称为操作映射。

5

controller

本节配置Struts内部,在实际情况中很少使用。

6

plug-in

本节告诉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"
   "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<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-paramweb.xml配置,以及使用struts.xml配置文件中的constant标记。 但是,如果您希望将事物分开并且更具特定的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忽略。

↑回到顶部↑
WIKI教程 @2018