struts2与spring4整合报错及处理方法

一开始搭建struts2时就没使用默认的配置方式,所以一路下来吃了不少的错,现在和spring整合又报错了,错误信息如下:

严重: Dispatcher initialization failed
Unable to load configuration. - [unknown location]
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)
	at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:967)
	at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:435)
	at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:479)
	at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
	at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4854)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5546)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
	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.AbstractBeanSelectionProvider.alias(AbstractBeanSelectionProvider.java:74)
	at org.apache.struts2.config.AbstractBeanSelectionProvider.alias(AbstractBeanSelectionProvider.java:45)
	at org.apache.struts2.config.DefaultBeanSelectionProvider.register(DefaultBeanSelectionProvider.java:363)
	at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:240)
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
	... 17 more

原因,struts2与spring整合时不仅需要使用struts2-spring-plugin-2.3.24.jar,还需要用到struts-plugin.xml,但是我是自定义放置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,../conf/struts/struts-config.xml</param-value>
       </init-param>
    </filter>

在配置struts配置文件引用时,未加入struts-plugin.xml的引用,导致出现上述的错误。

处理方式及过程

1、在WEB-INF/lib目录下加上struts2-spring-plugin-2.3.24.jar 

2、修改web.xml中有关struts2的配置,修改后的配置如下:

<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,../conf/struts/struts-config.xml</param-value>
       </init-param>
</filter>
<filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>*.do</url-pattern>
</filter-mapping>

到此,问题已经处理完毕。

在struts.xml配置中,中配置Action时,指定<action>class属性为Spring配置文件中相应beanid或者name值即可。

你可能感兴趣的:(struts2,spring4)