配置struts2.0项目遇到的问题

本来很简单的struts项目部署,却花掉我整整一天时间,归根结底问题出在struts2-spring-plugin-2.0.6.jar包身上。
[b]先描述一下出现的问题吧:[/b]
1、启动项目后就报HTTP Status 404 错误,也就是tomcat中找不到文件错误(NOT FOUND),所有的JSP页面都无法访问。
2、控制台的输出:
严重: Exception starting filter struts2
Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class:org.apache.struts2.spring.StrutsSpringObjectFactory - bean - jar:file:/D:/Eclipse%20workspaces/MyEclipse%207.1%20for%20Struts2.0%20workspace%20(1)/.metadata/.me_tcat/webapps/emaileProject/WEB-INF/lib/struts2-spring-plugin-2.0.6.jar!/struts-plugin.xml:8:132
...省略...
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware
... 33 more

说明是缺少org.springframework.context.ApplicationContextAware类,但是我配置的是一个Struts2.0+Hibernate的项目,并没有用到Spring,为什么在服务器加载Struts2.0的过程中涉及到Spring呢?
[b]问题解决:[/b]
折腾了一天,从诊断web.xml struts.xml到不断尝试新建workspace...
最终发现,问题居然是struts2-spring-plugin-2.0.6.jar这个是用于struts2.0与Spring整合的包,如果项目中没有用到Spring,就不要引入这个包,并且这个包也不能放在WEB-INF\lib文件夹里,只要lib下面有struts2-spring-plugin-2.0.6.jar包,就会对Spring进行监听,就会报上面出现的错误...

[b]补充:(我在JavaEye上找到struts2-spring-plugin包的作用,不知道对不对,接下来验证)[/b]
struts2与spring的整合。导入struts2-spring-plugin包,在web.xml中设置spring的监听器,
spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。
struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,其代码为:
<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" />

<package name="spring-default">
<interceptors>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
</interceptors>
</package>
</struts>
很明显,将struts.objectFactory定位为org.apache.struts2.spring.StrutsSpringObjectFactory
其余的工作就交给spring的IoC容器去做了。
另外:当我们需要增加spring的配置文件时,需要在web.xml中设定contextConfigLocation参数。代码如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>其他的spring配置文件名,用逗号隔开</param-value>
</context-param>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值