UserAction有个属性 private IUserDao userDao;第一种写法
在struts.xml中 <action name="userAction" class="com.my.web.UserAction"></action>在applicationContext.xml中<beans>
<bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>这么写运行成功。但是why?这个action是由spring ,还是struts还是strut2-spring-plugin产生的?
第二种写法
在struts.xml中 <action name="userAction" class="UserAction01"></action>在applicationContext.xml中<beans>
<bean id="UserAction01" class="com.my.web.UserAction">
</bean>
<bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>为什么第二种写法会空指针异常?为什么不像第一种方法给你自动注入属性?这个action是由spring ,还是struts还是strut2-spring-plugin产生的?请各位指点下
在struts.xml中 <action name="userAction" class="com.my.web.UserAction"></action>在applicationContext.xml中<beans>
<bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>这么写运行成功。但是why?这个action是由spring ,还是struts还是strut2-spring-plugin产生的?
第二种写法
在struts.xml中 <action name="userAction" class="UserAction01"></action>在applicationContext.xml中<beans>
<bean id="UserAction01" class="com.my.web.UserAction">
</bean>
<bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>为什么第二种写法会空指针异常?为什么不像第一种方法给你自动注入属性?这个action是由spring ,还是struts还是strut2-spring-plugin产生的?请各位指点下
解决方案 »
- websphere上部署spring quartz问题求教
- axis调用CXF开发的webservice服务端问题
- 使用s2sh框架,启动tomcat与junit单元测试出现冲突的问题
- 怎么样让servlet和action并存
- 安装 Myeclipse-8.0m1-win32.exe(772M)出现问题
- jstl标签出现怪问题,请熟悉的人帮我看下
- javamail 收件问题,请各位高手帮忙!
- Jbuilder4开发的EJB如何在Jboss中deploy?(着急啊)
- j2ee 1.3 beta2配置问题
- hibernate与oracle12c连接的问题
- org.eclipse.birt.core.exception.BirtException: Can not startup the OSGI framewor
- NoSuchMethodError错误,很急。
<beans>
<bean id="UserAction01" class="com.my.web.UserAction">
<property name="userDao" ref="userDao"></property>
</bean>
<bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>
就不会报空指针了吧
UserAction中有设置属性的方法叫setUserDao,则你的Bean的名字应叫userDao。
in struts.xml you specify the class for each Action. When using the default SpringObjectFactory, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.也就是说第一种action还是spring 产生的,是struts让spring自动注入的 you might want the bean to be completely managed by Spring.如果要完全的将action交给spring管理,那么all you have to do is configure the bean in your Spring applicationContext.xml and then change the class attribute from your Action in the struts.xml to use the bean name defined in Spring instead of the class name.
第二种是action已经完全交给了Spring管理,所以不配置不会自动注入不知道理解的对不对。
显然你还是没理解 struts2.。 你看的是struts2的spring插件的文档。很显然你的第一种方式,即使你不用spring,也能运行正常。 和spring又有何关系呢?
不与spring集成时,默认的是 org.apache.struts2.impl.StrutsObjectFactory,
可省略<constant name="struts.objectFactory" />的配置项。
与spring集成时是 com.opensymphony.xwork2.spring.SpringObjectFactory
需要在配置文件中指定<constant name="struts.objectFactory" value="spring" />