struts2配置文件详解

本章将带你经过一个Struts2应用程序所需的基本配置。在这里,咱们将看到在一些重要的配置文件,将配置文件:web.xml ,struts.xml,struts-config.xml和struts.propertieshtml

使用web.xml和struts.xml的配置文件,并在前面的章节中,已经看到咱们的例子中曾使用这两个文件,让我解释以及其余文件。java

这是第一个配置文件,将须要配置,若是没有一个模板或工具,可生成(如Eclipse或Maven2的)的帮助下开始。如下是web.xml文件中的内容,咱们用咱们的最后一个例子。node

<?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的过滤器。咱们将覆盖时,咱们将经过“注释”一章。web

struts.xml 文件:

struts.xml文件中包含的配置信息,将为动做开发被修改。这个文件能够被用来覆盖默认设置的应用程序,例如struts.devMode=false 和其余设置中定义的属性文件。这个文件能够被文件夹WEB-INF/classes下建立 apache

让咱们来看看在咱们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>容许分离和模块化的配置。这是很是有用的,当有一个大项目,项目被划分红不一样的模块。app

也就是说,若是项目有三个域 - 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.devMode属性。 Settingstruts.devMode属性可让咱们看到更多的调试消息,在日志文件中。yii

咱们定义动做标记对应的每个URL,咱们要访问,咱们定义了一个类的execute()方法,将访问时,咱们将访问相应的URL。webapp

结果决定获得执行动做后返回给浏览器。从操做返回的字符串应该是一个结果的名称。以上,或者做为一个“global”的结果,可包中的每个动做,结果被配置每次动做。结果有可选的名称和类型属性。默认名称的值是“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的核心2.2.3.jar文件default.properties文件。

struts-config.xml 文件:

在struts-config.xml 配置文件是在Web客户端组件的视图和模型之间的连接,但99.99%不会有触碰这些设置在项目中。基本配置文件包含如下主要内容:

SN
拦截&描述
1 struts-config
This is the root node of the configuration file.
2 form-beans
This is where you map your ActionForm subclass to a name. You use this name as an alias for your ActionForm throughout the rest of the struts-config.xml file, and even on your JSP pages.
3 global forwards
This section maps a page on your webapp to a name. You can use this name to refer to the actual page. This avoids hardcoding URLs on your web pages.
4 action-mappings
This is where you declare form handlers and they are also known as action mappings.
5 controller
This section configures Struts internals and rarely used in practical situations.

下面是示例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配置文件内包含的属性其实也能够被配置在web.xml中使用init-param中,以及在struts.xml的配置文件中使用恒定的标签。但若是喜欢保持独立和特定Struts,那么能够建立这个文件的文件夹下的WEB-INF/classes。

在这个文件中配置的值将覆盖默认值配置default.properties这是包含在struts2-core-x.y.z.jar 分布。有几个的属性,可能会考虑改变使用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