spring struts2整合之action 产生有关问题 service 为null
spring struts2整合之action 产生问题 service 为null
Service层: 定义接口WposService和接口的实现类WposServiceImpl
Action层:
WposAction.java的内容:
public class WposAction extends CommonActionSupport{ private Wpos wpos; private WposService wposSer;//注入service层接WposService private DataPubBiz dataPubBiz; public Wpos getWpos() { return wpos; } public void setWpos(Wpos wpos) { this.wpos = wpos; } public WposService getWposSer() { return wposSer; } public void setWposSer(WposService wposSer) { this.wposSer = wposSer; } public DataPubBiz getDataPubBiz() { return dataPubBiz; } public void setDataPubBiz(DataPubBiz dataPubBiz) { this.dataPubBiz = dataPubBiz; } public String queryWposList(){ List<Wpos> list=null; list=wposSer.queryAllWpos(); this.getRequest().setAttribute("wp", list); return "queryWposListSuccess"; } }
Spring 的applicationContext.xml 中bean的配置
<bean id="WposAction" class="com.actions.WposAction" scope="prototype"> <property name="wposSer" ref ="wposSer"/> <property name="dataPubBiz" ref ="dataPubBiz"/> </bean> <bean id="wposSer" class="com.service.WposServiceImpl" scope="prototype"> <property name="dao" ref="commonDao"/> </bean>
Structs2的struts.xml配置
<action name="WposAction" class="com.actions.WposAction"> <result name="queryWposListSuccess">跳转页面</result> </action>
问题如下:
1、applicationContext.xml中 property name="wposSer",是实际存在的id为wposSer的bean
因为Structs2的struts.xml中action的class路径不是Spring中配置的action的id,而是类的全路径,这样工程启动时action的控制权由structs掌管,当我们访问Action的时候首先由struts创建Action,然后在跟据Action的属性 wposSer去spring容器中去找id为 wposSer 的bean,如果不存在bean的id为wposSer(绿色标注),报空指针异常,wposSer为空。
2、WposAction中必须有spring中bean的id为property属性,并写好getter(),setter()方法,否则报org.springframework.beans.NotWritablePropertyException异常
3、可以把struts中action的class设置为spring容器中的bean的id时,action的创建及属性注入式有spring来管理的。