本文主要是介绍Struts2和Spring整合中出现的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天做了一个Struts2和Spring整合的练习,中间出了一点问题,贴出来供其它人参考。(该文章写于2010年)
代码写好后启动Tomcat服务器时报错:
严重: Exception starting filter struts2
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=java.lang.String, name='struts.objectFactory.spring.autoWire.alwaysRespect'] in public org.apache.struts2.spring.StrutsSpringObjectFactory(java.lang.String,java.lang.String,java.lang.String,javax.servlet.ServletContext).at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:290)at com.opensymphony.xwork2.inject.ContainerImpl$2.call(ContainerImpl.java:117)at com.opensymphony.xwork2.inject.ContainerImpl$2.call(ContainerImpl.java:115)at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:542)at com.opensymphony.xwork2.inject.ContainerImpl.injectStatics(ContainerImpl.java:114)at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:494)at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:145)at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:926)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:889)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:516)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:578)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
仔细检查了一遍代码,没有发现什么问题。后来上网查了一下,说是Jar包版本不对,我一看,果然我的Jar包有问题,Struts核心包是struts2-core-2.0.9.jar,而StrutsSpring插件包是struts2-spring-plugin-2.1.6.jar,后来将Struts核心包换成struts2-core-2.1.6.jar,重新启动Tomcat服务器,又出现了新的异常:
严重: Exception starting filter struts2
Unable to load configuration. - bean - jar:file:/D:/ApplicationServer/Tomcat%206.0/webapps/strutsspring/WEB-INF/lib/struts2-core-2.1.6.jar!/struts-default.xml:46:178at 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.FilterDispatcher.init(FilterDispatcher.java:190)at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:926)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:889)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:516)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:578)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
文件struts-default.xml的第46行的代码是:<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="struts" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default"/>
JakartaMultiPartRequest是一个处理文件上传的类,这儿的scope="default"要求其默认情况下加载,而我的工程中没有相应的Jar包,遂将处理文件上传下载相应的Jar包commons-fileupload和commons-io加上,启动Tomcat服务器,一切正常。
最后的Jar包是:
- commons-fileupload-1.1.1.jar
- commons-io-1.3.2.jar
- commons-logging-1.0.4.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- spring.jar
- struts2-core-2.1.6.jar
- struts2-spring-plugin-2.1.6.jar
- xwork-2.1.2.jar
希望其它人在整合时注意类似的问题。
这篇关于Struts2和Spring整合中出现的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!