问题描述
自己弄了spring的配置文件,和strut的配置文件这个是服务层的代码publicclassSerservicesimplementsISerservices{privateICstServiceDAOServicedao;publicvoidsetDao(ICstServiceDAOServicedao){this.dao=dao;}publicvoidsave(CstServicebean){dao.save(bean);}}这个是Action的代码publicclassSerActionextendsActionSupportimplementsModelDriven<CstService>{privateISerservicesservices;publicvoidsetServices(ISerservicesservices){this.services=services;}CstServicebean=newCstService();publicCstServicegetModel(){returnbean;}publicStringseradd()throwsException{services.save(bean);returnSUCCESS;}}自己写的strut配置文件<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><packagename="default"extends="struts-default"><actionname="Seradd"class="com.newer.crm.serManage.action.SerAction"method="seradd"><result>login.jsp</result></action></package></struts>在系统自动生成的配置文件中也引用了自己写的配置文件<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><includefile="com/newer/crm/serManage/Serservices-struts.xml"></include></struts>自己写的spring配置文件,同上。<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><beanid="Serservices"class="com.newer.crm.serManage.services.Serservices"><propertyname="dao"><refbean="CstServiceDAO"></ref></property></bean><beanid="Seradd"class="com.newer.crm.serManage.action.SerAction"><propertyname="services"><refbean="Serservices"></ref></property></bean></beans>系统自动生成的spring配置文件,字太多,我就只把关键的贴出来<beanid="CstServiceDAO"class="com.newer.crm.dao.impl.CstServiceDAO"><propertyname="sessionFactory"><refbean="sessionFactory"/></property></bean><importresource="classpath:com/newer/crm/serManage/Serservices.xml"/>当我想调用action中services的save方法时,便在界面上出现的空指针异常java.lang.NullPointerExceptioncom.newer.crm.serManage.action.SerAction.seradd(SerAction.java:21)sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)求各位高手帮我解决。。是不是我spring的注入有问题?
解决方案
解决方案二:
我先不说那个配置文件怎么样了,就是publicvoidsave(CstServicebean){dao.save(bean);}这个方法,就是你已经给spring管理了,你这个还直接用???肯定空指针。
解决方案三:
我的publicvoidsave(CstServicebean)这个方法不是DAO层的save方法,是我在服务层自己写的一个方法,我换个方法名,可能看的清楚些。publicclassSerservicesimplementsISerservices{privateICstServiceDAOServicedao;publicvoidsetDao(ICstServiceDAOServicedao){this.dao=dao;}publicvoidadd(CstServicebean){dao.save(bean);}}actionpublicclassSerActionextendsActionSupportimplementsModelDriven<CstService>{privateISerservicesservices;publicvoidsetServices(ISerservicesservices){this.services=services;}CstServicebean=newCstService();publicCstServicegetModel(){returnbean;}publicStringseradd()throwsException{services.add(bean);returnSUCCESS;}}不知道是我没理解你的意思,还是怎么。如果是我没理解你的意思,麻烦说详细点,多谢
解决方案四:
坐等解释啊。。。。。。。
解决方案五:
坐等观望
解决方案六:
老大你先去找个例子详细了解一下再说!
解决方案七:
没有get方法~~~你action中的bean什么用?new了一下就塞进去了,里面什么都没有
解决方案八:
是不是Service注入失败啊??求澄清~~~
解决方案九:
spring和strut集成不对造成注入失败
解决方案十:
CstServicebean=newCstService();楼主看看是不是action实例化的原因,
解决方案十一:
注入失败!。。。。。。
解决方案十二:
spring的注入有问题
解决方案十三:
这个问题很好解决的:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><packagename="default"extends="struts-default"><actionname="Seradd"class="这里应该填写成Spring中Action的id值就可以了,就不会跑出null的异常了"method="seradd"><result>login.jsp</result></action></package></struts>给成为“Seradd”就可以是你在bean中配合Action的id
解决方案十四:
还有一中方式就是:你把spring中的服务层的对象注入到Action中的时候你Action中的services名称,在spring中配置就可以了,spring会自动的给你注入到你的Action中的!下面的配置修改成这样就可以运行了、原来的:<beanid="Seradd"class="com.newer.crm.serManage.action.SerAction"><propertyname="services"><refbean="Serservices"></ref></property></bean>修改后的:<beanid="services"class="com.newer.crm.serManage.services.Serservices"><propertyname="dao"><refbean="CstServiceDAO"></ref></property></bean><beanid="Seradd"class="com.newer.crm.serManage.action.SerAction"></bean>
解决方案十五:
这是我在试验中做的!两种方式都不会错误的!
解决方案:
struts2与Sping关联是需要一个架包的,并且,需要在struts2的配置文件中进行配置,才能连在一起用。直接用当然会出错了。架包名字忘了,可以在网上找找看。
解决方案:
引用15楼xiongdan1982的回复:
struts2与Sping关联是需要一个架包的,并且,需要在struts2的配置文件中进行配置,才能连在一起用。直接用当然会出错了。架包名字忘了,可以在网上找找看。
如果是这样的话:spring-struts-plugin.jar