Struts2+spring+jpa(Hibernate)Action里面出现空指针问题。很无语……
找不到问题出哪了这事struts。xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//Apac...
找不到问题出哪了
这事struts。xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<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>
这事Action 最上面的省略了
@Controller("indexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport implements SessionAware{
@Resource private 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";
}
}
这事beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.demo" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="demo" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<beans>
这是DaoImpl实现
@Service
@Transactional
public class GoodsBizImpl implements GoodsBiz {
@Resource private GoodsDao goodsDao;
public List getAllGoods() {
List list = goodsDao.getAllGoods();
return list;
}
}
biz类估计你们不看都知道了
代码测试很正常BiZ跟DAO的,就是执行到Actiond的List listGoods = goodsBiz.getAllGoods();这条代码时就报空指针了······找不到错误出哪了 展开
这事struts。xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<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>
这事Action 最上面的省略了
@Controller("indexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport implements SessionAware{
@Resource private 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";
}
}
这事beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.demo" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="demo" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<beans>
这是DaoImpl实现
@Service
@Transactional
public class GoodsBizImpl implements GoodsBiz {
@Resource private GoodsDao goodsDao;
public List getAllGoods() {
List list = goodsDao.getAllGoods();
return list;
}
}
biz类估计你们不看都知道了
代码测试很正常BiZ跟DAO的,就是执行到Actiond的List listGoods = goodsBiz.getAllGoods();这条代码时就报空指针了······找不到错误出哪了 展开
5个回答
展开全部
注解的方式不熟悉,但是你用注解很不方便维护的啊!为什么不采用注入的方式呢!原因估计为:GoodsDao 的实现类未成功注入!
追问
这种注解我们这有几个人都遇到这种问题,因为这种之前人家是配合struts1.3开发的,我们改为struts2就出现莫名奇妙的问题了,我用Hibernate注解就没事了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-05-16
展开全部
把错误代码帖出来看下撒
追问
不是贴了么
追答
我说的是把你控制台打印出来的报错的错误信息帖出来看下子
public List getAllGoods() {
List list = goodsDao.getAllGoods();
return list;
}
你在这里面设置断点,看下是goodsDao为空还是其它的位置为空.
我估计是goodsDao为空
如果是goodsDao为空的话
1 定义DAO的时候
@Component("goodsDao")
public class GoodsDao
2 在SERVICE中注入该DAO
@Autowired
@Qualifier("goodsDao")
private GoodsDao goodsDao;
这样基本能解决上述问题
还有一种可能,就是sessionFctory为空
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-05-14
展开全部
不太懂 不好意思
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
GoodsBizImp
追问
测试时没问题的这个类····但一拿到action里面来就出问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询