java XML文件配置---SSH整合配置

S2SH整合配置
 

配置方法1:

***********************************web.xml*******************************

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext*.xml</param-value>
</context-param>

<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  
   <filter>
     <filter-name>lazyLoadingFilter</filter-name>
     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter>  
        <filter-name>s2</filter-name>  
        <filter-class>  
            org.apache.struts2.dispatcher.FilterDispatcher  
        </filter-class>  
    </filter>  
   
     <filter-mapping>
     <filter-name>lazyLoadingFilter</filter-name>
     <url-pattern>*.action</url-pattern>
     </filter-mapping>
   
    <filter-mapping>  
        <filter-name>s2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    
       <!--
<filter>
    <filter-name>Spring character encoding filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GBK</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>Spring character encoding filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
    -->
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

**************************aplicationContext-common.xml*****************************

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_hibernate_1?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
   
    <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
     <ref bean="dataSource"/>
   </property>
   <property name="hibernateProperties">
    <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

        <prop key="hibernate.show_sql">true</prop>    <!--显示SQL语句-->
          <prop key="hibernate.format_sql">true</prop>
    </props>
   </property>
   <property name="mappingResources">
     <list>
         <value>com/bcm/model/Author.hbm.xml</value>
     </list>
   </property>
</bean>

  
   <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
   <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="modify*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
   </tx:attributes>
</tx:advice>

<!-- 那些类的哪些方法参与事务 -->
<aop:config>
   <aop:pointcut id="allManagerMethod" expression="execution(* com.bcm.service.impl.*.*(..))"/>
   <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>

**************************aplicationContext-bean.xml*****************************

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

   <bean id="AuthorDaoImpl" class="com.bcm.dao.impl.AuthorDaoImpl">
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--Dao层的实现类,因为继承HibernateDaoSupport 所以是需要注入sessionFactory的 -->
    <bean id="AuthorServiceImpl" class="com.bcm.service.impl.AuthorServiceImpl">
       <property name="authordao" ref="AuthorDaoImpl"></property>
    </bean>
    
</beans>

**************************aplicationContext-action.xml*****************************

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns=" http://www.springframework.org/schema/beans"
      xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop=" http://www.springframework.org/schema/aop"
      xmlns:tx=" http://www.springframework.org/schema/tx"
      xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  
     <!-- 此处的id="addAction" 必须与struts.xml中class="addAction"相一致-->
     <bean id=" addAction" class="com.bcm.action.AuthorAction" scope="prototype" >
          <property name="authorservice" ref="AuthorServiceImpl"></property>
     </bean>
   
</beans>

 

 

*************************struts.xml*****************************

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
   
<struts>
      <constant name="struts.objectFactory" value="spring"></constant>

      <constant name="struts.i18n.encoding" value="gbk"></constant>
      <package name="struts2" extends="struts-default">
      
      <action name="add" class="addAction" method="add">
         <result>index.jsp</result>
      </action>

      <action name="log" class="logAction" method="add">
        <result type="redirect-action">list.action</result>
      </action>
      
       <action name="list" class="listAction" method="list">
        <result>list.jsp</result>
      </action>
          
    </package>
</struts>

 

 

 

 

 

备注:aplicationContext.xml文件 最好分为几个 如:

     *   aplicationContext-common.xml (用于配置事务,数据源...)

     *    aplicationContext-bean.xml(用于配置普通的类)

     *    aplicationContext-action.xml(用于配置action)

配置方法2:将aplicationContext-common.xml 中的数据库配置交给hibernate.cfg.xml 。 修改如下:

*****************************************aplicationContext-common.xml ***********************

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<!--
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_hibernate_1?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
   
    <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
     <ref bean="dataSource"/>
   </property>
   <property name="hibernateProperties">
    <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
       <prop key="hibernate.show_sql">true</prop>  
             <prop key="hibernate.format_sql">true</prop>
    </props>
   </property>
  
   <property name="mappingResources">
     <list>
         <value>com/bcm/model/Author.hbm.xml</value>
     </list>
   </property>
</bean>
-->

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
   </property>
</bean>
  
   <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
   <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="modify*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
   </tx:attributes>
</tx:advice>

<!-- 那些类的哪些方法参与事务 -->
<aop:config>
   <aop:pointcut id="allManagerMethod" expression="execution(* com.bcm.service.impl.*.*(..))"/>
   <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>

*********************************** hibernate.cfg.xml   ********************************

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
   <property name="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_1?characterEncoding=utf-8</property>
   <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="hibernate.connection.username">root</property>
   <property name="hibernate.connection.password">root</property>
   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="hibernate.show_sql">true</property>
   <mapping resource="com/bcm/model/Author.hbm.xml"/>
</session-factory>
</hibernate-configuration>

注:所有的配置文件都在src目录下(除了web,xml)

你可能感兴趣的:(java,ssh,职场,休闲)