引入了 struts2-spring-plugins 包后 Spring 就会管理你的 Action

Struts2 与 Spring 结合,使用 Spring 来管理 Action 实例,在项目中引入了struts2-spring-plugin-2.2.1.jar 包。然后想到的是既然是要用 Spring 来管理 Action 实例,就得在 struts.xml 里加上:

<constant name="struts.objectFactory" value="spring" /> 

或者是在 struts.properties 里加上一条属性:

struts.objectFactory = spring 

暂时不用 Spring 来管理 Bean,于是把上面的配置去了,也把 web.xml 中的相关 

Spring 的 ContextLoaderListener 也格啦。容器启动的时候却发现:

INFO: Initializing Struts-Spring integration...

Jan 20, 2011 7:32:08 PM org.apache.struts2.spring.StrutsSpringObjectFactory <init>

SEVERE: ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********

Looks like the Spring listener was not configured for your web app!

Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.

You might need to add the following to web.xml:

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

Jan 20, 2011 7:32:08 PM org.apache.catalina.core.StandardContext filterStart

SEVERE: Exception starting filter action2

java.lang.NullPointerException

 at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:189)

 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:479)

 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:450)

 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:407)

 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)

 at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)

 at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)

 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)

Struts2 并不依赖 Spring 是在 struts2-spring-plugin-2.2.1.jar 中的 struts-plugin.xml 中是这样的配置:


<struts>     

<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory"/>         

<!--  Make the Spring object factory the automatic default -->   

<constant name="struts.objectFactory" value="spring" />      

<constant name="struts.class.reloading.watchList" value="" />    

<constant name="struts.class.reloading.acceptClasses" value="" />    

<constant name="struts.class.reloading.reloadConfig" value="false" />     

<package name="spring-default">      

<interceptors>             

<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>            

 <interceptor name="sessionAutowiring" class="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>      

</interceptors>     

</package>   

</struts> 


<constant name="struts.objectFactory" value="spring" />,只要引入了 struts2-spring-plugin 插件包就启作用,在 struts.xml 或是 struts.properties 中的设置的该属性完全是多此一举,要知道 spring 这个 objectfactory 也是在这里边声明。

只要引入了 struts2-spring 插件包即可,最好还是从 strtus.xml 或 struts.properties 中把该项目配置拿掉。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值