Struts2+Spring+JPA(Hibernate)报空指针问题?

九两银 2011-05-14 03:24:02
有没有人遇到同样的问题··代码执行到Action里时调用biz方法时返回值是空的,报空指针了。。
但测试这个biz方法时没问题的。。。
...全文
101 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
九两银 2011-05-14
  • 打赏
  • 举报
回复
有没人来帮忙整一下啊
  • 打赏
  • 举报
回复
如果我没有记错的话,”我已经引用了这段代码了beans的<context:component-scan base-package="com.demo" />“spring的Ioc容器里已经有了这个action的bean,只要加上struts-spring插件让spring管理action,然后再加上class="IndexAction",说明一下,spring的Ioc容器会按照action的类名来创建bean。
九两银 2011-05-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 centre10 的回复:]
补充一下:由于你采用的annotation,你可以这么写:
<constant name="struts.devMode" value="true"></constant>
<package name="IndexAction" namespace="/" extends="struts-default">
<action name="indexAction" class="IndexAct……
[/Quote]
我已经引用了这段代码了beans的<context:component-scan base-package="com.demo" />
  • 打赏
  • 举报
回复
同时还应该加入struts-spring的jar包,让spring管理action
  • 打赏
  • 举报
回复
补充一下:由于你采用的annotation,你可以这么写:
<constant name="struts.devMode" value="true"></constant>
<package name="IndexAction" namespace="/" extends="struts-default">
<action name="indexAction" class="IndexAction" method="getAllGoods">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
  • 打赏
  • 举报
回复
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="IndexAction" namespace="/" extends="struts-default">
<action name="indexAction" class="com.demo.action.IndexAction" method="getAllGoods">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
Struts。xml

你的action是由struts2的容器管理的,没有用到spring容器里管理的acton,因此,你可以这样写试试:
<constant name="struts.devMode" value="true"></constant>
<package name="IndexAction" namespace="/" extends="struts-default">
<action name="indexAction" class="xxx(spring的actionbean)" method="getAllGoods">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
chenyinglong0712 2011-05-14
  • 打赏
  • 举报
回复
你在spring中没配置Action中的对象...
九两银 2011-05-14
  • 打赏
  • 举报
回复
我在线关注着··希望有人来解决下
九两银 2011-05-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
biz 没有注入到这个 action 里边。
[/Quote]
可我明明注了,怎么就不起作用呢?在哪需要检查??
AcHerat 2011-05-14
  • 打赏
  • 举报
回复
biz 没有注入到这个 action 里边。
九两银 2011-05-14
  • 打赏
  • 举报
回复
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="IndexAction" namespace="/" extends="struts-default">
<action name="indexAction" class="com.demo.action.IndexAction" method="getAllGoods">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
Struts。xml
九两银 2011-05-14
  • 打赏
  • 举报
回复
@Controller("indexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport implements SessionAware{
private GoodsBiz goodsBiz;

public GoodsBiz getGoodsBiz() {
return goodsBiz;
}
@Resource
public void setGoodsBiz(GoodsBiz goodsBiz) {
this.goodsBiz = goodsBiz;
}

private Map<String,Object> session=new HashMap<String, Object>();

public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> arg0) {
this.session=session;
}

private int count;

public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}


public String getAllGoods(){
// try {
List listGoods = goodsBiz.getAllGoods(); //这事报错的地方
count=listGoods.size();
session.put("listGoods", listGoods);
// } catch (Exception e) {
// System.out.println(e.getMessage());
// }
System.out.println("test_Ok");
return "index";
}
这里面的话有问题没?
宁波朱超 2011-05-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 jiuliangyin 的回复:]
有没有人遇到同样的问题··代码执行到Action里时调用biz方法时返回值是空的,报空指针了。。
但测试这个biz方法时没问题的。。。
[/Quote]

明显你的biz没被注入。所以action里调到的biz是空
wl_ldy 2011-05-14
  • 打赏
  • 举报
回复
不知道你的Action是否是让Spring来管理的,如果是的话,那很可能是Spring没有将Action注入进去或者Action中的Service层没有注入到Action中。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧