Struts的action自动装配到spring容器可能出现异常的解决方法

来源:互联网 发布:会员制软件 编辑:程序博客网 时间:2024/06/05 19:26

Struts在与Spring整合是要实现Struts的action自动装配到spring容器中,我们经常遇到的问题是:java.lang.NullPointException这个空指针异常,我碰到的有这么几种解决方案:

1.看一下你的set方法是否存在。

2.是否在web.xml中配置了“当web项目启动的时候,让spring扫描整个项目。”,当然也要加上一个名为:struts-spring-plugin***.jar的包,这个包实现了action装配到spring的功能。(如果已经加了这个包,也配置了web.xml还出现空指针异常,那么可能是你的struts-spring-plugin***.jar的版本不配套,还几个版本试试)

web.xml中添加的配置(添加在Struts配置的前面):

<listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:bean.xml</param-value></context-param>

当你已经完成上面描述的配置的时候,还可能出现下面的这个异常,异常信息如下(这个是浏览器报的异常,后台没有异常,服务器启动正常):


Struts Problem ReportStruts has detected an unhandled exception:Messages:Cannot convert value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy foundFailed to convert property value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy foundUnable to instantiate Action, com.zsq.wxssh.action.LoginAction, defined for 'Login' in namespace '/'Failed to convert property value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy foundFile:org/springframework/beans/TypeConverterDelegate.javaLine number:231StacktracesUnable to instantiate Action, com.zsq.wxssh.action.LoginAction, defined for 'Login' in namespace '/'Failed to convert property value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy found    com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:316)    com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)    org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)    java.lang.Thread.run(Thread.java:724)    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy found    org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)    com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:203)    com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)    com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)    com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)    com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)    com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)    org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)    java.lang.Thread.run(Thread.java:724)    java.lang.IllegalArgumentException: Cannot convert value of type [com.sun.proxy.$Proxy8 implementing com.zsq.wxssh.service.UserService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.zsq.wxssh.serviceimpl.UserServiceImpl] for property 'userServiceImpl': no matching editors or conversion strategy found    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)    org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)    com.opensymphony.xwork2.spring.SpringObjectFactory.autoWireBean(SpringObjectFactory.java:203)    com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:183)    com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)    com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)    com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)    com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)    org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)    org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)    java.lang.Thread.run(Thread.java:724)

仔细看异常信息,好像是在说“自动装配的时候用到了代理类,并且发生了类型转换异常的信息”,上网查的时候,说是存在实现接口的关系,就有可能报这种错误:

(我的项目中,有UserServiceImpl 实现了UserService这个接口,我估计这就是出现异常的地方)在网上查解决方案,最后得到了答案:在Action类中将UserServiceImpl类型的属性改变为其实现的父接口的类型,即UserService。将Action改了下,下面代码所示:


private UserService userServiceImpl;public UserService getUserServiceImpl() {return userServiceImpl;}//@Resource(name="userServiceImpl")//这里struts和spring整合,spring会自动扫描Action的属性,然后自动注入。public void setUserServiceImpl(UserService userService) {this.userServiceImpl = userService;}

其中,UserServiceImpl是Action类的一个成员变量(属性),将它的类型改为器父接口的形式UserService类型,切记:你的方法名字必须和你对应的bean的名字保持一致(struts会自动扫描,然后加载),我的一个bean的明字为:userServiceImpl所以我的方法名为getUserSerbvceImpl和setUserServiceImpl,按照我这种做法,你就不需要在Action中添加@component,@Scape和@Resource这几个注解了(自动装配)。



关于第二个异常,详情请看:http://grey2.iteye.com/blog/994926。


这个异常困扰了我很长时间,写出来给需要的人。

祝你成功。。。