专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Struts2配置文件的方法

Struts2配置文件的方法

更新时间:2021-11-12 12:03:28 来源:星辉 浏览729次

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,这意味着所有 url 都将由 struts 过滤器解析。我们将在完成注释章节时介绍这一点。

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.tutorialspoint.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 配置文件都需要有正确的文档类型,如我们的小例子所示。<struts>  是根标签元素,在它下面我们使用 <package>标签声明不同的包。这里 <package>允许配置的分离和模块化。当您有一个大型项目并且项目被划分为不同的模块时,这非常有用。

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

该常数与名称和值的属性一起标签应该被用来覆盖任何定义下列属性的default.properties,就像我们刚刚成立struts.devMode财产。设置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% 的项目修改这些设置。出于这个原因,我们不会对此文件进行过多的详细介绍。如果您有兴趣,请查看struts2-core-2.2.3.jar 文件中的default.properties文件。

Struts-config.xml 文件

struts-config.xml 配置文件是 Web Client 中 View 和 Model 组件之间的链接,但您不必为 99.99% 的项目修改这些设置。

以下是示例 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.properties 文件

此配置文件提供了一种更改框架默认行为的机制。实际上,struts.properties配置文件中包含的所有属性也可以在web.xml 中使用init-param进行配置,也可以使用struts.xml配置文件中的 constant 标签进行配置。但是,如果您希望将这些内容分开并更加具体,那么您可以在文件夹WEB-INF/classes下创建此文件。

此文件中配置的值将覆盖struts2-core-xyzjar 发行版中包含的default.properties中配置的默认值。您可能会考虑使用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忽略。

通过上述相信大家对Struts2配置文件的方法已经有所了解,如果大家想了解更多相关知识,不妨来关注一下星辉的Struts2视频教程,课程内容详细,通俗易懂,适合小白学习,希望对大家能够有所帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>