您的位置:首页 > 编程语言 > Java开发

SSH框架整合步骤

2012-12-08 10:12 323 查看
SSH框架整合步骤:
 
1.建web project

2.导入struts2.0的jar包(基本的五个加上struts2-spring-plugin-2.0.14.jar)

3.导入spring的jar包,这里加hibernate关联的包,用myeclipse可以完成。

4.建hibernate的数据映射文件

5.建自己要用到的业务类,action,jsp页面。

6.配制web.xml,struts.xml,applicationContext.xml

web.xml文件:

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

<web-app
version="2.4"    
xmlns="http://java.sun.com/xml/ns/j2ee"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- 配置applicationContext.xml的路径 -->

<!-- 用于初始化Spring容器的Listener -->

<!-- 定义Struts2的FilterDispathcer的Filter -->

<!-- 定义整合SiteMesh必须的ActionContextCleanUp Filter
-->

    

    

<!-- 配置applicationContext.xml的路径 -->
<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<!-- 配置监听,spring与struts关联 -->

<!-- 用于初始化Spring容器的Listener -->
<listener>

    <listener-class>

     org.springframework.web.context.ContextLoaderListener

    </listener-class>

</listener>

    

<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>

    <filter-name>struts2</filter-name>

    <filter-class>

     org.apache.struts2.dispatcher.FilterDispatcher

    </filter-class>

</filter>

<!-- 配置struts2.0的 cleanup-->

<!-- 定义整合SiteMesh必须的ActionContextCleanUp Filter
-->
<filter>

    <filter-name>struts-cleanup</filter-name>

    <filter-class>

     org.apache.struts2.dispatcher.ActionContextCleanUp

    </filter-class>

</filter>

<filter-mapping>

    <filter-name>struts-cleanup</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。
-->
<filter-mapping>

    <filter-name>struts2</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

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>

<package
name="default"
extends="struts-default">

    <action
name="Login"
class="loginAction">

     <result
name="input">Login.jsp</result>

     <result
name="success">success.jsp</result>

    </action>

    
</package>

</struts>

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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- 定义dataSource -->

<!-- 定义sessionFactory , 这里要加hibernate的数据库表的映射文件***.hbm.xml
-->

<!-- 事务管理 -->

<!-- 事务拦截器 -->

<!-- 业务实例动态代理 -->

<!-- 定义业务处理bean -->

<!-- 定义dataSource,myeclipse配置完成 -->
<bean
id="dataSource"

    class="org.apache.commons.dbcp.BasicDataSource">

    <property
name="driverClassName"

     value="com.microsoft.jdbc.sqlserver.SQLServerDriver">

    </property>

    <property
name="url"

     value="jdbc:microsoft:sqlserver://localhost:1433">

    </property>

    <property
name="username"
value="sa"></property>

    <property
name="password"
value="sa"></property>

</bean>

<!-- 定义sessionFactory,myeclipse配置完成 , 这里要加hibernate的数据库表的映射文件***.hbm.xml
-->
<bean
id="sessionFactory"

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property
name="dataSource">

     <ref
bean="dataSource"
/>

    </property>

    <property
name="mappingResources">

     <list>

        <value>com/student/model/User.hbm.xml</value>

     </list>

    </property>

    <property
name="hibernateProperties">

     <props>

        <prop
key="hibernate.dialect">

         org.hibernate.dialect.SQLServerDialect

        </prop>

        <prop
key="hibernate.hbm2ddl.auto">update</prop>

        <prop
key="hibernate.jdbc.batch_size">20</prop>

     </props>

    </property>

</bean>

<!-- 声明事务,作用就是对一系列操作在运行时错误的情况能回滚 -->

<!-- 事务管理 -->
<bean
id="transactionManager"

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property
name="sessionFactory"
ref="sessionFactory"
/>
</bean>

    

        <!-- 事务拦截器 -->

        <bean
id="transactionInterceptor"

    class="org.springframework.transaction.interceptor.TransactionInterceptor">

    <!--    事务拦截器bean需要依赖注入一个事务管理器 -->

    <property
name="transactionManager"
ref="transactionManager"
/>

    <property
name="transactionAttributes">

     <!--    下面定义事务传播属性-->

     <props>

        <prop
key="get*">PROPAGATION_REQUIRED,readOnly</prop>

        <prop
key="*">PROPAGATION_REQUIRED</prop>

     </props>

    </property>

</bean>

        

        <!-- 业务实例动态代理 -->

<!-- 定义BeanNameAutoProxyCreator-->
<bean

    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

    <!--    指定对满足哪些bean name的bean自动生成业务代理 -->

    <property
name="beanNames">

     <!--    下面是所有需要自动创建事务代理的bean-->

     <list>

        <value>userManager</value>

     </list>

     <!--    此处可增加其他需要自动创建事务代理的bean-->

    </property>

    <!--    下面定义BeanNameAutoProxyCreator所需的事务拦截器-->

    <property
name="interceptorNames">

     <list>

        <!-- 此处可增加其他新的Interceptor -->

        <value>transactionInterceptor</value>

     </list>

    </property>

</bean>

    

    

<!-- 定义业务处理bean -->
<bean
id="userManager"

    class="com.student.service.UserManagerImpl">

    <property
name="userDao"
ref="userDao"
/>
</bean>

<bean
id="userDao"
class="com.student.dao.UserDaoHibernate">

    <property
name="sessionFactory"
ref="sessionFactory"
/>
</bean>

 
<!-- 这里一定要加:scope = "prototype",否则action实例不会更新-->

<bean
id="loginAction"
class="com.student.action.LoginAction" scope = "prototype">

    <property
name="userManger"
ref="userManager"
/>
</bean>

    

    

    
</beans>

 
以上是一个大致的流程,关键是SSH有相关jar包要导入正确,还有就是上面的三个配置文件比较重要,这两点没有问题,SSH配置就基本OK。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hibernate Struts