struts配置spring:Struts+Spring+Hibernate精简配制文件

  1.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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener->
            org.springframework.web.context.ContextLoaderListener
        </listener->
    </listener>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet->
            org.apache.struts.action.ActionServlet
        </servlet->
        <init-param>
            <param-name>config</param-name>
            <param-value>
                /WEB-INF/struts-config.xml,/WEB-INF/struts-config-form.xml,/WEB-INF/struts-config-action.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>
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>

    <!-- The Usual Welcome File List -->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <!-- Tag Library Descriptors-->
    <jsp-config>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-bean.tld</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/struts-bean.tld
            </taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/struts-html.tld
            </taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-logic.tld</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/struts-logic.tld
            </taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-nested.tld</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/struts-nested.tld
            </taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-template.tld</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/struts-template.tld
            </taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/WEB-INF/tld/struts-tiles.tld</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/struts-tiles.tld
            </taglib-location>
        </taglib>
    </jsp-config>
</web-app>


  2.applicationContext-persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

    <!--  配置数据源 -->
    <bean id="dataSource"
        ="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>oracle.jdbc.driver.OracleDriver</value>
        </property>
        <property name="url">
            <value>jdbc:oracle:thin:@localhost:1521:ora9i</value>
        </property>
        <property name="username">
            <value>tom</value>
        </property>
        <property name="password">
            <value>tom</value>
        </property>
    </bean>

    <!-- 配置Hibernate -->
    <bean id="sessionFactory"
        ="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="mappingResources">
            <list>
                <value>com/example/entity/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

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

    <bean id="transactionInterceptor"
        ="org.springframework.transaction.erceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionProxyCreator"
        ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="proxyTargetClass" >
               <value>true</value>
          </property>
        <property name="beanNames">
            <list>
                <value>*Service</value>
            </list>
        </property>
        <property name="erceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

</beans>


  3.applicationContext-action.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

    <!-- example -->
    <bean name="/login" ="com.example.actions.LoginAction">
        <property name="userService">
            <ref bean="userService" />
        </property>
    </bean>

</beans>


  4.applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

    <!-- example -->
    <bean id="userService"
        ="com.example.service.UserService">
        <property name="userDAO">
            <ref bean="userDAO" />
        </property>
    </bean>
   
</beans>


  5.applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

    <!-- example -->
    <bean id="userDAO" ="com.example.dao.UserDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
   

</beans>


  6.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>

    <!-- Global-Forwards -->
    <global-forwards>
        <forward name="login" path="/login.do" />
        <forward name="welcome" path="/welcome.do" />
    </global-forwards>

    <!-- Resources -->
    <message-resources parameter="ApplicationResources" />

</struts-config>


  7.struts-config-action.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>
    <action-mappings>
   
        <!-- example -->
        <action path="/login" name="loginForm" scope="request"
            type="org.springframework.web.struts.DelegatingActionProxy" input="/login.jsp">
            <forward name="success" path="/welcome.jsp" />
            <forward name="failure" path="/login.jsp" />
        </action>
       
       
    </action-mappings>
</struts-config>




  8.struts-config-form.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>
    <form-beans>

        <!-- example -->
        <form-bean name="loginForm"
            type="com.example.forms.LoginForm">
        </form-bean>


    </form-beans>
</struts-config>




Tags:  springhibernate struts2hibernate strutshibernate struts配置spring

延伸阅读

最新评论

发表评论