SSH 框架整合总结
2017-10-29 23:10
323 查看
1. 搭建Struts2 环境
- 创建 struts2 的配置文件: struts.xml;
- 在 web.xml 中配置 struts2 的核心过滤器;
// 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> <package name="demo" namespace="/" extends="struts-default"> <!-- action 依靠spring框架创建 --> <action name="customerAction_*" class="customerAction" method="{1}"> </action> </package> </struts> // web.xml <!-- 配置 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>
2. 搭建 Spring 环境
- 创建 spring 配置文件: applicationContext.xml;
- 在 web.xml 中配置监听器: ContextLoaderListener;
// 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:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/binding/soap" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://cxf.apache.org/binding/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 配置action 对象 --> <bean id="customerAction" class="cn.itcast.web.action.CustomerAction" scope="prototype"/> </beans> // web.xml <!-- 配置Spring的核心监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 加载方式:默认只能加载 WEB-INF 目录下的配置文件; 通过上下文参数指定spring配置文件,加载 src 目录下配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
3. 搭建 Hibernate 环境
- 创建 hibernate 配置文件: hibernate.cfg.xml;
// hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 会话工厂 --> <session-factory> <!-- 数据库四大参数和方言 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/hibernate_day01 </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 为了方便调试是否在运行hibernat时,在日志中输出sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 是否在日志中对输出的 sql 语句格式化 --> <property name="hibernate.format_sql">true</property> <!-- 通过映射文件,生成数据库表结构 --> <property name="hibernate.hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
4. SSH 框架整合
4.1 Struts2 和 Spring 整合
- action 对象创建交给 Spring 创建;注意,action是多例的,配置
scope="prototype"
- 创建 action 类;
- 将 action 对象配置到 spring 配置文件中;
- 在 struts.xml 中, action节点的class属性配置为spring工厂中action对象的bean的id;
4.2 Hibernate 和 Spring 整合
- 数据源dataSource, 交给 Spring;
- SessionFactory 对象创建交给 Spring;
- 事务管理交给 Spring 管理;
// db.properties, 存放数据库四大参数 // "jdbc."表示前缀,是任意的,为了和其他参数区别; // "user"在Linux系统下有问题,"username"在 windows 下有问题 jdbc.driverClass = com.mysql.jdbc.driver jdbc.jdbcUrl = jdbc:mysql://localhost:3306/maven jdbc:user = root jdbc:password = root // applicationContext.xml <!-- 加载属性文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置dataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- XML 方式管理事务 --> <!-- 配置通知:具体增强逻辑 --> <tx:advice id="txAdvice"> <tx:attributes> <!-- 匹配业务类中方法名称 --> <tx:method name="save*"/> <tx:method name="update*"/> <tx:method name="delete*"/> <tx:method name="find*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置 AOP --> <aop:config> <!-- 配置切点: 具体哪些方法需要增强(真正被增强的方法) --> <aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="cut"/> <!-- 配置切面: 将增强的逻辑作用到切点(通知+切入点) --> <aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/> </aop:config> <!-- 注解方式管理事务 --> <!-- 1. 开启注解扫描, 2. 在service类上或者方法上使用注解 @Transactional --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置 action 对象 --> <bean id="customerAction" class="cn.itcast.web.action.CustomerAction" scope="prototype"/>
4.3 具体实现
- 创建Customer实体类,映射文件,将映射文件引入Hibernate核心配置文件;
- 创建 action,service,dao, 完成注入
-
在类中添加相应属性,并生成 set 方法;
- 在 spring 配置文件中完成注入;
- 在 struts.xml 中配置 action,以及结果视图;
参考资料
相关文章推荐
- SSH整合WebService框架XFire搭建总结
- SSH整合框架问题总结
- Maven 整合 SSH 框架
- spring mybatis springmvc框架整合配置(配置的总结和含义)
- SSH整合之框架搭建 .
- javaweb三大框架(ssh)知识点总结
- SSH整合开发的一些总结
- SSH三大框架整合步骤
- ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点
- ssm三大框架手动整合时遇到的错误与注意总结( 一)
- 整合DataTables到JavaWeb(SSH)实例总结分析
- Maven环境下整合ssh三大框架
- ssh整合问题总结--在添加商品模块实现图片(文件)的上传
- SSH三大框架整合步骤
- SSH三大框架整合需要的JAR包及大概作用(转载”蚂蚁路过“)
- 三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板
- SSH 整合框架(自整理)
- 【MarketAnalysis总结】3.0SpringMVC与Strut2的SSH整合的对比
- ssh全注解框架整合