kitten

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks

          struts2.X配置文件默認存放路徑在/WEB-INF/classes目錄下,即將struts.xml放在src的目錄下。

          但是為了方便管理,開發人員把struts.xml放到其他位置,處理方法如下。

          首先要明白struts2加載配置文件都是從自己的jar包和/WEB-INF/classes兩個默認的位置加載的。

          若修改struts2.x配置文件的存放位置,在web.xml配置過慮器時,具體配置如下:
           

          1<filter>
          2    <filter-name>struts2</filter-name>
          3    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
          4    <init-param
          5        <param-name>config</param-name
          6        <param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
          7    </init-param>
          8</filter>

           

          注意點1

          若設置了<param-name>config</param-name>參數,那struts-default.xml等原來struts2默認加載的文件也要手動指定,否則不會自動加載。

           

          注意點2

          struts-plugin.xml也需要指定。因為在struts2使用2.1.6版本時:

          若需要和spring集成的話,struts2-spring-plugin-2.1.6.jar中有struts-plugin.xml這個文件。

          若struts2要支持json的話, json-plugin-0.34.jar中也有一個叫struts-plugin.xm的文件。

          因此這個文件也是要加載的。

           

          注意點3

          采用相對/WEB-INF/classes的相對路徑。本例放在了/WEB-INF/classes/struts目錄下。當然也可以寫成classpath:struts/struts.xml

           

          注意點4

          若不在這里配置struts-default.xml,struts-plugin.xml,也可以在struts.xml文件中添加include標簽將兩個文件包括進去。

          <include file="struts-default.xml" />和<include file="struts-plugin.xml" />

           

          注意點5

          使用<include file="..." />標簽添加其他子配置文件時,file屬性也要是一個相對/WEB-INF/classes的路徑。

          若子配置文件路徑是/WEB-INF/classes/configs/struts/student/struts-config.xml的話,

          file屬性值應該寫configs/struts/student/struts-config.xml。

          若有多個子配置文件可以采用掃描的方式<include file="configs/struts/*/*.xml" />


          可能遇到的問題:

          警告: Could not find action or result
                There is no Action mapped for namespace / and action name hello. - [unknown location]

          為什么指定了自己的struts.xml文件路徑依然訪問不到呢?

          原因依然在struts加載配置文件的方式,struts并不是獲取的配置文件相對應用(項目)的路徑,而是相對src,對于web是相對/WEB-INF/classes文件夾的路徑,現在知道了最終的解決方案了?

          對了,就是把web.xml中的[/WEB-INF/struts.xml]改成 [../struts.xml],即使用相對/WEB-INF/classes文件夾的路徑!

          來自:http://blog.csdn.net/xiayuzheng/article/details/12319469

          posted on 2014-04-08 20:45 云③+_+輸 閱讀(7323) 評論(0)  編輯  收藏 所屬分類: struts2
          主站蜘蛛池模板: 和田市| 北川| 聂荣县| 南漳县| 内江市| 木兰县| 华坪县| 许昌市| 团风县| 清河县| 南平市| 武城县| 长顺县| 嵩明县| 平昌县| 钦州市| 南澳县| 遂昌县| 宣化县| 尼勒克县| 白水县| 兰州市| 万安县| 吉林省| 凤翔县| 苍溪县| 河津市| 衡山县| 滦南县| 全南县| 赤水市| 安图县| 马尔康县| 原平市| 海伦市| 临海市| 红原县| 岑巩县| 仲巴县| 佛坪县| 罗甸县|