public String getMes() { return mes; } public void setMes(String mes) { this.mes = mes; } public String fandAll(){ System.out.println("ssss"); try{ productDAO=new ProductDAOImpl(); int i= productDAO.findAll().size(); System.out.println("数组"+i); }catch (Exception e) { // TODO: handle exception mes="error"; System.out.println("错误"); System.out.println(e); } return SUCCESS; } } ProductDAO 接口代码 package com.linky.dao;package com.linky.dao;import java.util.List;import com.linky.entity.Product;public interface ProductDAO { // property constants public static final String NAME = "name"; public static final String SUPPLIER = "supplier"; public static final String TYPE = "type"; public static final String VERSIONS = "versions"; public static final String SPECIFICATION = "specification"; public static final String COLOR = "color"; public static final String SIZE = "size"; public static final String AGREEMENT = "agreement"; public static final String REMARK = "re"; public abstract List findAll();} ProductDAOImpl 接口实现类代码 package com.linky.dao;import java.sql.Timestamp; import java.util.List; import org.hibernate.LockMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.linky.entity.Product;/** * A data access object (DAO) providing persistence and search support for * Product entities. Transaction control of the save(), update() and delete() * operations can directly support Spring container-managed transactions or they * can be augmented to handle user-managed Spring transactions. Each of these * methods provides additional information for how to configure it for the * desired type of transaction control. * * @see com.linky.entity.Product * @author MyEclipse Persistence Tools */public class ProductDAOImpl extends HibernateDaoSupport implements ProductDAO { private static final Logger log = LoggerFactory.getLogger(ProductDAOImpl.class); protected void initDao() { // do nothing } /* (non-Javadoc) * @see com.linky.dao.ProductDAO#findAll() */ public List findAll() { log.debug("finding all Product instances"); try { String queryString = "from Product"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } /* (non-Javadoc) * @see com.linky.dao.ProductDAO#attachDirty(com.linky.entity.Product) */ public void attachDirty(Product instance) { log.debug("attaching dirty Product instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } /* (non-Javadoc) * @see com.linky.dao.ProductDAO#attachClean(com.linky.entity.Product) */ public void attachClean(Product instance) { log.debug("attaching clean Product instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static ProductDAO getFromApplicationContext(ApplicationContext ctx) { return (ProductDAO) ctx.getBean("ProductDAO"); } }页面显示效果 {"mes":"error","productDAO":{"hibernateTemplate":null,"sessionFactory":null}}myeclipse打印错误: ssss 错误 java.lang.NullPointerException
Unable to load configuration. - [unknown location] at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:428) at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51) at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4638) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5294) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1366) at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1454) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:295) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1379) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1537) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1526) at java.lang.Thread.run(Thread.java:722)
private String mes;
这两个实例化一下看看
package com.linky.service;import java.util.ArrayList;
import java.util.List;
import com.linky.dao.ProductDAO;
import com.linky.dao.ProductDAOImpl;import com.linky.entity.Product;
public class UserServiceImpl implements UserService {
private ProductDAO productDao; public void setProductDao(ProductDAO productDao) {
this.productDao = productDao;
}
/* (non-Javadoc)
* @see com.linky.service.UserService#getAllProduct()
*/
public ArrayList<Product> getAllProduct(){
System.out.println("bbbb");
productDao=new ProductDAOImpl();
try{
productDao.findAll();
}catch (Exception e) {
// TODO: handle exception
System.out.println("调用DAO错误:"+e);
}
return null;
}
}
DAO类
package com.linky.dao;import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.linky.entity.Product;/**
* A data access object (DAO) providing persistence and search support for
* Product entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see com.linky.entity.Product
* @author MyEclipse Persistence Tools
*/public class ProductDAOImpl extends HibernateDaoSupport implements ProductDAO {
private static final Logger log = LoggerFactory.getLogger(ProductDAOImpl.class);
protected void initDao() {
// do nothing
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#save(com.linky.entity.Product)
*/
public void save(Product transientInstance) {
log.debug("saving Product instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#delete(com.linky.entity.Product)
*/
public void delete(Product persistentInstance) {
log.debug("deleting Product instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findById(java.lang.Integer)
*/
public Product findById(java.lang.Integer id) {
log.debug("getting Product instance with id: " + id);
try {
Product instance = (Product) getHibernateTemplate().get(
"com.linky.entity.Product", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByExample(com.linky.entity.Product)
*/
public List findByExample(Product instance) {
log.debug("finding Product instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByProperty(java.lang.String, java.lang.Object)
*/
public List findByProperty(String propertyName, Object value) {
log.debug("finding Product instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Product as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByName(java.lang.Object)
*/
public List findByName(Object name) {
return findByProperty(NAME, name);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findBySupplier(java.lang.Object)
*/
public List findBySupplier(Object supplier) {
return findByProperty(SUPPLIER, supplier);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByType(java.lang.Object)
*/
public List findByType(Object type) {
return findByProperty(TYPE, type);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByVersions(java.lang.Object)
*/
public List findByVersions(Object versions) {
return findByProperty(VERSIONS, versions);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findBySpecification(java.lang.Object)
*/
public List findBySpecification(Object specification) {
return findByProperty(SPECIFICATION, specification);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByColor(java.lang.Object)
*/
public List findByColor(Object color) {
return findByProperty(COLOR, color);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findBySize(java.lang.Object)
*/
public List findBySize(Object size) {
return findByProperty(SIZE, size);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByAgreement(java.lang.Object)
*/
public List findByAgreement(Object agreement) {
return findByProperty(AGREEMENT, agreement);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findByRe(java.lang.Object)
*/
public List findByRe(Object re) {
return findByProperty(REMARK, re);
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findAll()
*/
public List findAll() {
log.debug("finding all Product instances");
try {
String queryString = "from Product";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#merge(com.linky.entity.Product)
*/
public Product merge(Product detachedInstance) {
log.debug("merging Product instance");
try {
Product result = (Product) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#attachDirty(com.linky.entity.Product)
*/
public void attachDirty(Product instance) {
log.debug("attaching dirty Product instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#attachClean(com.linky.entity.Product)
*/
public void attachClean(Product instance) {
log.debug("attaching clean Product instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static ProductDAO getFromApplicationContext(ApplicationContext ctx) {
return (ProductDAO) ctx.getBean("ProductDAO");
}
}
ssss
bbbb
调用DAO错误:java.lang.NullPointerException
<property name="productDao">
<ref bean="ProductDAO"/>
</property>
</bean>
开头必须小写
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://127.0.0.1:4000/mysql">
</property>
<property name="username" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/linky/entity/Product.hbm.xml</value>
<value>com/linky/entity/Number.hbm.xml</value></list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="productDAO"/>
</property>
</bean>
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="productDAO"/>
</property>
</bean>
<bean id="productDAO" class="com.linky.dao.ProductDAOImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="userAction" class="com.linky.action.UserAction">
<property name="productDAO">
<ref bean="userProxy"/>
</property>
</bean>
</beans>页面显示的效果
{"mes":"error","productDAO":{"hibernateTemplate":null,"sessionFactory":null}}
他都已经自己定义bean了 就不用,@了
[color=#FF0000]applicationContext.xml设置[/color]
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://127.0.0.1:4000/mysql">
</property>
<property name="username" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/linky/entity/Product.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="productDAO" class="com.linky.dao.ProductDAOImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="userAction" class="com.linky.action.UserAction">
<property name="productDAO">
<ref bean="productDAO"/>
</property>
</bean>
</beans>UserAction action类代码
package com.linky.action;import com.linky.dao.ProductDAO;
import com.linky.dao.ProductDAOImpl;
import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {
private ProductDAO productDAO; public ProductDAO getProductDAO() {
return productDAO;
} public void setProductDAO(ProductDAO productDAO) {
this.productDAO = productDAO;
}
private String mes;
public String getMes() {
return mes;
} public void setMes(String mes) {
this.mes = mes;
}
public String fandAll(){
System.out.println("ssss");
try{
productDAO=new ProductDAOImpl();
int i= productDAO.findAll().size();
System.out.println("数组"+i);
}catch (Exception e) {
// TODO: handle exception
mes="error";
System.out.println("错误");
System.out.println(e);
}
return SUCCESS;
}
}
ProductDAO 接口代码
package com.linky.dao;package com.linky.dao;import java.util.List;import com.linky.entity.Product;public interface ProductDAO { // property constants
public static final String NAME = "name";
public static final String SUPPLIER = "supplier";
public static final String TYPE = "type";
public static final String VERSIONS = "versions";
public static final String SPECIFICATION = "specification";
public static final String COLOR = "color";
public static final String SIZE = "size";
public static final String AGREEMENT = "agreement";
public static final String REMARK = "re"; public abstract List findAll();}
ProductDAOImpl 接口实现类代码 package com.linky.dao;import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.linky.entity.Product;/**
* A data access object (DAO) providing persistence and search support for
* Product entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see com.linky.entity.Product
* @author MyEclipse Persistence Tools
*/public class ProductDAOImpl extends HibernateDaoSupport implements ProductDAO {
private static final Logger log = LoggerFactory.getLogger(ProductDAOImpl.class);
protected void initDao() {
// do nothing
}
/* (non-Javadoc)
* @see com.linky.dao.ProductDAO#findAll()
*/
public List findAll() {
log.debug("finding all Product instances");
try {
String queryString = "from Product";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.linky.dao.ProductDAO#attachDirty(com.linky.entity.Product)
*/
public void attachDirty(Product instance) {
log.debug("attaching dirty Product instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} /* (non-Javadoc)
* @see com.linky.dao.ProductDAO#attachClean(com.linky.entity.Product)
*/
public void attachClean(Product instance) {
log.debug("attaching clean Product instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static ProductDAO getFromApplicationContext(ApplicationContext ctx) {
return (ProductDAO) ctx.getBean("ProductDAO");
}
}页面显示效果
{"mes":"error","productDAO":{"hibernateTemplate":null,"sessionFactory":null}}myeclipse打印错误:
ssss
错误
java.lang.NullPointerException
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:428)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4638)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5294)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1366)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1454)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:295)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1379)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1537)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1526)
at java.lang.Thread.run(Thread.java:722)