本章將帶你通過(guò)一個(gè)Struts2應(yīng)用程序所需的基本配置。在這里,我們將看到在一些重要的配置文件,將配置文件:web.xml ,struts.xml,struts-config.xml和struts.properties
使用web.xml和struts.xml的配置文件,并在前面的章節(jié)中,已經(jīng)看到我們的例子中曾使用這兩個(gè)文件,讓我解釋以及其他文件。
web.xml配置文件是一個(gè)J2EE的配置文件,決定如何處理元素的HTTP請(qǐng)求由servlet容器。嚴(yán)格來(lái)說(shuō)它不是一個(gè)Struts2的配置文件,但它是Struts2的工作需要進(jìn)行配置的文件。
如前所述,這個(gè)文件為任何Web應(yīng)用程序提供了一個(gè)切入點(diǎn)。 Struts2 應(yīng)用程序的入口點(diǎn),將是一個(gè)部署描述符(web.xml)中定義的過(guò)濾器。因此,我們將定義在web.xml中的FilterDispatcher是類的項(xiàng)。需要?jiǎng)?chuàng)建的文件夾的WebContent/ WEB-INF下web.xml文件。
這是第一個(gè)配置文件,將需要配置,如果沒(méi)有一個(gè)模板或工具,可生成(如Eclipse或Maven2的)的幫助下開(kāi)始。以下是web.xml文件中的內(nèi)容,我們用我們的最后一個(gè)例子。
<?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>
請(qǐng)注意,我們Struts 2的過(guò)濾器映射為/*, /*.action這意味著所有的URL將被解析struts的過(guò)濾器。我們將覆蓋時(shí),我們將通過(guò)“注釋”一章。
struts.xml文件中包含的配置信息,將為動(dòng)作開(kāi)發(fā)被修改。這個(gè)文件可以被用來(lái)覆蓋默認(rèn)設(shè)置的應(yīng)用程序,例如struts.devMode=false 和其他設(shè)置中定義的屬性文件。這個(gè)文件可以被文件夾WEB-INF/classes下創(chuàng)建
讓我們來(lái)看看在我們struts.xml文件中創(chuàng)建的Hello World的例子在前面的章節(jié)中解釋。
<?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>根標(biāo)簽的元素,我們聲明不同的包使用<package>標(biāo)簽。 <package>允許分離和模塊化的配置。這是非常有用的,當(dāng)有一個(gè)大項(xiàng)目,項(xiàng)目被劃分成不同的模塊。
也就是說(shuō),如果項(xiàng)目有三個(gè)域 - business_applicaiton ,customer_application 和 staff_application,可以創(chuàng)建三個(gè)包和存儲(chǔ)相關(guān)的動(dòng)作,在適當(dāng)?shù)陌?。包裝標(biāo)簽具有以下屬性:
屬性 | 描述 |
---|---|
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屬性恒定的標(biāo)簽將被用于覆蓋default.properties中定義以下屬性,就像我們剛剛設(shè)置struts.devMode屬性。 Settingstruts.devMode屬性可以讓我們看到更多的調(diào)試消息,在日志文件中。
我們定義動(dòng)作標(biāo)記對(duì)應(yīng)的每一個(gè)URL,我們要訪問(wèn),我們定義了一個(gè)類的execute()方法,將訪問(wèn)時(shí),我們將訪問(wèn)相應(yīng)的URL。
結(jié)果決定得到執(zhí)行動(dòng)作后返回給瀏覽器。從操作返回的字符串應(yīng)該是一個(gè)結(jié)果的名稱。以上,或者作為一個(gè)“global”的結(jié)果,可包中的每一個(gè)動(dòng)作,結(jié)果被配置每次動(dòng)作。結(jié)果有可選的名稱和類型屬性。默認(rèn)名稱的值是“success”。
隨著時(shí)間的推移,struts.xml文件可以逐步擴(kuò)展,打破它包是模塊化的方式之一,但Struts提供了另一種模塊化struts.xml文件??梢詫⑽募指顬槎鄠€(gè)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>
其他的配置文件,我們還沒(méi)有涉及到在struts-default.xml中。這個(gè)文件包含了Struts的標(biāo)準(zhǔn)配置設(shè)置,就不必去觸摸項(xiàng)目的這些99.99%設(shè)置。出于這個(gè)原因,我們不打算對(duì)這個(gè)文件介紹太多。如果有興趣,不妨看看到struts2的核心2.2.3.jar文件default.properties文件。
在struts-config.xml 配置文件是在Web客戶端組件的視圖和模型之間的鏈接,但99.99%不會(huì)有觸碰這些設(shè)置在項(xiàng)目中?;九渲梦募韵轮饕獌?nèi)容:
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"/> 上一篇:Struts2 <s:date>標(biāo)簽示例下一篇:<s:include>標(biāo)簽示例