Struts2(2):Struts2中配置常量的3種方式及包含其他設定檔

來源:互聯網
上載者:User

Struts2中配置常量總共有3種方式:

① 通過struts.properties檔案。

② 通過struts.xml設定檔。

③ 通過Web應用的web.xml檔案。

Struts2的所有設定檔,包括struts-default.xml,struts-plugin.xml,甚至使用者自訂的、只要能被Struts2載入的設定檔,都可以使用常量配置的方式來配置Struts2常量。

如下struts.xml配置片段配置了一個常量,該常量即可替代struts.properties檔案中的配置屬性。

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts>    <constant name="struts.custom.i18n.resources" value="mess"/>    ...</struts>

上面代碼配置了一個常用屬性:struts.custom.i18n.resources,該屬性指定了應用所需的國際化資源檔的baseName為mess。

除此之外,當我們在web.xml檔案中配置StrutsPrepareAndExecuteFilter時也可配置Struts2常量,此時採用為StrutsPrepareAndExecuteFilter配置初始化參數的方式來配置Struts2常量,如下所示:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><init-param>   <param-name>struts.custom.i18n.resources</param-name>   <param-value>mess</param-value></init-param></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

通常推薦在struts.xml檔案中配置Struts2屬性,而不是在struts.properties中。之所以保留使用struts.properties配置屬性的方式,主要是為了保持與WebWork的向後相容性。

通常,Struts2架構按照如下搜尋順序載入Struts2常量。

⑴ struts-default.xml

⑵ struts-plugin.xml

⑶ struts.xml

⑷ struts.properties

⑸ web.xml

上面指定了Struts2架構搜尋常量的順序,如果在多個檔案中配置了同一個Struts2常量,則後一個檔案中配置的常量值會覆蓋前面檔案中配置的常量值。

在不同檔案中配置常量的方式是不一樣的,但不管在哪個檔案中,配置Struts2常量都需要指定兩個屬性:常量name和常量value,分別指定Struts2屬性名稱和屬性值。

包含其他設定檔:

在預設情況下,Struts2隻自動載入類載入路徑下的struts.xml、struts-default.xml和struts-plugin.xml三類檔案。但隨著應用規模的增大,系統中Action數量也大量增加,將導致struts.xml檔案變得非常臃腫。

為了避免struts.xml檔案過於龐大,提高struts.xml檔案的可讀性,我們可以將一個struts.xml檔案分解成多個設定檔,然後在struts.xml中包含其他設定檔。

下面的struts.xml檔案中就通過include手動匯入了一個設定檔:struts-part1.xml檔案,通過這種方式,就可以將Struts2的Action按照模組配置在多個設定檔中。

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts>    <include file="struts-part1.xml"/>    ...   </struts>

通過這種方式,Struts2能以一種模組化的方式來管理struts.xml設定檔。

被包含的struts-part1.xml檔案是標準的Struts2設定檔,一樣包含了dtd資訊、Struts2設定檔的根項目等資訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.