参考资料:
1 ,三种整合 Struts 应用程序与 Spring 的方式
http://www.ibm.com/developerworks/cn/java/j-sr2.html
SSH 集成碰到的问题
1. getHibernateTemplate() 方法返回 NULL 值
***********
HibernateTemplate ttpl = getHibernateTemplate();
List rlt = ttpl.find("from User as c "); // 请注意这个 hql 语句中的 User 是
上面的操作语句是我自己写的,左看由看都没有问题。 那问题出在哪里呢?
这里注意前提是 , 你必须保证你的配置文件没有任何错误 .
#################
答案 : 问题出在 applicationContext.xml 的配置上,从页面传来的数据由 Struts 的 Action 层处理,在该类调用业务层类,不能直接进行实例化,要通过 applicationContext.xml 进行配置,通过 Spring 的 IoC 来获取。如果直接实例化,则造成其后的操作都不在 Spring 的事务管理之中。业务层调用 Hibernate 的 DAO 层时,业务层中的 DAO 对象也不能直接实例化,也要通过 Spring 的 IoC 来获取
由于我也是初学者 , 具体的原因见上面 , 从一个博客摘录而来 :
http://hi.baidu.com/no9527/blog/item/37d72bdffb8bcd1262279800.html
你应该在你的 action 里面这样使用后台的处理类 .
ApplicationContext context = this.getWebApplicationContext();
LoginDao bsDao = (LoginDao)context.getBean("loginDao"); // 通过 Spring 获取操作类
你可不要告诉我上面的两行语句你看不懂 , 那我就无语了 . 如果看不懂 , 你还是抓紧补习下 Spring 的知识吧 . 至于说 Spring 的配置文件 applicationContext.xml 中的配置如下 :
<bean id="loginDao" class="com.wellhope.dao.LoginDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
#################
2 没有正确配置 Spring 配置文件 applicationContext.xml 导致的问题 .
org.springframework.orm.hibernate.HibernateQueryException : unexpected token:
这个问题一看就知道查询的语句有问题,至于查询的条件为什么有问题,我认为无外乎以下 2 点。
(1) 本身的查询写的就有问题
这个查询,比如我的是,【 from User as c 】,
这里你就必须注意 User 不是表名,而是你配置文件配置的名字。
(2) 配置没有没有配置好。 --- 请检查你的 Spring 配置文件是否正确把 Hibernate 配置文件配置在 Spring 里面。配置的方式如下
<bean>
< property name = "mappingResources" >
< list >
< value > AM_Users.hbm.xml </ value >
< value > User.hbm.xml </ value >
</ list >
</ property >
</bean>
下面是这个异常的具体:
org.springframework.orm.hibernate.HibernateQueryException : unexpected token: as [from User as c ]; nested exception is net.sf.hibernate.QueryException : unexpected token: as [from User as c ]
net.sf.hibernate.QueryException : unexpected token: as [from User as c ]
at net.sf.hibernate.hql.FromParser.token( FromParser.java:94 )
at net.sf.hibernate.hql.ClauseParser.token( ClauseParser.java:87 )
at net.sf.hibernate.hql.PreprocessingParser.token( PreprocessingParser.java:123 )
at net.sf.hibernate.hql.ParserHelper.parse( ParserHelper.java:29 )
at net.sf.hibernate.hql.QueryTranslator.compile( QueryTranslator.java:149 )
at net.sf.hibernate.hql.QueryTranslator.compile( QueryTranslator.java:138 )
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery( SessionFactoryImpl.java:295 )
at net.sf.hibernate.impl.SessionImpl.getQueries( SessionImpl.java:1572 )
at net.sf.hibernate.impl.SessionImpl.find( SessionImpl.java:1543 )
at net.sf.hibernate.impl.QueryImpl.list( QueryImpl.java:49 )
#################