鍍金池/ 教程/ Java/ Struts2 配置文件
設(shè)置復(fù)選框的默認(rèn)值
<s:if>, <s:elseIf>, <s:else> 標(biāo)簽示例
下拉框示例
Struts2 &lt;s:bean&gt;標(biāo)簽示例
Struts2 &lt;s:date&gt;標(biāo)簽示例
<s:text>標(biāo)簽示例
Struts2的ActionError&ActionMessage示例
Struts2 MySQL數(shù)據(jù)庫(kù)訪問(wèn)
Struts2上傳多個(gè)文件例子
Struts2+Log4j集成
Struts 2 hello world (XML版本)
Struts2和Spring集成
Struts2 控件標(biāo)簽
Struts2 和 JSON 實(shí)例
Struts2 環(huán)境配置
Struts2 &lt;s:checkboxlist&gt;多個(gè)復(fù)選框例子
Struts2 注釋類型
在Struts2中配置靜態(tài)參數(shù)
Struts2 驗(yàn)證框架
Struts2 param 標(biāo)簽
Struts2 &lt;s:hidden&gt;隱藏值例子
Struts2下載文件實(shí)例
Struts2 &lt;sx:autocompleter&gt;自動(dòng)提示例子
Struts2 action標(biāo)簽
autocompleter自動(dòng)完成示例
Struts2 merge標(biāo)簽
Struts2 &lt;s:action&gt;標(biāo)簽示例
<s:property>標(biāo)簽示例
Struts2 &lt;s:sort&gt;標(biāo)簽示例
Struts2和Hibernate集成
自動(dòng)選擇下拉框示例
Struts2 結(jié)果和結(jié)果類型
Struts2 是什么?
如何刪除Struts2動(dòng)作的后綴擴(kuò)展名
Struts2 Hello World 實(shí)例
<s:action>標(biāo)簽示例
<s:bean>標(biāo)簽示例
Struts2 + Quartz調(diào)度集成實(shí)例
如何獲取ServletContext對(duì)象
Struts2 &lt;s:i18n&gt;標(biāo)簽示例
Struts2 append標(biāo)簽
i18n 或本地化示例
autocompleter + JSON 示例
Struts2獲取HttpServletRequest實(shí)例
updownselect 示例
Struts2 &lt;s:debug&gt;標(biāo)簽示例
Struts2重寫(xiě)攔截器參數(shù)
多個(gè)Struts配置文件示例
<s:i18n>標(biāo)簽示例
Struts2中文本地化問(wèn)題
文件上傳示例
Struts2 &lt;s:property&gt;標(biāo)簽實(shí)例
Struts2攔截器棧的例子
<s:merge>標(biāo)簽示例
Struts2 數(shù)據(jù)標(biāo)簽
Struts2 &lt;s:merge&gt;標(biāo)簽示例
復(fù)選框checkboxes 示例
Struts2 generator標(biāo)簽
如何配置Struts2全局資源包
key 屬性示例
Struts2 注釋
FilterDispatcher 和 StrutsPrepareAndExecuteFilter區(qū)別?
Struts 2 Hello World (注解版本)
Struts 2 + Log4j 集成實(shí)例
Struts2 值棧/OGNL
Struts2配置Action類的靜態(tài)參數(shù)
Struts2 本地化/國(guó)際化(i18n)
Chinese 本地化問(wèn)題
Struts2單選預(yù)選按鈕值
Struts2 include標(biāo)簽
Struts2 + Hibernate 集成實(shí)例
配置全局資源包
Struts2 異常處理
Struts2 &lt;s:set&gt;標(biāo)簽示例
Struts2 &lt;s:doubleselect&gt;示例
Struts2 iterator標(biāo)簽
optiontransferselect 示例
Struts2映射攔截動(dòng)作
head 示例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2命名空間配置和解釋
<s:param>標(biāo)簽示例
Struts2+Hibernate集成實(shí)例
Struts2 &lt;s:head&gt;示例
Struts2 &lt;s:push&gt;標(biāo)簽示例
Struts2文件上傳例子
Struts2 &lt;s:param&gt;標(biāo)簽示例
@ResultPath 注釋示例
Struts2 體系結(jié)構(gòu)
Struts2 execAndWait攔截器例子
Struts2 攔截器
Textarea - 文本域
Struts2教程
<s:push>標(biāo)簽示例
Struts2 文件上傳
Struts2 date 標(biāo)簽
Struts2 &lt;s:texttag&gt;標(biāo)簽示例
Struts2+Quartz2調(diào)度集成實(shí)例
Struts2開(kāi)發(fā)模式
Struts2 autocompleter+JSON例子
Struts2 類型轉(zhuǎn)換
<s:url>標(biāo)簽示例
Struts2 Ajax 標(biāo)簽
Struts2 bean 標(biāo)簽
<s:iterator>標(biāo)簽迭代示例
Hidden隱藏值示例
在Struts2獲取HttpServletResponse對(duì)象
Struts2 主題和模板
Struts2自動(dòng)選擇下拉框的值
Struts2下載文件實(shí)例
在Struts2中獲取 HttpServletRequest
級(jí)聯(lián)選擇示例
Struts2 &lt;sx:datetimepicker&gt;示例
Struts2 include(包含)多個(gè)配置文件
Struts2 &lt;s:select&gt;下拉框例子
Struts2 Action/動(dòng)作
Struts2 &lt;s:textarea&gt;文本域?qū)嵗?/span>
Struts2+Spring集成實(shí)例
Struts2 &lt;s:updownselect&gt;示例
Struts2 property 標(biāo)簽
Struts2 &lt;s:url&gt;標(biāo)簽示例
Struts2 表單標(biāo)簽
Struts2資源包使用示例
subset tag example
Struts 2 ActionError &amp; ActionMessage Example
Struts2 MVC架構(gòu)
Struts2 if/else標(biāo)簽
Struts2 &lt;s:generator&gt;標(biāo)簽示例
<s:include>標(biāo)簽示例
Struts2開(kāi)發(fā)者模式
<s:generator> 標(biāo)簽示例
復(fù)選框heckbox 示例
Struts2 text 標(biāo)簽
Radio 單選按鍵示例
Struts2 &lt;s:a&gt;標(biāo)簽示例
Struts2 key鍵屬性示例
Struts2設(shè)置多個(gè)復(fù)選框缺省值
Struts2 基于XML校驗(yàn)
Struts2 配置文件
Struts2 &lt;s:radio&gt;單選按鈕例子
Struts2 發(fā)送郵件Email
Struts2獲取HttpServletResponse實(shí)例
<s:sort>標(biāo)簽示例
Struts2 &lt;s:optiontransferselect&gt;示例
Struts2和Tiles集成
Struts2模型驅(qū)動(dòng)實(shí)例
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什么不同?
<s:append>標(biāo)簽示例
組合框示例
Struts2 &lt;s:password&gt;密碼示例
datetimepicker 日期選擇
Struts2 &lt;s:iterator&gt;迭代器標(biāo)簽示例
TextBox文檔示例
Struts2 url標(biāo)簽
資源包使用示例
Struts2本地化和國(guó)際化
Struts2 &lt;s:subset&gt;標(biāo)簽示例
Struts2 + Hibernate使用“Full Hibernate Plugin"插件集成
Struts2和JSON實(shí)例
Struts2注解示例
Struts2 &lt;s:checkbox&gt;復(fù)選框例子
Struts2快速入門(mén)
<s:date>標(biāo)簽示例
Struts2 set 標(biāo)簽
<s:set>標(biāo)簽示例
Struts2 命名空間配置和解釋
Struts 2 hello world (XML版本)
&nbsp;預(yù)選單選按鈕
<s:a>標(biāo)簽示例
Struts2+Spring+Hibernate集成實(shí)例
Struts2 &lt;s:combobox&gt;組合框的例子
Struts2 if,elseif,else標(biāo)簽示例
Struts2 @ResultPath注釋示例
struts2 &lt;s:append&gt;標(biāo)簽示例
Struts2 &lt;s:textfield&gt;文本示例
多文件上傳示例
使用Struts2動(dòng)作
<s:debug>標(biāo)簽示例
在Struts2中如何獲取ServletContext對(duì)象
Struts2 &lt;s:include&gt;標(biāo)簽示例
Password 示例

Struts2 配置文件

本章將帶你通過(guò)一個(gè)Struts2應(yīng)用程序所需的基本配置。在這里,我們將看到在一些重要的配置文件,將配置文件:web.xml ,struts.xml,struts-config.xml和struts.properties

使用web.xml和struts.xml的配置文件,并在前面的章節(jié)中,已經(jīng)看到我們的例子中曾使用這兩個(gè)文件,讓我解釋以及其他文件。

web.xml 文件:

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 文件:

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 文件:

在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 &lt;s:date&gt;標(biāo)簽示例下一篇:<s:include>標(biāo)簽示例