Struts Hibernate Spring 三者结合之配置实践

时间:2021-07-31 20:31:41

一:web.xml文件的配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
  <filter>
   <filter-name>encoding</filter-name>
   <filter-class>com.yysoft.filter.Encoding</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>encoding</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <!-- 配置一个过滤器,进行session的处理 -->
 <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
   <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>
   
  <!-- 配置了spring文件的配置信息,APPLICATON -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
    <param-value>

    //同时启动这三个配置文件上,有利于管理
     /WEB-INF/applicationContext.xml,/WEB-INF/cjhbeans.xml,/WEB-INF/cjhstrutsbeans.xml       

    </param-value>
  </context-param>
 
   <!-- 和上面的一起的 -->
  <listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <!-- 配置了一个Spring的servlet,用来创建spring框架,加载配置文件 -->
  <servlet>
   <servlet-name>context</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
 
    <!-- 在web.xml中文件配置dwrServlet,这里才能使用dwr -->
   <servlet>
   <servlet-name>dwr-invoker</servlet-name>
   <servlet-class>
     org.directwebremoting.servlet.DwrServlet
   </servlet-class>
   <init-param>
     <param-name>debug</param-name><!-- debug:表示是否可以允许用户测试 -->
     <param-value>true</param-value>
   </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>dwr-invoker</servlet-name>
   <url-pattern>/dwr/*</url-pattern>
 </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

二:struts-config.xml文件的配置

<?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>
   <form-bean name="dept" type="com.yysoft.actionform.DeptForm"></form-bean>
   <form-bean name="users" type="com.yysoft.actionform.UsersForm"></form-bean>
   <form-bean name="book" type="com.yysoft.actionform.BookForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards>
    <forward name="no" path="/no.jsp" redirect="true"></forward>
  <forward name="ok" path="/ok.jsp"></forward>
  </global-forwards>
  <action-mappings>
   <!--以下是陈建华的 分隔线-->
  <action path="/dept"
    name="dept"
    attribute="dept"
  parameter="mmmm"
  type="com.yysoft.action.DeptAction">
 </action>
 <action path="/users"
    name="users"
    attribute="users"
  parameter="mmmm"
  type="com.yysoft.action.UsersAction">
 </action>
 <action path="/book"
    name="book"
    attribute="book"
  parameter="mmmm"
  type="com.yysoft.action.BookAction">
 </action>
  </action-mappings>
  <controller processorClass="com.yysoft.myProcessor.MyRequestProcessor"></controller>
  <message-resources parameter="com.yysoft.struts.ApplicationResources" />
</struts-config>

 

三:cjhbeans.xml Bean对象的配置

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <!-- 部门dao对象,因为dao继承了hibernateDAoSupport,
   需要一个sessionFacroty对象 -->
 <bean id="deptdao" class="com.yysoft.dao.DeptDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 <!-- 配置一个DAO的代理,
  该代理对象主要是其中的方法由容器对这些方法进行了事物处理 -->
 <bean id="deptdaoproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="proxyInterfaces">
   <list>
    <value>com.yysoft.idao.IDeptDao</value>
   </list>
  </property>
  <property name="target">
   <ref bean="deptdao"/>
  </property>
  <!-- PROPAGATION_REQUIRED 表示check,add。。
  readOnly 只读
  开始的方法都需要开启事务控制 -->
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="logon*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 <!--将部门业务层对象,注入对应的部门dao对象  -->
 <bean id="deptservice" class="com.yysoft.serviceimp.DeptServiceImp">
  <property name="deptdao">
   <ref bean="deptdaoproxy"/>
  </property>
 </bean>
 
 <!-- ====================用户区======================= -->
 <!-- 部门dao对象,因为dao继承了hibernateDAoSupport,
   需要一个sessionFacroty对象 -->
 <bean id="usersdao" class="com.yysoft.dao.UsersDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 <!-- 配置一个DAO的代理,
  该代理对象主要是其中的方法由容器对这些方法进行了事物处理 -->
 <bean id="usersdaoproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="proxyInterfaces">
   <list>
    <value>com.yysoft.idao.IUsersDao</value>
   </list>
  </property>
  <property name="target">
   <ref bean="usersdao"/>
  </property>
  <!-- PROPAGATION_REQUIRED 表示check,add。。
  readOnly 只读
  开始的方法都需要开启事务控制 -->
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="logon*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 <!--将部门业务层对象,注入对应的部门dao对象  -->
 <bean id="usersservice" class="com.yysoft.serviceimp.UsersServiceImp">
  <property name="iud">
   <ref bean="usersdaoproxy"/>
  </property>
 </bean>
 
 <!-- ====================BOOK区======================= -->
 <!-- BOOKdao对象,因为BOOK继承了hibernateDAoSupport,
   需要一个sessionFacroty对象 -->
 <bean id="bookdao" class="com.yysoft.dao.BookDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 <!-- 配置一个DAO的代理,
  该代理对象主要是其中的方法由容器对这些方法进行了事物处理 -->
 <bean id="bookdaoproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="proxyInterfaces">
   <list>
    <value>com.yysoft.idao.IBookDao</value>
   </list>
  </property>
  <property name="target">
   <ref bean="bookdao"/>
  </property>
  <!-- PROPAGATION_REQUIRED 表示check,add。。
  readOnly 只读
  开始的方法都需要开启事务控制 -->
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="logon*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 <!--将部门业务层对象,注入对应的部门dao对象  -->
 <bean id="bookservice" class="com.yysoft.serviceimp.BookServiceImp">
  <property name="ibd">
   <ref bean="bookdaoproxy"/>
  </property>
 </bean>
</beans>

四:cjhstrutsbeans.xml 配置Struts与service层相关联

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <!--配置由spring管理的action对象,其name和path一致 这里是管理action  -->
 
 <!--将部门Action与部门seriver相互关联 -->
 <bean name="/dept" class="com.yysoft.action.DeptAction">
  <property name="deptsi">
   <ref bean="deptservice"/>
  </property>
 </bean>
 
 <!--将用户Action与用户seriver相互关联 -->
 <bean name="/users" class="com.yysoft.action.UsersAction">
  <property name="usi">
   <ref bean="usersservice"/>
  </property>
  <property name="dsi">
   <ref bean="deptservice"/>
  </property>
 </bean>
 
 <!--将Book Action与用户seriver相互关联 -->
 <bean name="/book" class="com.yysoft.action.BookAction">
  <property name="bsi">
   <ref bean="bookservice"/>
  </property>
 </bean>
</beans>

 

五:applicationContext.xml  配置

<?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="ds" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="oracle.jdbc.driver.OracleDriver">
  </property>
  <property name="url"
   value="jdbc:oracle:thin:@localhost:1521:orcl">
  </property>
  <property name="username" value="test"></property>
  <property name="password" value="111111"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="ds" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.Oracle9Dialect
    </prop>
    <prop key="show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/yysoft/pojos/Users.hbm.xml</value>
    <value>com/yysoft/pojos/Myrole.hbm.xml</value>
    <value>com/yysoft/pojos/Dept.hbm.xml</value>
    <value>com/yysoft/pojos/Book.hbm.xml</value>
   </list>
  </property>
 </bean>
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"> </property>
 </bean>
</beans>