Struts2 配置檔

本章將帶你通過一個Struts2應用程式所需的基本配置。在這裏,我們將看到在一些重要的配置檔,將配置檔:web.xml ,struts.xml,struts-config.xml和struts.properties

使用web.xml和struts.xml的配置檔,並在前面的章節中,已經看到我們的例子中曾使用這兩個檔,讓我解釋以及其他檔。

web.xml 檔:

web.xml配置檔是一個J2EE的配置檔,決定如何處理元素的HTTP請求由servlet容器。嚴格來說它不是一個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下創建 

讓我們來看看在我們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.zaixian.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.devMode屬性。 Settingstruts.devMode屬性可以讓我們看到更多的調試消息,在日誌檔中。

我們定義動作標記對應的每一個URL,我們要訪問,我們定義了一個類的execute()方法,將訪問時,我們將訪問相應的URL。

結果決定得到執行動作後返回給流覽器。從操作返回的字串應該是一個結果的名稱。以上,或者作為一個“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.
6 plug-in
This section tells Struts where to find your properties files, which contain prompts and error messages

下麵是示例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

這裏井號(#)開頭的行會被假定作為注釋,它將被Struts 2忽略。


上一篇: Struts2 Hello World 實例 下一篇: Struts2快速入門