この記事はstruts2とSpringHibernateの統合であり、Mavenの統合ではありません。トランザクションは、アノテーション方式、単純なネイティブssh統合を採用しています。
理解できない場合は問題ありません。ブロガーは、参照と調査のためにコードをcsdnにアップロードします.............
ダウンロードリンク:https://download.csdn.net/download/qq_30764991/11293457
ツール、eclipse jdk7 struts2 spring hibernateを使用し、ダウンロードして使用します
プロジェクトの構成は次のとおりです。
基本的な手順:
1つは、上記のようにWebプロジェクトsshTestを作成します。
次に、struts2とspringhibernateの3つのフレームワークの統合jarパッケージを紹介します
約46個のjarパッケージ。
3:application.xmlを構成します。このステップは非常に重要です。具体的なコードは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 开启扫描类中的注解 -->
<context:component-scan base-package="cn.demo"></context:component-scan>
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" ></property>
<property name="hibernateProperties">
<props>
<!-- mysql数据库方言 -->
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<!-- 可选配置 -->
<!--控制台显示sql语句,格式化sql语句,自动更新 -->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!-- 加载domain -->
<property name="mappingDirectoryLocations" value="classpath:cn/demo/domain" ></property>
</bean>
</beans>
4:struts.xmlコードを次のように構成します。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- # struts.objectFactory = spring 将action的创建交给spring容器
struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="demo" namespace="/" extends="struts-default" >
<!--配置全局异常处理 -->
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
</package>
</struts>
5:db.propertiesを構成します
jdbc.jdbcUrl=jdbc:mysql:///demo
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
6:web.xmlを構成するこのステップも非常に重要です。忘れないでください。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>sshTest</display-name>
<!-- 让spring随web启动而创建的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件位置参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 扩大session作用范围 注意: 任何filter一定要在struts的filter之前调用 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<!-- struts2核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
わかりました。上記の構成が完了したら、プロジェクトを開始します。
次のように:
この時点で、単純なsshネイティブ統合フレームワークの準備が整います。ブロガーの文章はとてもシンプルだと思いますか。これは始めるのに必要なテクニックです。ブロガーをフォローして記事を構成し、理解を深めることをお勧めします。さあ!革命はまだ成功していません、仲間はまだ一生懸命働く必要があります!いい加減にして................................
理解できない場合は問題ありません。ブロガーは、参照と調査のためにコードをcsdnにアップロードします.............
ダウンロードリンク:https://download.csdn.net/download/qq_30764991/11293457