請求時報以下異常:
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參數指定配置文件路徑,那麼這幾個默認配置文件路徑都要顯式指定一下:
- <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>
<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 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" />
- <constant name="struts.action.extension" value="action" />
- <constant name="struts.objectFactory" value="spring" />
- <include file="struts/struts-config.xml" />
- </struts>
是相對於classpath根目錄的路徑,而不是相對於文件的路徑。比如說上面的配置,struts.xml在classpath的struts目錄下,struts-config.xml也在同一目錄下,但是include中file的路徑要寫成struts/struts-config.xml,而不是struts-config.xml。