架构整合要点。

整合spring2 + struts1.2 + hibernate3.2

程序要使用spring2,需要spring插件jar包和spring的类库外。
1.web.xml配置spring的监听。

     <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>





通过contextConfigLocation上下文参数指定spring配置文件
	<context-param>
		<param-name>
			javax.servlet.jsp.jstl.fmt.localizationContext
		</param-name>
		<param-value>/i18nMessage/MessageResources</param-value>
	</context-param>



2。spring整合struts1
spring 对struts的支持有3种模式,分别是代理模式,控制器继承和插件模式,这里使用控制器模式。即采用spirng的controller处理器,实际上spring的处理器继承了struts.修改的struts如下
struts-config.xml配置
           <controller>
                    <set-property property="processorClass"    value="org.springframework.web.struts.DelegatingRequestProcessor"/>
             </controller>   



struts-action.xml和spring-action.xml配置

<action path="/departmentAction" name="departmentForm"
			attribute="departmentForm" scope="request" parameter="dispatch">
			<forward name="error" path="/error.jsp" />
			<forward name="update"
				path="/department/departmentForm.jsp" />
			<forward name="list" path="/department/department.jsp" />
			<forward name="flagAlex" path="/department/departmentflagAlex.jsp" />
			<forward name="selTreeList" path="/dialog/selectTree.jsp" />
			<forward name="treeList" path="/department/tree.jsp" />
			<forward name="treeDept" path="/serviceItem/treeDept.jsp" />	
			<forward name="selectTreeList" path="/docFlow/runFlow/selectTreeList.jsp"/>
		</action>




	<bean name="/departmentAction"
		class="com.ving.xzfw.action.DepartmentAction" scope="request">
		<property name="departmentService" ref="departmentService" />
		<property name="disService" ref="distictService" />
		<property name="dictionaryTypeService"
			ref="dictionaryTypeBOService" />
		<property name="dictionaryService" ref="dictionaryService" />
		<property name="logInfoService" ref="logInfoService" />			
	</bean>


bean的name属性一定要和action的path的名称相同



就这样struts的action便可以使用spring的IOC特性了
3spring集成hibernate
  
  <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
                  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                  <property name="url" value="jdbc:mysql://localhost:3306/music"/>
                <property name="username" value="root"/>
                  <property name="password" value="admin"/>
    </bean>
    
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"> 
           <ref bean="dataSource"/> 
        </property>     
       <property name="mappingResources"> 
         <list>
           <value>com/tlb/db/domain/User.hbm.xml</value>
           <value>com/tlb/db/domain/Song.hbm.xml</value>
         </list> 
       </property> 
       <property name="hibernateProperties"> 
          <props> 
             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
             <prop key="hibernate.show_sql">true</prop>
          </props> 
       </property> 
    </bean>

     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

     <bean name="userDao" class="com.tlb.db.dao.UserDao">
       <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    
配置好后,我们就可在DAO中调用sessionFactory进行各种操作。不再需要自己写一大堆数据库的共通类。HibernateDaoSupport 提供了丰富的方法,足够我们使用。而且由于spring对异常的封装机制,我们的程序代码,再也不需要很多的try catch 了。

public class UserDao extends HibernateDaoSupport implements IUserDao {

public void insUser(User user) throws DataAccessException {
   // TODO Auto-generated method stub
   this.getHibernateTemplate().save(user);
}

}



你可能感兴趣的:(java,spring,xml,Hibernate,struts)