SSH问题集(一) spring和struts整合配置文件问题
1、spring默认配置文件applicationContext.xml的路径设置
<context-param>
<param-name>contextConfigLocation</param-name>
<!--如果文件在classes下,配置为classpath:-->
<param-value>classpath:applicationContext.xml</param-value>
<!--如果指定目录WEB-INF或resources目录下-->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、struts.xml文件,默认加载在classes目录下
如果指定struts.xml的路径,参见如下配置
<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,../../WEB-INF/struts.xml</param-value> -->
</init-param>
</filter>
由于此配置文件导致的各类报错:
问题 A:
严重: Exception starting filter struts2
com.opensymphony.xwork2.inject.DependencyException: 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).
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:144)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:113)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:90)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:86)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:71)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:69)
at com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)
at com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:483)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:523)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:522)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:574)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:521)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:188)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:360)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:403)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:48)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: 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).
at com.opensymphony.xwork2.inject.ContainerImpl.createParameterInjector(ContainerImpl.java:235)
at com.opensymphony.xwork2.inject.ContainerImpl.getParametersInjectors(ContainerImpl.java:225)
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.<init>(ContainerImpl.java:287)
at com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:117)
at com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:115)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:141)
... 36 more
原因:
配置成了<param-value>/WEB-INF/struts.xml</param-value> :
解决方法:
加上struts-default.xml,struts-plugin.xml,虽然没这2个文件。
<param-value>struts-default.xml,struts-plugin.xml,/WEB-INF/struts.xml</param-value>
问题 B:
严重: Exception starting filter struts2
Unable to load configuration. - [unknown location]
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:360)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:403)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:48)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:287)
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:258)
at org.apache.struts2.config.BeanSelectionProvider.register(BeanSelectionProvider.java:203)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:165)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
... 20 more
原因:
很多可能造成这个报错
解决方法:
1)将WEB-INF/struts.xml修改为../../WEB-INF/struts.xml
<param-value>../../WEB-INF/struts.xml</param-value>
还是报加载不了配置文件Unable to load configuration. - [unknown location]
2)如果配置为:<param-value>struts.xml</param-value> ,同时将struts.xml放到classes目录下,感觉上应该去加载classes下的,但仍然报错
3)加上struts-default.xml,struts-plugin.xml
即:<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
问题解决。
综上,配置都要包括 struts-default.xml,struts-plugin.xml,即便没有这2个文件 。如果struts.xml在classes下,就不需要<init-param> </init-param>段,加了反而容易出错。
上述验证采用的各组件和版本如下:
antlr-2.7.6.jar
aopalliance-1.0.jar
asm-1.5.3.jar
asm-attrs-1.5.3.jar
c3p0-0.9.2.jar
cglib-2.1_3.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
freemarker-2.3.15.jar
hibernate-3.2.1.ga.jar
hibernate-annotations-3.3.0.ga.jar
hibernate-commons-annotations-3.3.0.ga.jar
hibernate-core-3.3.0.GA.jar
jta-1.1.jar
junit-3.8.1.jar
log4j-1.2-api-2.0.jar
log4j-api-2.0.jar
log4j-core-2.0.jar
mchange-commons-java-0.2.3.3.jar
mysql-connector-java-5.1.6.jar
ognl-2.6.11.jar
persistence-api-1.0.jar
slf4j-api.jar
slf4j-log4j12.jar
spring-aop-3.0.1.RELEASE.jar
spring-asm-3.0.1.RELEASE.jar
spring-beans-3.0.1.RELEASE.jar
spring-context-3.0.1.RELEASE.jar
spring-core-3.0.1.RELEASE.jar
spring-expression-3.0.1.RELEASE.jar
spring-jdbc-3.0.1.RELEASE.jar
spring-orm-3.0.1.RELEASE.jar
spring-test-2.5.6.jar
spring-tx-3.0.1.RELEASE.jar
spring-web-3.0.1.RELEASE.jar
struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
xml-apis-1.0.b2.jar
xwork-2.1.2.jar