请求时报以下异常:
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>
注意前面不需要"/"。
另外一个需要注意的地方就是,使用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。