struts2配置文件路徑

博客分類: 隨手記

請求時報以下異常:

com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=com.opensymphony.xwork2.ObjectFactory, name='default'] in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.setObjectFactory(com.opensymphony.xwork2.ObjectFactory). - Class: com.opensymphony.xwork2.inject.ContainerImpl File: ContainerImpl.java

 

原因是自定義了struts的配置文件路徑,但是路徑指向不正確。默認情況下struts2會在classpath下加載默認配置文件struts.xml和struts-default.xml(在struts2-core.jar下)和struts-plugin.xml(在相關struts2插件jar包中)【可選】。

 

如果通過config參數指定配置文件路徑,那麼這幾個默認配置文件路徑都要顯式指定一下:

 

Java代碼 複製代碼 收藏代碼
  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>  
<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
		</init-param>
	</filter>

 注意前面不需要"/"。

 

另外一個需要注意的地方就是,使用include指令包含文件時:

struts.xml的配置:

 

Xml代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.devMode" value="true" />  
  9.     <constant name="struts.action.extension" value="action" />  
  10.     <constant name="struts.objectFactory" value="spring" />  
  11.   
  12.     <include file="struts/struts-config.xml" />  
  13.   
  14. </struts>  

 是相對於classpath根目錄的路徑,而不是相對於文件的路徑。比如說上面的配置,struts.xml在classpath的struts目錄下,struts-config.xml也在同一目錄下,但是include中file的路徑要寫成struts/struts-config.xml,而不是struts-config.xml。

發佈了25 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章