1. 程式人生 > >(二)Struts2配置文件

(二)Struts2配置文件

程序 tro 是你 標記 har 內容 sun odin webapp

一、web.xml文件

web.xml配置文件是一種J2EE配置文件,決定servlet容器的HTTP元素需求如何進行處理。它嚴格來說不是一個Struts2 配置文件,但它是Struts2 運作所需要進行配置的文件。

正如前面所討論的,這個文件為每個web應用程序提供接入點。在部署描述符(web.xml)中,Struts2 應用程序的接入點將會定義為一個過濾器。因此我們將在web.xml裏定義一個FilterDispatcher類的接入點,而這個web.xml文件需要在WebContent/WEB-INF文件夾下創建。

如果你開始時沒有模板或工具(比如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>

註意,我們將Struts2 過濾器映射到 /* ,而不是 /*.action ,這意味著所有的url都會被Struts過濾器解析。當我們學到關於註解的章節時會對這個進行討論。

註意:自2.1.3版本開始,ActionContextCleanUp和FilterDispatcher都由StrutsPrepareAndExecuteFilter代替。

二、struts.xml文件

struts.xml文件包含有隨著Actions的開發你將要修改的配置信息。它可用於覆蓋應用程序的默認設置,例如: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="cn.w3cschool.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的話,你可以創建三個包,並將相關的Actions存儲到相應的包中。 <package>標簽具有以下屬性:

屬性描述
name(必需) 為package的唯一標識
extends 指定package繼承另一package的所有配置。通常情況下,我們使用struts-default作為package的基礎。
abstract 定義package為抽象的。如果標記為true,則package不能被最終用戶使用。
namespace Actions的唯一命名空間

<constant>標簽以及name和value屬性將用於覆蓋default.properties中定義的任一屬性,就像我們設置的struts.devMode屬性一樣。設置struts.devMode屬性允許我們在日誌文件中查看更多的調試消息。

我們定義<action>標簽對應於我們想要訪問的每個URL,並且使用execute()方法定義一個訪問相應的URL時將要訪問的類。

Results(結果)確定在執行操作後返回到瀏覽器的內容,而從操作返回的字符串應該是結果的名稱。 Results按上述方式配置,或作為“全局”結果配置,可用於包中的每個操作。 Results有

name

type

屬性可選,默認的name值是“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 Client中View和Model組件之間的鏈接,但在你99.99%的項目裏你不必使用這些設置。 struts-config.xml配置文件包含以下主要元素:

序號攔截器和說明
1 struts-config

這是配置文件的根節點。

2 form-beans

這是你將ActionForm子類映射到name的位置,你可以在struts-config.xml文件的其余部分,甚至在JSP頁面上,將這個name用作ActionForm的別名。

3 global forwards

此部分將你在webapp上的頁面映射到name,你可以使用這個name來引用實際頁面。這避免了對你網頁上的URL進行硬編碼。

4 action-mappings

這是你聲明表單處理程序的地方,也被稱為操作映射(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 Documentation。

四、struts.properties文件(初學非重點)

這個配置文件提供了一種機制來改變框架的默認行為。實際上,struts.properties配置文件中包含的所有屬性也可以在web.xml中配置使用init-param,以及在struts.xml配置文件中使用constant標簽。 但如果你想保持事件獨立以及保留更多struts細節,那麽你可以在WEB-INF/classes文件夾下創建這個文件。

struts.properties

文件中配置的值將覆蓋

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

這裏任何以#(hash)開頭的行都將被假定為註釋,並且它會被Struts 2默認忽略。

(二)Struts2配置文件