报的错误:
1 严重: Exception starting filter struts2 2 Class: com.opensymphony.xwork2.spring.SpringObjectFactory 3 File: SpringObjectFactory.java 4 Method: getClassInstance 5 Line: 220 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1 6 at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437) 7 at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:195) 8 at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) 9 at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397) 10 at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108) 11 at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709) 12 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4356) 13 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) 14 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) 15 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525) 16 at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626) 17 at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553) 18 at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488) 19 at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1215) 20 at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293) 21 at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) 22 at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337) 23 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601) 24 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610) 25 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590) 26 at java.lang.Thread.run(Unknown Source) 27 Caused by: java.lang.NullPointerException 28 at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:220) 29 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:530) 30 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:501) 31 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:457) 32 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:275) 33 at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111) 34 at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:204) 35 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66) 36 at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:380) 37 at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:424) 38 ... 20 more 39 2011-8-28 1:19:47 org.apache.catalina.core.StandardContext start 40 严重: Error filterStart 41 2011-8-28 1:19:47 org.apache.catalina.core.StandardContext start 42 严重: Context [] startup failed due to previous errors
一、原因:
问题处在你导入的“Struts2 Spring Libraries”库下的“Struts2-spring-plugin-2.2.1.jar”jar包下的struts-plugin.xml里。如下图:
问题出现的具体位置在上图所示的第29行的代码,这段代码如下:
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
这 表明该插件引入工程后,会自动设置Struts的 ObjectFactory为
StrutsSpringObjectFactory,从而让Spring的IOC容器来托管Struts的Action。但是
在工程的web.xml中
没有配置
加载spring ApplicationContext相关的listener
,从而导致了启动时出现的问题:com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1。
二、解决办法
在工程的web.xml中 配置 加载spring ApplicationContext相关的listener ,配置的代码如下:
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
终于解决了这个小问题,记录一下,方便查看和回顾,也希望能帮到遇到这个问题需要寻求帮助的朋友。