亚洲男人天堂av,日韩av在线一区二区,日韩欧美精品在线不卡http://www.aygfsteel.com/AndyZhang/category/49329.htmlwelcome to java worldzh-cnMon, 08 Aug 2011 07:29:35 GMTMon, 08 Aug 2011 07:29:35 GMT60Struts-config.xml配置文件講解http://www.aygfsteel.com/AndyZhang/archive/2011/08/08/356017.htmlSkyDreamSkyDreamMon, 08 Aug 2011 06:05:00 GMThttp://www.aygfsteel.com/AndyZhang/archive/2011/08/08/356017.htmlstruts的核心是struts-config.xml配置文件,在這個文件里描述了所有的struts組件。在這里包括配置主要的組件及次要的組件,下面是struts-config.xml包含主要元素的內(nèi)容:

一、    struts-config.xml的主要元素:
<?xml version=”1.0” encoding=”iso-885Array-1”?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
   
   <data-sources>
       <data-source>
       </data-source>
   </data-sources>

   <form-beans>
       <form-bean / >
   </form-beans>

   <global-forwards>
       <forward / >
   </global-forwards>

   <action-mappings>
       <action / >
   </action-mappings>

   <controller / >

   <message-resources / >

   <plug-in />

</struts-config>
注意:以上各元素的順序是非常重要的,你的struts-config.xml配置文件必須按照這個順序進(jìn)行配置,否則在你的容器啟動的時候就會出錯。

二、    struts-config.xml的子元素:
1.<icon / >子元素
   它包含<small-icon / >及<large-icon / >,它的作用是圖形化其父元素,<small-icon/>的內(nèi)容是一個16x16的圖像文件,而<large-icon/>的內(nèi)容是一個32x32的圖像文件。如下例子:
   <icon>
<small-icon>
              /images/smalllogo.gif
</small-icon>
<large-icon>
    /images/largelogo.gif
</large-icon>
</icon>
2.<display-name / >子元素
   它提供對父元素的短文字(short textual)描述信息,如下:
   <display-name>
           short textual discription of its parent element
   </display-name>
3.<description / >子元素
   它提供對父元素的完全(full-length textual)的描述信息,如下:
<description>
full-length textual discription of its parent element
</description>
4.<set-property / >子元素
       它用來設(shè)置它的父元素中設(shè)定的javabean的屬性值,它一般用在指定的genericdatasource 屬性,擴展的actionmappings以及擴展的 global forwards。如下:
       <set-property 
           property="name of bean property"       
value="value of bean property" />
         例如:
         <set-property property="driverclass" value="org.gjt.mm.mysql.driver" />
        <set-property property="user" value="admin"/>
        <set-property property="maxcount" value="4"/>
<set-property property="mincount" value="2"/> 
<set-property property="password" value=""/> 
<set-property property="url" value="jdbc:mysql://localhost:3306/struts"/>

三、    配置jdbc數(shù)據(jù)源
其配置形式如下:
<data-sources>
<data-source>
<set-property property="driverclass" value="fully qualified path of jdbc driver"/>
<set-property property="url" value="data source url"/>
<set-property property=”mincount” value="the minimum number of connections to open"/>
<set-property property="password" value="the password used to create connections"/>
<set-property property="user" value="the username used to create connections"/>
</data-source>
</data-sources>
<data-source>的屬性及其描述信息如下:
屬  性    描 述 信 息
key    綁定在servletcontext上的datasource實例的索引鍵,若不設(shè)定則缺省為action.data_source_key,如果在應(yīng)用程序中有多于一個的datasource,則必須設(shè)置key的值。
driverclass    所用的jdbc驅(qū)動類(必須的)如:com.microsoft.jdbc.sqlserver.sqlserverdriver
url    所用的jdbc的url(必須的)如:jdbc:microsoft:sqlserver://xg088:1433
maxcount    同時打開的最大連結(jié)數(shù),缺省值為2(可選的)
mincount    同時打開的最小連結(jié)數(shù),缺省值為1(可選的)
user    連結(jié)到數(shù)據(jù)庫的用戶名(必須的)
password    連結(jié)到數(shù)據(jù)庫的密碼(必須的)
description    關(guān)于datasource的描述信息(可選的)
readonly    如果設(shè)為true,則表示該連結(jié)是只讀的,缺省為false。(可選的)
logintimeout    創(chuàng)建連結(jié)的最大允許時間,以秒為單位。(可選的)
autocommit    如果為true,則每次execute之后會強制回滾。缺省為true。(可選的)
舉例說明:
<data-sources>
    <data-source>
        <set-property property=”key” value=” value="wiley_data_source" />
<set-property property="driverclass" value="org.gjt.mm.mysql.driver" />
<set-property property="url" value="jdbc:mysql://localhost/wileyusers" />
<set-property property="maxcount" value="5"/>
<set-property property="mincount" value="1"/>
<set-property property="user" value="sa"/>
<set-property property="password" value="yourpassword"/>
</data-source>
</data-sources>

四、    配置formbean
<form-bean / >用來定義將要綁定到action的formbean的實例。語法如下:
<form-beans>
<form-bean name="name used to uniquely identify a formbean"
            type=”fully qualified class name of formbean"/>
         </form-beans>
例:
<form-beans>
<form-bean name="lookupform" type="wiley.lookupform" />
</form-beans>

五、    配置全局轉(zhuǎn)發(fā)
全局轉(zhuǎn)發(fā)可以定義幾個<forward/>子元素,struts首先會在<action-mappings>元素中找對應(yīng)的<forward>,若找不到,則到全局轉(zhuǎn)發(fā)配置中找。語法如下:
<global-forwards>
<forward name="unique target identifier" 
path="context-relative path to targetted resource "/>
</global-forwards>
除了name及path屬性之外,還有一個redirect屬性,如果redirect設(shè)為true的時候,則用httpservletresponse.sendredirect()方法,否則用requestdispatcher.forward()方法,缺省為false。
注:如果為true,則用httpservletresponse.sendredirect()方法,此時存儲在原來的httpservletrequest中的值將會丟失。
例子:
<global-forwards>
<forward name="success" path="/welcome.jsp"/>
<forward name="failure" path="/index.jsp"/>
</global-forwards>
六、    配置<action-mappings>
它可以定義幾個<action / >子元素,它主要是定義action實例到actionservlet類中,語法如下:
<action-mappings>
<action path="context-relative path mapping action to a request"
type="fully qualified class name of the action class"
name="the name of the form bean bound to this action">
<forward name="forwardname1" path="context-relative path"/>
<forward name="forwardname2" path="context-relative path"/>
</action>
</action-mappings>
<action/>屬性及其描述信息如下:
屬  性    描 述 信 息
path    在瀏覽器的url中輸入的字符(必須的)
type    連結(jié)到本映射的action的全稱(可選的)
name    與本操作關(guān)聯(lián)的action bean在<form-bean/>中定義name名(可選的)
scope    指定actionform bean的作用域(session和request),缺省為session。(可選的)
input    當(dāng)bean發(fā)生錯誤時返回的控制。(可選的)
classname    指定一個調(diào)用這個action類的actionmapping類的全名。缺省用org.apache.struts.action.actionmapping,(可選的)
forward    指定處理相應(yīng)請求所對應(yīng)的jsp頁面。(可選的)
include    如果沒有forward的時候,它起forward的作用。(可選的)
validate    若為true,則會調(diào)用actionform的validate()方法,否則不調(diào)用,缺省為true。(可選的)
例子:
<action-mappings>
<action path="/lookupaction" 
type="wiley.lookupaction"
name="lookupform"
scope="request"
validate="true"
input="/index.jsp">
<forward name="success" path="/quote.jsp"/>
<forward name="faliue" path="/index.jsp"/>
</action>
</action-mappings>

七、    配置requestprocessor
在struts-config.xml文件中用<controller/>子元素來定義requestprocessor,其語法格式如下:
<controller processorclass="fully qualified class name" />
<controller />元素屬性及其描述信息如下:
屬  性    描  述
processorclass    指定自定義的requestprocessor類的全名
buffersize    指定用來下載所用的緩存大小。缺省是40Array6字節(jié)。
contenttype    定義response文本類型,缺省是text/html
debug    定義當(dāng)前系統(tǒng)的除錯級別,缺省是0
locale    如果是true,則在用戶的session中存放locale對象,缺省為true
maxfilesize    指定下載文件最大的大小。缺省是250m
multipartclass    指定去代替org.apache.struts.upload.diskmultipartrequesthandler類的類的全名。
nocache    如果是true,則會關(guān)閉每個response的緩存功能。缺省是false
tempdir    指定上載文件所用的臨時目錄。缺省值由容器決定
例子:
① <controller processorclass="wiley.wileyrequestprocessor" />
② <controller
    contenttype="text/html;charset=utf-8"
    debug="3"
    locale="true"
    nocache="true"
    processorclass="org.apache.struts.action.requestprocessor"/>
八、    配置message resources
在struts-config.xml文件中用<message-resources />元素來定義消息資源。其語法如下:
       <message-resources  parameter="wiley.applicationresources"/>
<message-resources />元素屬性及其描述信息如下:
屬  性    描  述
parameter    給定資源文件全名
classname    定義處理消息資源的類名的全名,缺省是org.apache.struts.config.messageresourcesconfig
factory    定義messageresourcesfactory類的全名,缺省是org.apache.struts.util.property.messageresourcesfacotry
key    定義綁定在這個資源包中的servletcontext的屬性主鍵,缺省值是action.messages_key.
null    如果為true,則找不到消息key時,則返回null,缺省是true.
例子:
① <message-resources parameter="wiley.applicationresources"/>
② <message-resources
    parameter="storefrontmessageresources"
    null="false"/>
<message-resources
    key="image_resource_key"
    parameter="storefrontimageresources"
    null="false"/>
注意:設(shè)定key的目的如下:
<html:img altkey="navbar.home.image.alt" bundle="image_resource_key" pagekey="navbar.home.image" width="125" height="15" border="0"/>
這里說明要到storefrontimageresources.properties資源文件中找主鍵值是”navbar.home.image”所對應(yīng)的值。
這里storefrontimageresources.properties的內(nèi)容如下:
……
navbar.home.image=/images/home.gif
navbar.home.image.alt=home
……
此處navbar.home.image.alt說明的和<img alt=”home”……/>一樣。
九、    配置plug-in
配置plug-in如下:
<plug-in classname="wiley.wileyplugin"/>
也可如下:
<plug-in classname="com.oreilly.struts.storefront.service.memory.storefrontmemorydatabaseplugin">
  <set-property property="pathname" value="/web-inf/database.xml"/>
</plug-in>
完整配置實例

       本小節(jié)舉例說明struts-config.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <data-sources />
 <form-beans>
    <form-bean name="UserForm"
                   type="com.amigo.struts.form.user.UserForm" />
 </form-beans>
 
 <global-exceptions />
 <global-forwards />
 <action-mappings>
<action
                path="/userAction"
                type="com.amigo.struts.action.UserAction"
                name="UserForm"
                scope="request"
                validate = "false"
                parameter="method" >
             <forward name="error" path="/user/error.jsp" />
                  <forward name="success" path="/user/success.jsp"/>
                   <forward name="add" path="/user/addUser.jsp"/>
                   <forward name="update" path="/user/updateUser.jsp"/>
                   <forward name="list" path="/user/userList.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.amigo.struts. ApplicationResources " />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="false" /> 
</plug-in>
</struts-config>


SkyDream 2011-08-08 14:05 發(fā)表評論
]]>
主站蜘蛛池模板: 元谋县| 额尔古纳市| 新密市| 普安县| 阳东县| 武功县| 石棉县| 云浮市| 射阳县| 平利县| 玛多县| 新泰市| 乐陵市| 杭州市| 洛浦县| 井冈山市| 疏附县| 邵东县| 宜君县| 平湖市| 浪卡子县| 绍兴市| 远安县| 遂宁市| 苗栗县| 高邑县| 伊金霍洛旗| 若尔盖县| 渭源县| 本溪市| 密山市| 高邑县| 灵武市| 宜宾县| 陆川县| 法库县| 吴忠市| 登封市| 宁河县| 南澳县| 鄂伦春自治旗|