第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4)

第二步:编写web.xml 和struts.xml和applicationContext.xml和applicationContext-service.xml和application-actionContext.xml和applicationContext-dao.xml

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>
<description>
将applicationContext.xml放在src目录下,依然能够找到该配置文件
</description> <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 配置CharacterEncoding,设置字符集 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<description>
项目启动时,创建Ioc容器,将项目下所有费数据类创建对象,并注入,建立对象之间的关系
</description> <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置Spring自动管理Session. 要配置到struts过滤器之前扩大session生命周期!-->
<filter>
<filter-name>hibernateSessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 页面session配置 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config> <!-- struts2拦截器,将所有请求拦截到struts2的框架中 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

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>
<!-- 如果请求地址=actionName!methodName ,则该配置需要进行设置,否则访问地址错误-->
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 开发模式 -->
<constant name="struts.devMode" value="true" /> <!-- 编码格式过滤 -->
<constant name="struts.i18n.encoding" value="utf-8"></constant> <!-- 告诉struts.xml不要自己通过反射new,对象,去spring的ioc容器中找
action中的class='spring中Ioc容器中对象的id'
annotation注解生成对象默认情况下id值为是:类名首字符小写
需要加jar包struts-spring-plugin..jar
-->
<constant name="struts.objectFactory" value="spring"></constant> <package name="default" namespace="/" extends="struts-default">
<!-- actionName!methodName请求方式的配置 -->
<action name="StudentAction" class="StudentAction">
<result name="success">/page/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"
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.5.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="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"> </property>
<!-- 数据库地址 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"> </property>
<!-- 默认初始化获取3个连接 -->
<!-- 空闲连接检查时间 -->
<property name="idleConnectionTestPeriod" value="18000"></property>
<!-- 最大空闲连接时间 3小时 -->
<property name="maxIdleTime" value="25000"></property>
<!-- 检查获取的连接是否有效 -->
<property name="testConnectionOnCheckin" value="true"></property>
<property name="testConnectionOnCheckout" value="true"></property>
<!-- 测试语句 -->
<property name="preferredTestQuery" value="select 1"></property>
<!-- 连接数据库 -->
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="c3p0.acquire_increment">5</prop>
<prop key="c3p0.idle_test_period">18000</prop> <!-- 连接空闲超时时间 -->
<prop key="c3p0.timeout">20000</prop>
<prop key="c3p0.max_size">40</prop>
<prop key="c3p0.max_statements">100</prop>
<prop key="c3p0.min_size">10</prop>
</props>
</property>
</bean> <!-- 替代hibernate中hibernate.cfg.xml包括:连接数据库信息,实体类和表的映射桥梁,全局参数的配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--
一般有三类配置信息!!
1. 和数据库连接信息。
2. 全局参数配置 比如缓存配置, sql打印信息,等等。。
3. mapping
-->
<!-- 【1】数据库连接 -->
<property name="dataSource" >
<ref bean="dataSource"/>
</property>
<!-- 【2】参数配置 -->
<property name="hibernateProperties">
<props>
<!-- 一些hibernate框架的设置 -->
<!-- hibernate 会自动生成sql。 为了能够屏蔽 数据库的差异。 需要配置 数据库方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 如果数据库中无相应的表的话,则自动生成一个与po对应的表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 在服务器后台打印出hibernate映射的sql语句 ,格式打印sql语句-->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
</props>
</property> <!-- 【3】mapping数据模型和数据库的桥梁,只需要配置到路径,无需一个个配置 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/bjsxt/sxf/po</value>
</list>
</property>
</bean> <!-- 在ioc容器中创建HibernateTemplate对象,并将sessionFactory工厂的对象,注入其中。 -->
<bean id="hibernateTemplate" 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="sessionFactory"></property>
</bean> <!-- 动态批量代理,事务,确保实际业务逻辑的完整性,合理性。比如银行转账的事务 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 面向切面编程 -->
<aop:config proxy-target-class="true">
<!-- 切面 -->
<aop:pointcut id="serviceMethods" expression="execution(* com.bjsxt.sxf.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="serviceMethods"/>
</aop:config> <!-- 导入各种包,将bean分类。进行配置。 -->
<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
<import resource="applicationContext-action.xml"/> </beans>

applicationContext-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="StudentDao" class="com.bjsxt.sxf.dao.StudentDao" scope="prototype" >
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
</beans>

applicationContext-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="StudentService" class="com.bjsxt.sxf.service.impl.StudentServiceImpl" scope="prototype" autowire="byType"></bean>
</beans>

applicationContext-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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="StudentAction" class="com.bjsxt.sxf.action.StudentAction" scope="prototype" autowire="byType" ></bean> </beans>

第三步:测试--项目布局。

项目布局

student.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.bjsxt.sxf.po"><!-- 实体类包名 --> <class name="Student" table="shang_student"> <!-- 主键递增 -->
<id name="id" column="id">
<generator class="native"></generator>
</id> <!-- 学生名字 -->
<property name="name" column="name"></property>
<!-- 学生性别 -->
<property name="sex" column="sex"></property> </class> </hibernate-mapping>

添加一个用户

访问localhost:8080/Struts2HibernateSpring/StudentAction!add向数据库中的表添加一个用户

StudenAction省去setget方法

 public class StudentAction {
private StudentService studentService; public String add(){
System.out.println("StudentAction.add(8888888888888)");
Student student =new Student();
student.setName("shangxiaoyan");
student.setSex("女");
studentService.addStudent(student);
return null;
}

最新文章

  1. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(二)
  2. (转)Deep Learning Research Review Week 1: Generative Adversarial Nets
  3. JAVA中抽象类的一些总结
  4. mongo 安装
  5. [Buzz.Today]2013.08.06
  6. 2、netlink简介
  7. MiniProfiler.3.0.10 用于MVC4.0中不能显示SQL语句
  8. Cent OS 命令行和窗口界面默认登录切换方法
  9. [HDU 2068] RPG的错排 (错排问题)
  10. jQuery获取url参数值
  11. 【GitHub】在Mac上配置/使用Github
  12. JAVA WEBSERVICE服务端&amp;客户端的配置及调用(基于JDK)
  13. Undefined index: HTTP_RAW_POST_DATA的解决办法
  14. 【BZOJ4033】【HAOI2015】树上染色
  15. 利用webmagic获取天猫评论
  16. 如何理解Axis?
  17. iOS-拍照后裁剪,不可拖动照片的问题
  18. elasticsearch 拼音检索能力研究
  19. P1012 拼数
  20. HDU-1087-Super Jumping! Jumping! Jumping!(线性DP, 最大上升子列和)

热门文章

  1. Python笔记 #09# Basic plots with matplotlib
  2. STM32的中断系统
  3. Spark 任务提交脚本
  4. linux第四章读书笔记
  5. 20172305 2018-2019-1 《Java软件结构与数据结构》第三周学习总结
  6. git-format-patch如何指定补丁生成的Subject格式
  7. ExtJS实现分页grid paging
  8. 使用 v-cloak 防止页面加载时出现 vue.js 的变量名
  9. MaintainableCSS 《可维护性 CSS》 --- 模板篇
  10. PHP自定义XML类实现数组到XML文件的转换