[转自]http://www.pben.cn
一、首先应该明白,在SSH中,spring做了两次集成,第一次是与struts做了集成,第二次是与hibernate集成。
二、下面来谈第一次集成,与struts集成,通过修改struts-config.xml 中的action type 为 org.springframework.web.struts.DelegatingActionProxy,将action的控制转到spring 容器,当转到spring容器, spring会在spring的配置文件中找与action 的path属性同名的bean从而执行相应的类的操作.
- <action
- attribute="loginForm"
- input="login.jsp"
- name="loginForm"
- path="/login"
- scope="request"
- type="org.springframework.web.struts.DelegatingActionProxy" >
- <bean name="/login" class="com.ssh.action.LoginAction" singleton="false">
- <property name="dao">
- <ref bean="UsersDAOImp"/>
- property>
- bean>
三、第二次集成,与hibernate集成, DAO 继承了org.springframework.orm.hibernate3.support.HibernateDaoSupport类,所以可以简单的利用sping提供的一些功能。 HibnernateDaoSupport实现了HibernateTemplate与SessionFactory的关联。HibernateTemplate(用HibnernateDaoSupport里的getHibernateTemplate()可以获得)对Hibernate Session进行了封装,可省去获得Session实例、事务启动与提交/回滚以及异常处理等一系列步骤,所以非常简单。
- public class UsersDAOImp extends HibernateDaoSupport implements UserDao {
- private static final Log log = LogFactory.getLog(UsersDAOImp.class);
- protected void initDao() {
- //do nothing
- }
- /* (non-Javadoc)
- * @see com.ssh.dao.impl.aa#save(com.ssh.entity.Users)
- */
- public void save(Users transientInstance) {
- log.debug("saving Users instance");
- try {
- getHibernateTemplate().save(transientInstance);
- log.debug("save successful");
- } catch (RuntimeException re) {
- log.error("save failed", re);
- throw re;
- }
- }
- /* (non-Javadoc)
- * @see com.ssh.dao.impl.aa#delete(com.ssh.entity.Users)
- */
- public void delete(Users persistentInstance) {
- log.debug("deleting Users instance");
- try {
- getHibernateTemplate().delete(persistentInstance);
- log.debug("delete successful");
- } catch (RuntimeException re) {
- log.error("delete failed", re);
- throw re;
- }
- }
- /* (non-Javadoc)
- * @see com.ssh.dao.impl.aa#findById(java.lang.Integer)
- */
- public Users findById( java.lang.Integer id) {
- log.debug("getting Users instance with id: " + id);
- try {
- Users instance = (Users) getHibernateTemplate()
- .get("com.ssh.entity.Users", id);
- return instance;
- } catch (RuntimeException re) {
- log.error("get failed", re);
- throw re;
- }
- }
- /* (non-Javadoc)
- * @see com.ssh.dao.impl.aa#findByExample(com.ssh.entity.Users)
- */
- public List findByExample(Users instance) {
- log.debug("finding Users 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.ssh.dao.impl.aa#findByProperty(java.lang.String, java.lang.Object)
- */
- public List findByProperty(String propertyName, Object value) {
- log.debug("finding Users instance with property: " + propertyName
- + ", value: " + value);
- try {
- String queryString = "from Users as model where model."
- + propertyName + "= ?";
- return getHibernateTemplate().find(queryString, value);
- } catch (RuntimeException re) {
- log.error("find by property name failed", re);
- throw re;
- }
- }
</property>
代码不全。
四、当然,你得首先把spring环境搭起来,通过在struts-config.xml
增加
xml 代码
<plug-in classname="org.springframework.web.struts.ContextLoaderPlugIn"></plug-in>
- <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/applicationContext.xml" />
- plug-in>