这是Struts.xml
<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{
@Autowired
private GoodsBiz goodsBiz;
public GoodsBiz getGoodsBiz() {
return goodsBiz;
}
@Resource(name="goodsBiz")
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";
}
这是beans.xml
<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>
这是Dao实现
@Service("goodsDao")
@Transactional
public class GoodsDaoImpl implements GoodsDao{
@PersistenceContext protected EntityManager em;
public List getAllGoods() {
Query query = em.createQuery("from Goods g");
return query.getResultList();
}}
这事业务实现biz
@Service("goodsBiz")
@Transactional
public class GoodsBizImpl implements GoodsBiz {
private GoodsDao goodsDao;
public GoodsDao getGoodsDao() {
return goodsDao;
}
@Resource(name="goodsDao")
public void setGoodsDao(GoodsDao goodsDao) {
this.goodsDao = goodsDao;
}
public List getAllGoods() {
return goodsDao.getAllGoods();
}
这是web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value> <!-- 自定义路径 -->
</context-param>
<filter>
<display-name>jpaFilter</display-name>
<filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter </filter-class>
</filter> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
代码执行到Action红色那行代码是就报空指针,调用不到biz的方法?怎么回事
<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{
@Autowired
private GoodsBiz goodsBiz;
public GoodsBiz getGoodsBiz() {
return goodsBiz;
}
@Resource(name="goodsBiz")
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";
}
这是beans.xml
<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>
这是Dao实现
@Service("goodsDao")
@Transactional
public class GoodsDaoImpl implements GoodsDao{
@PersistenceContext protected EntityManager em;
public List getAllGoods() {
Query query = em.createQuery("from Goods g");
return query.getResultList();
}}
这事业务实现biz
@Service("goodsBiz")
@Transactional
public class GoodsBizImpl implements GoodsBiz {
private GoodsDao goodsDao;
public GoodsDao getGoodsDao() {
return goodsDao;
}
@Resource(name="goodsDao")
public void setGoodsDao(GoodsDao goodsDao) {
this.goodsDao = goodsDao;
}
public List getAllGoods() {
return goodsDao.getAllGoods();
}
这是web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value> <!-- 自定义路径 -->
</context-param>
<filter>
<display-name>jpaFilter</display-name>
<filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter </filter-class>
</filter> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
代码执行到Action红色那行代码是就报空指针,调用不到biz的方法?怎么回事
<action name="indexAction" class="indexAction" method="getAllGoods">
试试,这里的Action是应该交给Spring来管理的。