[02:27:02] ERROR context.ContextLoader "Context initialization failed"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginManager' defined in file [D:\Tomcat 5.5\webapps\DataCenter\WEB-INF\classes\applicationContext-beans.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:927)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:890)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 39 more
[02:27:02] ERROR [localhost].[/DataCenter] "Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginManager' defined in file [D:\Tomcat 5.5\webapps\DataCenter\WEB-INF\classes\applicationContext-beans.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:927)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:890)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 39 more
由于对于这个spring的注入不是特别理解,请各位大虾帮忙指点!不胜感激!
6 个解决方案
#1
应该是注入 loginManager
这个bean时出的错 检查这块
这个bean时出的错 检查这块
#2
这是我的applicationContext.xml配置文件:
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="loginAction" class="com.test.dc.action.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property>
</bean>
<bean id="DBUtil"
class="com.adtec.datacenter.util.DBUtil">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="pageDao"
class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="loginManager"
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
其中数据库连接在hibernate.cfg.xml中配置,随后贴出
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="loginAction" class="com.test.dc.action.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property>
</bean>
<bean id="DBUtil"
class="com.adtec.datacenter.util.DBUtil">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="pageDao"
class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="loginManager"
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
其中数据库连接在hibernate.cfg.xml中配置,随后贴出
#3
以下是Hibernate.cfg.xml的配置:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- JDBC connection -->
<!-- SQL dialect -->
<!--<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@//127.0.0.1:1521/orcl
</property>
<property name="connection.username">test</property>
<property name="connection.password">000000</property>
--><!-- proxool connection pools-->
<property name="hibernate.proxool.pool_alias">pool</property>
<property name="hibernate.proxool.xml">proxoolconf.xml</property>
<property name="connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_format">true</property>
<mapping resource="com/test/dc/model/Teacher.hbm.xml" />
<mapping resource="com/test/dc/model/TUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
我这里使用了proxool连接池配置
proxoolconf.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>pool</alias>
<!--proxool只能管理由自己产生的链接-->
<driver-url>jdbc:oracle:thin:@//127.0.0.1:1521/orcl</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="test"></property>
<property name="password" value="000000"></property>
</driver-properties>
<!-- proxool自动侦查各个链接状态的毫秒数,侦查到空闲的链接就马上回收,超时的销毁 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 当连接忙时等候的最大请求数,超过这个请求将不再接收-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数 -->
<prototype-count>5</prototype-count>
<!--最大连接数 -->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数 -->
<minimum-connection-count>10</minimum-connection-count>
<!-- 自动重连 -->
<house-keeping-test-sql>select 1 from DUAL</house-keeping-test-sql>
</proxool>
</something-else-entirely>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- JDBC connection -->
<!-- SQL dialect -->
<!--<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@//127.0.0.1:1521/orcl
</property>
<property name="connection.username">test</property>
<property name="connection.password">000000</property>
--><!-- proxool connection pools-->
<property name="hibernate.proxool.pool_alias">pool</property>
<property name="hibernate.proxool.xml">proxoolconf.xml</property>
<property name="connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_format">true</property>
<mapping resource="com/test/dc/model/Teacher.hbm.xml" />
<mapping resource="com/test/dc/model/TUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
我这里使用了proxool连接池配置
proxoolconf.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>pool</alias>
<!--proxool只能管理由自己产生的链接-->
<driver-url>jdbc:oracle:thin:@//127.0.0.1:1521/orcl</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="test"></property>
<property name="password" value="000000"></property>
</driver-properties>
<!-- proxool自动侦查各个链接状态的毫秒数,侦查到空闲的链接就马上回收,超时的销毁 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 当连接忙时等候的最大请求数,超过这个请求将不再接收-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数 -->
<prototype-count>5</prototype-count>
<!--最大连接数 -->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数 -->
<minimum-connection-count>10</minimum-connection-count>
<!-- 自动重连 -->
<house-keeping-test-sql>select 1 from DUAL</house-keeping-test-sql>
</proxool>
</something-else-entirely>
#4
struts.xml只是配置了跳转,这里就不贴了,以下是我写的分页类:
package com.adtec.datacenter.util.page;
import java.util.List;
import java.util.Map;
import org.hibernate.criterion.Criterion;
public interface PageDao {
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map);
public int pageCount(int rowCount,int pageSize);
public int getTotalCount(String hql, Map map);
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map);
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions);
}
对以上接口的实现:
package com.adtec.datacenter.util.page;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageDaoImpl extends HibernateDaoSupport implements PageDao {
/**
* @function 通过条件查询对应数据
* @param pageNo : 页数
* @param pageSize:页面记录数
* @param hql : 查询语句
* @param map : 查询条件
* @return :查询结果集
*/
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map){
List<Object> result = null;
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
result = query.list();
} catch (RuntimeException re)
{
throw re;
}
return result;
}
/**
* 通过记录总数和每页记录数,计算总页数
* @param rowCount :记录总条数
* @param pageSize :每页记录数
* @return pCount :页数
*/
public int pageCount(int rowCount,int pageSize){
int pCount = 0;
if(rowCount < 0){
return -1;
}
if(pageSize < 0){
return -1;
}
if(rowCount % rowCount==0){
pCount = rowCount/rowCount;
}else{
pCount = rowCount/rowCount + 1;
}
return pCount;
}
/**
* @function 根据查询条件查询记录数的个数
* @param hql
* hql查询语句
* @param map
* 用map封装查询条件
* @return 数据库中满足查询条件的数据的条数
*/
public int getTotalCount(String hql, Map map)
{
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
Integer i = (Integer) query.list().get(0);
return i;
} catch (RuntimeException re)
{
throw re;
}
}
/**
* @function 根据查询条件查询记录数的个数
* @param pageObject :页面类实例
* @param queryStr :查询语句
* @param map :查询条件集合
* @return List :查询结果集
*/
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map)
{
int rowCount = getTotalCount(queryStr , map);
int pageCount = this.pageCount(rowCount, pageObject.getPageSize());
int pageCurrent = pageObject.getPageCurrent();
pageObject.setPageCount(pageCount);
pageObject.setPageCurrent(pageCurrent);
pageObject.setRowCount(rowCount);
Query query = this.getSession().createQuery(queryStr);
query.setFirstResult((pageCurrent - 1) * pageObject.getPageSize());
query.setMaxResults(pageObject.getPageSize());
List list = query.list();
return list;
}
/**
* @function 分页显示符合所有的记录数,将查询结果封装为page
* @param pageCurrent
* 当前页数
* @param pageSize
* 每页显示的条数
* @param criterions
* 不定参数Criterion
* @return 查询结果Pager
*/
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions) {
PageInfo page = null;
try {
Criteria criteria = this.getSession().createCriteria(
Class.forName(fullClassName));
if (criterions != null) {
for (Criterion criterion : criterions) {
if (criterion != null) {
criteria.add(criterion);
}
}
}
// 获取根据条件分页查询的总行数
int rowCount = (Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult();
// 通过总行数和每页条数,计算页数
int pageCount = this.pageCount(rowCount, pageSize);
criteria.setProjection(null);
criteria.setFirstResult((pageCurrent - 1) * pageSize);
criteria.setMaxResults(pageSize);
List result = criteria.list();
page = new PageInfo(rowCount, pageCount, pageSize, pageCurrent,
result);
} catch (RuntimeException re) {
throw re;
} finally {
return page;
}
}
}
package com.adtec.datacenter.util.page;
import java.util.List;
import java.util.Map;
import org.hibernate.criterion.Criterion;
public interface PageDao {
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map);
public int pageCount(int rowCount,int pageSize);
public int getTotalCount(String hql, Map map);
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map);
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions);
}
对以上接口的实现:
package com.adtec.datacenter.util.page;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageDaoImpl extends HibernateDaoSupport implements PageDao {
/**
* @function 通过条件查询对应数据
* @param pageNo : 页数
* @param pageSize:页面记录数
* @param hql : 查询语句
* @param map : 查询条件
* @return :查询结果集
*/
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map){
List<Object> result = null;
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
result = query.list();
} catch (RuntimeException re)
{
throw re;
}
return result;
}
/**
* 通过记录总数和每页记录数,计算总页数
* @param rowCount :记录总条数
* @param pageSize :每页记录数
* @return pCount :页数
*/
public int pageCount(int rowCount,int pageSize){
int pCount = 0;
if(rowCount < 0){
return -1;
}
if(pageSize < 0){
return -1;
}
if(rowCount % rowCount==0){
pCount = rowCount/rowCount;
}else{
pCount = rowCount/rowCount + 1;
}
return pCount;
}
/**
* @function 根据查询条件查询记录数的个数
* @param hql
* hql查询语句
* @param map
* 用map封装查询条件
* @return 数据库中满足查询条件的数据的条数
*/
public int getTotalCount(String hql, Map map)
{
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
Integer i = (Integer) query.list().get(0);
return i;
} catch (RuntimeException re)
{
throw re;
}
}
/**
* @function 根据查询条件查询记录数的个数
* @param pageObject :页面类实例
* @param queryStr :查询语句
* @param map :查询条件集合
* @return List :查询结果集
*/
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map)
{
int rowCount = getTotalCount(queryStr , map);
int pageCount = this.pageCount(rowCount, pageObject.getPageSize());
int pageCurrent = pageObject.getPageCurrent();
pageObject.setPageCount(pageCount);
pageObject.setPageCurrent(pageCurrent);
pageObject.setRowCount(rowCount);
Query query = this.getSession().createQuery(queryStr);
query.setFirstResult((pageCurrent - 1) * pageObject.getPageSize());
query.setMaxResults(pageObject.getPageSize());
List list = query.list();
return list;
}
/**
* @function 分页显示符合所有的记录数,将查询结果封装为page
* @param pageCurrent
* 当前页数
* @param pageSize
* 每页显示的条数
* @param criterions
* 不定参数Criterion
* @return 查询结果Pager
*/
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions) {
PageInfo page = null;
try {
Criteria criteria = this.getSession().createCriteria(
Class.forName(fullClassName));
if (criterions != null) {
for (Criterion criterion : criterions) {
if (criterion != null) {
criteria.add(criterion);
}
}
}
// 获取根据条件分页查询的总行数
int rowCount = (Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult();
// 通过总行数和每页条数,计算页数
int pageCount = this.pageCount(rowCount, pageSize);
criteria.setProjection(null);
criteria.setFirstResult((pageCurrent - 1) * pageSize);
criteria.setMaxResults(pageSize);
List result = criteria.list();
page = new PageInfo(rowCount, pageCount, pageSize, pageCurrent,
result);
} catch (RuntimeException re) {
throw re;
} finally {
return page;
}
}
}
#5
<bean id="loginManager"
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>错了
你LoginManagerImpl里面注入的是什么?没有dao??
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>错了
你LoginManagerImpl里面注入的是什么?没有dao??
#6
谢谢各位,经过仔细的检查,发现了bean注入的时候包含有问题,还是对这个架构不熟悉的缘故啊!
修改了程序,已经没问题了:
具体来说,我的LoginAction中,使用了LoginManger的实现类LoginManagerImpl
而在LoginManagerImpl中又调用了PageDao的实现类PageDaoImpl,真正的查询时在PageDaoImpl完成的
所以,applicationContext.xml应该如下配置:
<bean id="loginAction" class="com.adtec.datacenter.action.other.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property
</bean>
<bean id="loginManager" class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="pageDao" ref="pageDao"></property>
</bean>
<bean id="pageDao" class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
struts.xml只是做页面定为跳转,就很简单了!
PS:说实话,之前不是很理解,为什么每个Dao类都需要有一个接口,后来看了网上的评论,觉得自己对代码看得太死,没有理解spring的真正意义所在!
修改了程序,已经没问题了:
具体来说,我的LoginAction中,使用了LoginManger的实现类LoginManagerImpl
而在LoginManagerImpl中又调用了PageDao的实现类PageDaoImpl,真正的查询时在PageDaoImpl完成的
所以,applicationContext.xml应该如下配置:
<bean id="loginAction" class="com.adtec.datacenter.action.other.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property
</bean>
<bean id="loginManager" class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="pageDao" ref="pageDao"></property>
</bean>
<bean id="pageDao" class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
struts.xml只是做页面定为跳转,就很简单了!
PS:说实话,之前不是很理解,为什么每个Dao类都需要有一个接口,后来看了网上的评论,觉得自己对代码看得太死,没有理解spring的真正意义所在!
#1
应该是注入 loginManager
这个bean时出的错 检查这块
这个bean时出的错 检查这块
#2
这是我的applicationContext.xml配置文件:
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="loginAction" class="com.test.dc.action.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property>
</bean>
<bean id="DBUtil"
class="com.adtec.datacenter.util.DBUtil">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="pageDao"
class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="loginManager"
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
其中数据库连接在hibernate.cfg.xml中配置,随后贴出
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="loginAction" class="com.test.dc.action.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property>
</bean>
<bean id="DBUtil"
class="com.adtec.datacenter.util.DBUtil">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="pageDao"
class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="loginManager"
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
其中数据库连接在hibernate.cfg.xml中配置,随后贴出
#3
以下是Hibernate.cfg.xml的配置:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- JDBC connection -->
<!-- SQL dialect -->
<!--<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@//127.0.0.1:1521/orcl
</property>
<property name="connection.username">test</property>
<property name="connection.password">000000</property>
--><!-- proxool connection pools-->
<property name="hibernate.proxool.pool_alias">pool</property>
<property name="hibernate.proxool.xml">proxoolconf.xml</property>
<property name="connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_format">true</property>
<mapping resource="com/test/dc/model/Teacher.hbm.xml" />
<mapping resource="com/test/dc/model/TUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
我这里使用了proxool连接池配置
proxoolconf.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>pool</alias>
<!--proxool只能管理由自己产生的链接-->
<driver-url>jdbc:oracle:thin:@//127.0.0.1:1521/orcl</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="test"></property>
<property name="password" value="000000"></property>
</driver-properties>
<!-- proxool自动侦查各个链接状态的毫秒数,侦查到空闲的链接就马上回收,超时的销毁 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 当连接忙时等候的最大请求数,超过这个请求将不再接收-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数 -->
<prototype-count>5</prototype-count>
<!--最大连接数 -->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数 -->
<minimum-connection-count>10</minimum-connection-count>
<!-- 自动重连 -->
<house-keeping-test-sql>select 1 from DUAL</house-keeping-test-sql>
</proxool>
</something-else-entirely>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- JDBC connection -->
<!-- SQL dialect -->
<!--<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@//127.0.0.1:1521/orcl
</property>
<property name="connection.username">test</property>
<property name="connection.password">000000</property>
--><!-- proxool connection pools-->
<property name="hibernate.proxool.pool_alias">pool</property>
<property name="hibernate.proxool.xml">proxoolconf.xml</property>
<property name="connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_format">true</property>
<mapping resource="com/test/dc/model/Teacher.hbm.xml" />
<mapping resource="com/test/dc/model/TUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
我这里使用了proxool连接池配置
proxoolconf.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>pool</alias>
<!--proxool只能管理由自己产生的链接-->
<driver-url>jdbc:oracle:thin:@//127.0.0.1:1521/orcl</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="test"></property>
<property name="password" value="000000"></property>
</driver-properties>
<!-- proxool自动侦查各个链接状态的毫秒数,侦查到空闲的链接就马上回收,超时的销毁 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 当连接忙时等候的最大请求数,超过这个请求将不再接收-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数 -->
<prototype-count>5</prototype-count>
<!--最大连接数 -->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数 -->
<minimum-connection-count>10</minimum-connection-count>
<!-- 自动重连 -->
<house-keeping-test-sql>select 1 from DUAL</house-keeping-test-sql>
</proxool>
</something-else-entirely>
#4
struts.xml只是配置了跳转,这里就不贴了,以下是我写的分页类:
package com.adtec.datacenter.util.page;
import java.util.List;
import java.util.Map;
import org.hibernate.criterion.Criterion;
public interface PageDao {
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map);
public int pageCount(int rowCount,int pageSize);
public int getTotalCount(String hql, Map map);
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map);
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions);
}
对以上接口的实现:
package com.adtec.datacenter.util.page;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageDaoImpl extends HibernateDaoSupport implements PageDao {
/**
* @function 通过条件查询对应数据
* @param pageNo : 页数
* @param pageSize:页面记录数
* @param hql : 查询语句
* @param map : 查询条件
* @return :查询结果集
*/
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map){
List<Object> result = null;
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
result = query.list();
} catch (RuntimeException re)
{
throw re;
}
return result;
}
/**
* 通过记录总数和每页记录数,计算总页数
* @param rowCount :记录总条数
* @param pageSize :每页记录数
* @return pCount :页数
*/
public int pageCount(int rowCount,int pageSize){
int pCount = 0;
if(rowCount < 0){
return -1;
}
if(pageSize < 0){
return -1;
}
if(rowCount % rowCount==0){
pCount = rowCount/rowCount;
}else{
pCount = rowCount/rowCount + 1;
}
return pCount;
}
/**
* @function 根据查询条件查询记录数的个数
* @param hql
* hql查询语句
* @param map
* 用map封装查询条件
* @return 数据库中满足查询条件的数据的条数
*/
public int getTotalCount(String hql, Map map)
{
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
Integer i = (Integer) query.list().get(0);
return i;
} catch (RuntimeException re)
{
throw re;
}
}
/**
* @function 根据查询条件查询记录数的个数
* @param pageObject :页面类实例
* @param queryStr :查询语句
* @param map :查询条件集合
* @return List :查询结果集
*/
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map)
{
int rowCount = getTotalCount(queryStr , map);
int pageCount = this.pageCount(rowCount, pageObject.getPageSize());
int pageCurrent = pageObject.getPageCurrent();
pageObject.setPageCount(pageCount);
pageObject.setPageCurrent(pageCurrent);
pageObject.setRowCount(rowCount);
Query query = this.getSession().createQuery(queryStr);
query.setFirstResult((pageCurrent - 1) * pageObject.getPageSize());
query.setMaxResults(pageObject.getPageSize());
List list = query.list();
return list;
}
/**
* @function 分页显示符合所有的记录数,将查询结果封装为page
* @param pageCurrent
* 当前页数
* @param pageSize
* 每页显示的条数
* @param criterions
* 不定参数Criterion
* @return 查询结果Pager
*/
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions) {
PageInfo page = null;
try {
Criteria criteria = this.getSession().createCriteria(
Class.forName(fullClassName));
if (criterions != null) {
for (Criterion criterion : criterions) {
if (criterion != null) {
criteria.add(criterion);
}
}
}
// 获取根据条件分页查询的总行数
int rowCount = (Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult();
// 通过总行数和每页条数,计算页数
int pageCount = this.pageCount(rowCount, pageSize);
criteria.setProjection(null);
criteria.setFirstResult((pageCurrent - 1) * pageSize);
criteria.setMaxResults(pageSize);
List result = criteria.list();
page = new PageInfo(rowCount, pageCount, pageSize, pageCurrent,
result);
} catch (RuntimeException re) {
throw re;
} finally {
return page;
}
}
}
package com.adtec.datacenter.util.page;
import java.util.List;
import java.util.Map;
import org.hibernate.criterion.Criterion;
public interface PageDao {
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map);
public int pageCount(int rowCount,int pageSize);
public int getTotalCount(String hql, Map map);
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map);
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions);
}
对以上接口的实现:
package com.adtec.datacenter.util.page;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageDaoImpl extends HibernateDaoSupport implements PageDao {
/**
* @function 通过条件查询对应数据
* @param pageNo : 页数
* @param pageSize:页面记录数
* @param hql : 查询语句
* @param map : 查询条件
* @return :查询结果集
*/
public List<Object> findPageByQuery(int pageNo, int pageSize, String hql,
Map map){
List<Object> result = null;
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
result = query.list();
} catch (RuntimeException re)
{
throw re;
}
return result;
}
/**
* 通过记录总数和每页记录数,计算总页数
* @param rowCount :记录总条数
* @param pageSize :每页记录数
* @return pCount :页数
*/
public int pageCount(int rowCount,int pageSize){
int pCount = 0;
if(rowCount < 0){
return -1;
}
if(pageSize < 0){
return -1;
}
if(rowCount % rowCount==0){
pCount = rowCount/rowCount;
}else{
pCount = rowCount/rowCount + 1;
}
return pCount;
}
/**
* @function 根据查询条件查询记录数的个数
* @param hql
* hql查询语句
* @param map
* 用map封装查询条件
* @return 数据库中满足查询条件的数据的条数
*/
public int getTotalCount(String hql, Map map)
{
try
{
Query query = this.getSession().createQuery(hql);
Iterator it = map.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
query.setParameter(key.toString(), map.get(key));
}
Integer i = (Integer) query.list().get(0);
return i;
} catch (RuntimeException re)
{
throw re;
}
}
/**
* @function 根据查询条件查询记录数的个数
* @param pageObject :页面类实例
* @param queryStr :查询语句
* @param map :查询条件集合
* @return List :查询结果集
*/
public List queryEntitiesByPage(PageInfo pageObject, String queryStr, Map map)
{
int rowCount = getTotalCount(queryStr , map);
int pageCount = this.pageCount(rowCount, pageObject.getPageSize());
int pageCurrent = pageObject.getPageCurrent();
pageObject.setPageCount(pageCount);
pageObject.setPageCurrent(pageCurrent);
pageObject.setRowCount(rowCount);
Query query = this.getSession().createQuery(queryStr);
query.setFirstResult((pageCurrent - 1) * pageObject.getPageSize());
query.setMaxResults(pageObject.getPageSize());
List list = query.list();
return list;
}
/**
* @function 分页显示符合所有的记录数,将查询结果封装为page
* @param pageCurrent
* 当前页数
* @param pageSize
* 每页显示的条数
* @param criterions
* 不定参数Criterion
* @return 查询结果Pager
*/
public PageInfo queryEntitiesByCriteriaWihtPage(String fullClassName , int pageCurrent, int pageSize,
Criterion... criterions) {
PageInfo page = null;
try {
Criteria criteria = this.getSession().createCriteria(
Class.forName(fullClassName));
if (criterions != null) {
for (Criterion criterion : criterions) {
if (criterion != null) {
criteria.add(criterion);
}
}
}
// 获取根据条件分页查询的总行数
int rowCount = (Integer) criteria.setProjection(
Projections.rowCount()).uniqueResult();
// 通过总行数和每页条数,计算页数
int pageCount = this.pageCount(rowCount, pageSize);
criteria.setProjection(null);
criteria.setFirstResult((pageCurrent - 1) * pageSize);
criteria.setMaxResults(pageSize);
List result = criteria.list();
page = new PageInfo(rowCount, pageCount, pageSize, pageCurrent,
result);
} catch (RuntimeException re) {
throw re;
} finally {
return page;
}
}
}
#5
<bean id="loginManager"
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>错了
你LoginManagerImpl里面注入的是什么?没有dao??
class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>错了
你LoginManagerImpl里面注入的是什么?没有dao??
#6
谢谢各位,经过仔细的检查,发现了bean注入的时候包含有问题,还是对这个架构不熟悉的缘故啊!
修改了程序,已经没问题了:
具体来说,我的LoginAction中,使用了LoginManger的实现类LoginManagerImpl
而在LoginManagerImpl中又调用了PageDao的实现类PageDaoImpl,真正的查询时在PageDaoImpl完成的
所以,applicationContext.xml应该如下配置:
<bean id="loginAction" class="com.adtec.datacenter.action.other.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property
</bean>
<bean id="loginManager" class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="pageDao" ref="pageDao"></property>
</bean>
<bean id="pageDao" class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
struts.xml只是做页面定为跳转,就很简单了!
PS:说实话,之前不是很理解,为什么每个Dao类都需要有一个接口,后来看了网上的评论,觉得自己对代码看得太死,没有理解spring的真正意义所在!
修改了程序,已经没问题了:
具体来说,我的LoginAction中,使用了LoginManger的实现类LoginManagerImpl
而在LoginManagerImpl中又调用了PageDao的实现类PageDaoImpl,真正的查询时在PageDaoImpl完成的
所以,applicationContext.xml应该如下配置:
<bean id="loginAction" class="com.adtec.datacenter.action.other.LoginAction"
scope="prototype">
<property name="loginManager" ref="loginManager"></property
</bean>
<bean id="loginManager" class="com.test.dc.manager.impl.LoginManagerImpl">
<property name="pageDao" ref="pageDao"></property>
</bean>
<bean id="pageDao" class="com.adtec.datacenter.util.page.PageDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
struts.xml只是做页面定为跳转,就很简单了!
PS:说实话,之前不是很理解,为什么每个Dao类都需要有一个接口,后来看了网上的评论,觉得自己对代码看得太死,没有理解spring的真正意义所在!