我是靠谱客的博主 老迟到汽车,最近开发中收集的这篇文章主要介绍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来管理的。

 

最后

以上就是老迟到汽车为你收集整理的spring struts2整合之action 产生问题 service 为null的全部内容,希望文章能够帮你解决spring struts2整合之action 产生问题 service 为null所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部