• 周日. 9月 25th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Spring 整合Struts2 + hibernate

admin

11月 28, 2021

applicationContext.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:p="http://www.springframework.org/schema/p"
    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-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="dataSource" 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="orcl"></property>
        <property name="password" value="admin"></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.Oracle9Dialect
                </prop>
                <prop key="hibernate.show_sql">
                    true
                </prop>
            </props>
        </property>
        <!--<property name="mappingResources"> <list> <value>./User.hbm.xml</value> 
            <value>./Mark.hbm.xml</value> <value>./Activity.hbm.xml</value> <value>./Comment.hbm.xml</value> 
            <value>./Photo.hbm.xml</value> <value>./Album.hbm.xml</value></list> </property> -->
        <!-- 手动添加     -->
        <property name="mappingDirectoryLocations">
            <value>classpath:com/vincent/faceLook/entity</value>
        </property>
    </bean>
    <!-- <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></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="mod*" propagation="REQUIRED" />
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!-- 手动添加     -->
    <aop:config>
        <!-- 切入点 -->
        <aop:pointcut id="newServicesPointcut" expression="execution(public * com.vincent.faceLook.dao.impl..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="newServicesPointcut" />
    </aop:config>
    
    
    <!-- 根据业务手动添加     -->
    <!-- Dao Beans -->
    <bean id="UserDao" class="com.vincent.faceLook.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    
    
    <!-- Biz Beans -->
    <bean id="UserBiz" class="com.vincent.faceLook.biz.impl.UserBizImpl">
        <property name="userDao" ref="UserDao"></property>
        <property name="baseDao" ref="UserDao"></property>
    </bean>
    
    
    
    <!--  Action Beans -->
    <bean id="BaseAction" class="com.vincent.faceLook.action.BaseAction">
        <property name="userBiz" ref="UserBiz"></property>
    </bean>
    <bean id="UserAction" class="com.vincent.faceLook.action.UserAction">
        <property name="userBiz" ref="UserBiz"></property>
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>FaceLook</display-name>
    
    
    <!-- struts2 strat -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- struts2 end -->
    
    
    
    <!-- hibernate start -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>
       <!-- 此处通常都自己写个类继承OpenSessionInViewFilter,自己控制session的一些特效 -->
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- hibernate end --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

OpenSessionInViewFilter

package com.vincent.faceLook.util;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;

public class OpenSessionInView extends OpenSessionInViewFilter {

    @Override
    protected void closeSession(Session session, SessionFactory sessionFactory) {
        // TODO Auto-generated method stub
        session.flush();
        super.closeSession(session, sessionFactory);
    }

    @Override
    protected Session getSession(SessionFactory sessionFactory)
            throws DataAccessResourceFailureException {
        // TODO Auto-generated method stub
        Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.COMMIT);
        return session;
    }

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="com.vincent.faceLook" extends="struts-default">
        <!-- 此处的class可以直接使用Spring配置的bean的name -->
        <action name="login" class="UserAction" method="login">
            <result name="success">/home.jsp</result>
        </action>
    </package>
</struts>    

发表回复

您的电子邮箱地址不会被公开。