`
hebenben
  • 浏览: 35740 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

struts,spring,hibernate整合方式技术文章

阅读更多
方式1:
             1在struts-config.xml中所有的action的class全部写成org.springframework.web.struts.DelegatingActionProxy
            
                        <action  parameter="method"  path="/users"
                              type="org.springframework.web.struts.DelegatingActionProxy" >
                          <forward name="login" path="/WEB-INF/jsp/login.jsp" />
                        </action>           
      
             2在spring.xml中配置action
                         <bean name="/users" class="com.struts.struts.action.UsersAction">
                                <property name="dao" ref="UsersDAO"></property>
                         </bean>

             3在struts-config.xml文件中配置plug-in
                   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
                              <set-property property="contextConfigLocation"                         value="WEB-INF/classes/com/xaccp/config/action-servlet.xml,WEB-INF/classes/com/xaccp/config/spring.xml"/>
  </plug-in>






方式2:
       1 在web.xml中的<web-app>节点中加上
           <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>WEB-INF/classes/com/xaccp/config/spring.xml</param-value>
  </context-param>
  <servlet>
      <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>


    2 配置struts-config.xml中的
          <action  parameter="method"  path="/users"
                              type="org.springframework.web.struts.DelegatingActionProxy" >
                          <forward name="login" path="/WEB-INF/jsp/login.jsp" />
         </action>   


    3 在spring.xml中配置
            <bean name="/users" class="com.struts.struts.action.UsersAction">
        <property name="dao" ref="UsersDAO"></property>
    </bean>





方式3:
          在src下新建一个包如com.xaccp.config中放置action-servlet.xml,hibernate.cfg.xml,spring.xml
          三个文件
          在action-servlet.xml文件中全部放置真正的action的包名和类名,其中完成IOC的注入
             如:
                <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean name="/users" class="com.struts.struts.action.UsersAction">
        <property name="dao" ref="UsersDAO"></property>
    </bean>
</beans>

       在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>
    <property name="dialect">
        org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.url">
        jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=HR_DB
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.driver_class">
        com.microsoft.jdbc.sqlserver.SQLServerDriver
    </property>
    <property name="myeclipse.connection.profile">DBM</property>
    <property name="show_sql">true</property>
    <mapping resource="com/xaccp/po/Users.hbm.xml"/>

</session-factory>

</hibernate-configuration>


在spring.xml中配置DAO和事务等
    如:
          <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>


    <bean id="session"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>WEB-INF/classes/com/xaccp/config/hibernate.cfg.xml</value>
        </property>
    </bean>
   
   
    <bean id="jndi" class="com.xaccp.jndidemo.jnditest"></bean>
   
    <bean id="dao" class="com.xaccp.dao.loginDao">
        <property name="jndi" ref="jndi"></property>
    </bean>
    <bean id="UsersDAO" class="com.xaccp.dao.UsersDAO">
        <property name="sessionFactory">
            <ref bean="session" />
        </property>
    </bean>

    </beans>

最后在struts-config.xml中设置action和plug-in
    如:
       <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards>
      <forward name="ok" path="/ok.jsp"></forward>
      <forward name="error" path="/error.jsp"></forward>
  </global-forwards>
  <action-mappings >
    <action
      parameter="method"
      path="/users"
      type="org.springframework.web.struts.DelegatingActionProxy" >
      <forward name="login" path="/WEB-INF/jsp/login.jsp" />
      </action>
  </action-mappings>
  <message-resources parameter="com.xaccp.struts.ApplicationResources" />
 
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
      <set-property property="contextConfigLocation" value="WEB-INF/classes/com/xaccp/config/action-servlet.xml,WEB-INF/classes/com/xaccp/config/spring.xml"/>
  </plug-in>
 
</struts-config>



其中在spring.xml中的session<bean id="session"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>WEB-INF/classes/com/xaccp/config/hibernate.cfg.xml</value>
        </property>
    </bean>



不用再在spring.xml中配置数据源和sessionFactory了,可以直接使用hibernate.cfg.xml配置的信息
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics