溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Struts2怎么配置文件

發(fā)布時間:2022-09-29 11:29:07 來源:億速云 閱讀:122 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Struts2怎么配置文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Struts2怎么配置文件”吧!

web.xml 文件

web.xml 配置文件是一個 J2EE 配置文件,它決定了servlet容器如何處理 HTTP 請求的元素。它不是嚴(yán)格意義上的 Struts2 配置文件,而是 Struts2 需要配置才能工作的文件。

如前所述,該文件為任何 Web 應(yīng)用程序提供了一個入口點(diǎn)。Struts2 應(yīng)用程序的入口點(diǎn)將是部署描述符(web.xml)中定義的過濾器。因此,我們將在 web.xml 中定義FilterDispatcher類的條目 。需要在文件夾WebContent/WEB-INF下創(chuàng)建 web.xml 文件。

如果您在沒有生成它的模板或工具(例如 Eclipse 或 Maven2)的幫助下啟動,這是您需要配置的第一個配置文件。

以下是我們在上一個示例中使用的 web.xml 文件的內(nèi)容。

<?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文件中包含的配置信息,作為行動的開發(fā),你會被修改。此文件可用于覆蓋應(yīng)用程序的默認(rèn)設(shè)置,例如struts.devMode = false和在屬性文件中定義的其他設(shè)置。該文件可以在文件夾WEB-INF/classes下創(chuàng)建。

讓我們看看我們在前一章解釋的 Hello World 示例中創(chuàng)建的 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>  是根標(biāo)簽元素,在它下面我們使用 <package>標(biāo)簽聲明不同的包。這里 <package>允許配置的分離和模塊化。當(dāng)您有一個大型項(xiàng)目并且項(xiàng)目被劃分為不同的模塊時,這非常有用。

例如,如果您的項(xiàng)目具有三個域 - business_application、customer_application 和 staff_application,那么您可以創(chuàng)建三個包并將關(guān)聯(lián)的操作存儲在適當(dāng)?shù)陌小?/p>

該常數(shù)與名稱和值的屬性一起標(biāo)簽應(yīng)該被用來覆蓋任何定義下列屬性的default.properties,就像我們剛剛成立struts.devMode財(cái)產(chǎn)。設(shè)置struts.devMode屬性可以讓我們在日志文件中看到更多的調(diào)試信息。

我們定義了對應(yīng)于我們想要訪問的每個 URL 的動作標(biāo)簽,并且我們定義了一個帶有 execute() 方法的類,當(dāng)我們訪問相應(yīng)的 URL 時,它將被訪問。

結(jié)果決定了執(zhí)行操作后返回給瀏覽器的內(nèi)容。操作返回的字符串應(yīng)該是結(jié)果的名稱。結(jié)果按上述配置,或作為“全局”結(jié)果,可用于包中的每個操作。結(jié)果具有可選的名稱和類型屬性。默認(rèn)名稱值為“成功”。

Struts.xml 文件會隨著時間的推移而變大,因此通過包將其分解是模塊化的一種方法,但Struts提供了另一種模塊化 struts.xml 文件的方法。您可以將文件拆分為多個 xml 文件并按以下方式導(dǎo)入它們。

<?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 的標(biāo)準(zhǔn)配置設(shè)置,您不必為 99.99% 的項(xiàng)目修改這些設(shè)置。出于這個原因,我們不會對此文件進(jìn)行過多的詳細(xì)介紹。如果您有興趣,請查看struts2-core-2.2.3.jar 文件中的default.properties文件。

Struts-config.xml 文件

struts-config.xml 配置文件是 Web Client 中 View 和 Model 組件之間的鏈接,但您不必為 99.99% 的項(xiàng)目修改這些設(shè)置。

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

此配置文件提供了一種更改框架默認(rèn)行為的機(jī)制。實(shí)際上,struts.properties配置文件中包含的所有屬性也可以在web.xml 中使用init-param進(jìn)行配置,也可以使用struts.xml配置文件中的 constant 標(biāo)簽進(jìn)行配置。但是,如果您希望將這些內(nèi)容分開并更加具體,那么您可以在文件夾WEB-INF/classes下創(chuàng)建此文件。

此文件中配置的值將覆蓋struts2-core-xyzjar 發(fā)行版中包含的default.properties中配置的默認(rèn)值。您可能會考慮使用struts.properties文件更改幾個屬性

### When set to true, Struts will act much more friendly for developersstruts.devMode = true### Enables reloading of internationalization filesstruts.i18n.reload = true### Enables reloading of XML configuration filesstruts.configuration.xml.reload = true### Sets the port that the server is run onstruts.url.http.port = 8080

這里任何以hash (#)開頭的行都將被假定為注釋,它會被Struts 2忽略。

到此,相信大家對“Struts2怎么配置文件”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI