三大框架整合

  一、SSH导包

  二、书写Spring

  三、书写Struts

  四、整合Spring与Struts

  五、书写(与整合)Hibernate、引入c3p0连接池并使用hibernate模板

  六、整合事务

  --完成用户登录

  项目已上传到github  传送门

  在MySQL数据库中创建spring表,添加一条假数据

  

一、SSH导包

  导入struts的jar包

  

  导入spring的jar包

  

  导入hibernate的jar包

  

  导入c3p0连接池jar包和springapo的jar包,mysql链接数据库驱动包,第四个spring整合包暂时不需要导入,下文导入的时候会说!!!

  

二、书写Spring

  准备applicationContext.xml作为spring的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
  16. </beans>

applicationContext.xml

  编写web.xml

  1. <!-- 让spring随着web项目的启动而启动 -->
  2. <listener>
  3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  4. </listener>
  5.  
  6. <!-- 读取配置文件 -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext.xml</param-value>
  10. </context-param>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>SSH</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <!-- 让spring随着web项目的启动而启动 -->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17.  
  18. <!-- 读取配置文件 -->
  19. <context-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>classpath:applicationContext.xml</param-value>
  22. </context-param>
  23.  
  24. </web-app>

web.xml

三、配置struts

  准备struts.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5.  
  6. <struts>
  7. </struts>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5.  
  6. <struts>
  7. </struts>

struts.xml

  在web.xml中开启struts

  1. <!-- 让struts启动 -->
  2. <filter>
  3. <filter-name>struts</filter-name>
  4. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>struts</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>SSH</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <!-- 让spring随着web项目的启动而启动 -->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17.  
  18. <!-- 读取配置文件 -->
  19. <context-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>classpath:applicationContext.xml</param-value>
  22. </context-param>
  23.  
  24. <!-- 让struts启动 -->
  25. <filter>
  26. <filter-name>struts</filter-name>
  27. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>struts</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33.  
  34. </web-app>

web.xml

四.1、书写Action,并在struts.xml中开启动态方法调用

  

  1. package com.Gary.domain;
  2.  
  3. public class User {
  4.  
  5. private String id;
  6. private String username;
  7. private String password;
  8.  
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getUsername() {
  16. return username;
  17. }
  18. public void setUsername(String username) {
  19. this.username = username;
  20. }
  21. public String getPassword() {
  22. return password;
  23. }
  24. public void setPassword(String password) {
  25. this.password = password;
  26. }
  27.  
  28. }

User.java

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5.  
  6. <!-- 配置包名字 -->
  7. <hibernate-mapping package="com.Gary.domain">
  8. <!-- 配置包类和数据库中的哪个表对应 -->
  9. <class name="User" table="user">
  10. <!-- 主键 -->
  11. <id name="id">
  12. <generator class="uuid"></generator>
  13. </id>
  14.  
  15. <!-- 普通字段 -->
  16. <property name="username" column="username"></property>
  17. <property name="password" column="password"></property>
  18.  
  19. </class>
  20.  
  21. </hibernate-mapping>

User.hbm.xml

  1. package com.Gary.web;
  2.  
  3. import com.Gary.domain.User;
  4. import com.Gary.service.UserService;
  5. import com.opensymphony.xwork2.ActionContext;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. import com.opensymphony.xwork2.ModelDriven;
  8.  
  9. public class UserAction extends ActionSupport implements ModelDriven<User>{
  10.  
  11. public User user = new User();
  12.  
  13. private UserService userService;
  14.  
  15. public String execute() throws Exception {
  16.  
  17. boolean success = userService.findUser(user);
  18.  
  19. if(success)
  20. {
  21. return "toIndex";
  22. }else {
  23. ActionContext.getContext().put("error", "用户名或密码错误!!!");
  24. return "login";
  25. }
  26.  
  27. }
  28.  
  29. public UserService getUserService() {
  30. return userService;
  31. }
  32.  
  33. public void setUserService(UserService userService) {
  34. this.userService = userService;
  35. }
  36.  
  37. @Override
  38. public User getModel() {
  39. return user;
  40. }
  41.  
  42. }

UserAction.java

  1. package com.Gary.service;
  2.  
  3. import com.Gary.dao.UserDao;
  4. import com.Gary.domain.User;
  5.  
  6. public class UserService {
  7.  
  8. private UserDao userDao;
  9.  
  10. public boolean findUser(User user) {
  11.  
  12. User temp = userDao.findUser(user);
  13.  
  14. return temp == null?false:true;
  15. }
  16.  
  17. public UserDao getUserDao() {
  18. return userDao;
  19. }
  20.  
  21. public void setUserDao(UserDao userDao) {
  22. this.userDao = userDao;
  23. }
  24.  
  25. }

UserService.java

  1. package com.Gary.dao;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.query.NativeQuery;
  5. import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
  6.  
  7. import com.Gary.domain.User;
  8.  
  9. public class UserDao extends HibernateDaoSupport {
  10.  
  11. public User findUser(User user) {
  12. Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
  13.  
  14. String sql = "select * from user where username = ? and password = ?";
  15. NativeQuery query = session.createSQLQuery(sql);
  16. query.setParameter(1,user.getUsername());
  17. query.setParameter(2, user.getPassword());
  18. query.addEntity(User.class);
  19.  
  20. User result = (User) query.uniqueResult();
  21.  
  22. return result;
  23. }
  24.  
  25. }

UserDao.java

  1. <struts>
  2. <!-- 开启动态方法调用 -->
  3. <constant name="struts.devMode" value="true"></constant>
  4. <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
  5.  
  6. <package name="ssh" namespace="/" extends="struts-default">
  7. <!-- 允许所有方法 -->
  8. <global-allowed-methods>regex:.*</global-allowed-methods>
  9. <!-- 配置Action -->
  10. <action name="UserAction_*" class="com.Gary.web.UserAction">
  11. <!-- 配置结果集第一个为转发,第二个为重定向 -->
  12. <result name="login">/login.jsp</result>
  13. <result name="toIndex" type="redirect">/index.html</result>
  14. </action>
  15. </package>
  16.  
  17. </struts>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5.  
  6. <struts>
  7. <!-- 开启动态方法调用 -->
  8. <constant name="struts.devMode" value="true"></constant>
  9. <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
  10.  
  11. <package name="ssh" namespace="/" extends="struts-default">
  12. <!-- 允许所有方法 -->
  13. <global-allowed-methods>regex:.*</global-allowed-methods>
  14. <!-- 配置Action -->
  15. <action name="UserAction_*" class="com.Gary.web.UserAction">
  16. <!-- 配置结果集第一个为转发,第二个为重定向 -->
  17. <result name="login">/login.jsp</result>
  18. <result name="toIndex" type="redirect">/index.html</result>
  19. </action>
  20. </package>
  21.  
  22. </struts>

struts.xml

  提交登陆<form>表单

  1. <form action="${pageContext.request.contextPath }/UserAction_login" method="post">
  2. <div class="register-box">
  3. <label for="username" class="username_label">
  4. 用 户 名
  5. <input maxlength="20" name="username" type="text"
  6. placeholder="您的用户名和登录名" />
  7. </label>
  8. <div class="tips">
  9. </div>
  10. </div>
  11. <div class="register-box">
  12. <label for="username" class="other_label">
  13. 密 码
  14. <input maxlength="20" type="password" name="password"
  15. placeholder="建议至少使用两种字符组合" />
  16. </label>
  17. <div class="tips">
  18.  
  19. </div>
  20. </div>
  21. <div class="arguement">
  22. <input type="checkbox" id="xieyi" /> 阅读并同意
  23. <a href="javascript:void(0)">《你问我答用户注册协议》</a>
  24. <a href="register.html">没有账号,立即注册</a>
  25. <div class="tips" style="color: red">
  26. <s:property value="#error"/>
  27. </div>
  28. </div>
  29. <div class="submit_btn">
  30. <button type="submit" id="submit_btn">
  31. 立 即 登录
  32. </button>
  33. </div>
  34. </form>
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib uri="/struts-tags" prefix="s" %>
  4. <!DOCTYPE html>
  5. <html>
  6.  
  7. <head>
  8. <meta charset="UTF-8">
  9.  
  10. <link rel="stylesheet" href="css/head.css" />
  11. <link rel="stylesheet" type="text/css" href="css/login.css" />
  12. </head>
  13.  
  14. <body>
  15. <div class="dvhead">
  16. <div class="dvlogo"><a href="index.html">你问我答</a></div>
  17. <div class="dvsearch">10秒钟注册账号,找到你的同学</div>
  18. <div class="dvreg">
  19. 已有账号,立即&nbsp;<a href="login.html">登录</a>
  20. </div>
  21. </div>
  22. <section class="sec">
  23. <form action="${pageContext.request.contextPath }/UserAction_login" method="post">
  24. <div class="register-box">
  25. <label for="username" class="username_label">
  26. 用 户 名
  27. <input maxlength="20" name="username" type="text"
  28. placeholder="您的用户名和登录名" />
  29. </label>
  30. <div class="tips">
  31. </div>
  32. </div>
  33. <div class="register-box">
  34. <label for="username" class="other_label">
  35. 密 码
  36. <input maxlength="20" type="password" name="password"
  37. placeholder="建议至少使用两种字符组合" />
  38. </label>
  39. <div class="tips">
  40.  
  41. </div>
  42. </div>
  43. <div class="arguement">
  44. <input type="checkbox" id="xieyi" /> 阅读并同意
  45. <a href="javascript:void(0)">《你问我答用户注册协议》</a>
  46. <a href="register.html">没有账号,立即注册</a>
  47. <div class="tips" style="color: red">
  48. <s:property value="#error"/>
  49. </div>
  50. </div>
  51. <div class="submit_btn">
  52. <button type="submit" id="submit_btn">
  53. 立 即 登录
  54. </button>
  55. </div>
  56. </form>
  57. </section>
  58. <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
  59. </body>

login.jsp

  

四.2、整合Spring与Struts

  导入struts2-spring-plugin-2.5.16.jar整合包

  在stuts.xml中让spring管理Action的创建

  1.   <!-- 让spring管理Action的创建 -->
  2.   <constant name="struts.objectFactory" value="spring"></constant>

  在applicationContext.xml中配置web层、service层、dao层的<bean>元素

  1. <!-- 配置userAciton -->
  2. <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
  3. <!-- 注入userService -->
  4. <property name="userService" ref="userService"></property>
  5. </bean>
  6.  
  7. <!-- 配置userService -->
  8. <bean name="userService" class="com.Gary.service.UserService">
  9. <!-- 注入userDao -->
  10. <property name="userDao" ref="userDao"></property>
  11. </bean>
  12.  
  13. <!-- 配置userDao -->
  14. <bean name="userDao" class="com.Gary.dao.UserDao">
  15. <!-- 注入sessionFactory (用到了HibernateDaoSupport) -->
  16. <property name="sessionFactory" ref="sessionFactory"></property>
  17. </bean>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
  16. <!-- 配置userAciton -->
  17. <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
  18. <!-- 注入userService -->
  19. <property name="userService" ref="userService"></property>
  20. </bean>
  21.  
  22. <!-- 配置userService -->
  23. <bean name="userService" class="com.Gary.service.UserService">
  24. <!-- 注入userDao -->
  25. <property name="userDao" ref="userDao"></property>
  26. </bean>
  27.  
  28. <!-- 配置userDao -->
  29. <bean name="userDao" class="com.Gary.dao.UserDao">
  30. <!-- 注入sessionFactory (用到了HibernateDaoSupport) -->
  31. <property name="sessionFactory" ref="sessionFactory"></property>
  32. </bean>
  33.  
  34. </beans>

applicationContext.xml

五、整合Hibernate、引入连接池并使用hibernate模板

  在applicationContext.xml中整合Hibernate

  1. <bean name="dataSource"
  2. class="com.mchange.v2.c3p0.ComboPooledDataSource">
  3. <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
  4. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  5. <property name="user" value="root"></property>
  6. <property name="password" value="123456"></property>
  7. </bean>
  8.  
  9. <!-- 配置sesisonFactory -->
  10. <bean name="sessionFactory"
  11. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  12. <!-- 配置dataSource -->
  13. <property name="dataSource" ref="dataSource"></property>
  14. <!-- 配置基本属性 -->
  15. <property name="hibernateProperties">
  16. <props>
  17. <!-- 方言 -->
  18. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  19. <!-- 生成表策略 -->
  20. <prop key="hibernate.hbm2ddl.auto">update</prop>
  21. <!-- 是否生成sql语句 -->
  22. <prop key="hibernate.show_sql">true</prop>
  23. <!-- 是否格式化sql -->
  24. <prop key="hibernate.format_sql">true</prop>
  25. </props>
  26. </property>
  27. <!-- 读取orm元数据 -->
  28. <property name="mappingDirectoryLocations"
  29. value="classpath:com/Gary/domain"></property>
  30.  
  31. </bean>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
  16. <bean name="dataSource"
  17. class="com.mchange.v2.c3p0.ComboPooledDataSource">
  18. <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
  19. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  20. <property name="user" value="root"></property>
  21. <property name="password" value="123456"></property>
  22. </bean>
  23.  
  24. <!-- 配置sesisonFactory -->
  25. <bean name="sessionFactory"
  26. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  27. <!-- 配置dataSource -->
  28. <property name="dataSource" ref="dataSource"></property>
  29. <!-- 配置基本属性 -->
  30. <property name="hibernateProperties">
  31. <props>
  32. <!-- 方言 -->
  33. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  34. <!-- 生成表策略 -->
  35. <prop key="hibernate.hbm2ddl.auto">update</prop>
  36. <!-- 是否生成sql语句 -->
  37. <prop key="hibernate.show_sql">true</prop>
  38. <!-- 是否格式化sql -->
  39. <prop key="hibernate.format_sql">true</prop>
  40. </props>
  41. </property>
  42. <!-- 读取orm元数据 -->
  43. <property name="mappingDirectoryLocations"
  44. value="classpath:com/Gary/domain"></property>
  45.  
  46. </bean>
  47.  
  48. <!-- 配置userAciton -->
  49. <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
  50. <!-- 注入userService -->
  51. <property name="userService" ref="userService"></property>
  52. </bean>
  53.  
  54. <!-- 配置userService -->
  55. <bean name="userService" class="com.Gary.service.UserService">
  56. <!-- 注入userDao -->
  57. <property name="userDao" ref="userDao"></property>
  58. </bean>
  59.  
  60. <!-- 配置userDao -->
  61. <bean name="userDao" class="com.Gary.dao.UserDao">
  62. <!-- 注入sessionFactory (用到了HibernateDaoSupport) -->
  63. <property name="sessionFactory" ref="sessionFactory"></property>
  64. </bean>
  65.  
  66. </beans>

applicationContext.xml

  在web.xml中扩大session范围【扩大session范围<filter>一定要放在启动struts上】

  1. <!-- 扩大session范围 -->
  2. <filter>
  3. <filter-name>openSessionInView</filter-name>
  4. <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>openSessionInView</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>SSH</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <!-- 让spring随着web项目的启动而启动 -->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17.  
  18. <!-- 读取配置文件 -->
  19. <context-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>classpath:applicationContext.xml</param-value>
  22. </context-param>
  23.  
  24. <!-- 扩大session范围 -->
  25. <filter>
  26. <filter-name>openSessionInView</filter-name>
  27. <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>openSessionInView</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33.  
  34. <!-- 让struts启动 -->
  35. <filter>
  36. <filter-name>struts</filter-name>
  37. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  38. </filter>
  39. <filter-mapping>
  40. <filter-name>struts</filter-name>
  41. <url-pattern>/*</url-pattern>
  42. </filter-mapping>
  43.  
  44. </web-app>

web.xml

六、整合事务

  在applicationContext.xml中配置事务

  1. <!-- 事务的核心管理器 -->
  2. <bean name="transactionManager"
  3. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  4. <!-- 需要sessionFactory -->
  5. <property name="sessionFactory" ref="sessionFactory"></property>
  6. </bean>
  7.  
  8. <!-- 通知 -->
  9. <tx:advice id="advice"
  10. transaction-manager="transactionManager">
  11. <tx:attributes>
  12. <tx:method name="*" />
  13. </tx:attributes>
  14. </tx:advice>
  15.  
  16. <!-- 织入 -->
  17. <aop:config>
  18. <!-- 切入点 -->
  19. <aop:pointcut
  20. expression="execution(* com.Gary.service.*.*(..))" id="pc" />
  21. <!-- 配置切面 -->
  22. <aop:advisor advice-ref="advice" pointcut-ref="pc" />
  23. </aop:config>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
  16. <bean name="dataSource"
  17. class="com.mchange.v2.c3p0.ComboPooledDataSource">
  18. <property name="jdbcUrl" value="jdbc:mysql:///spring"></property>
  19. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  20. <property name="user" value="root"></property>
  21. <property name="password" value="123456"></property>
  22. </bean>
  23.  
  24. <!-- 配置sesisonFactory -->
  25. <bean name="sessionFactory"
  26. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  27. <!-- 配置dataSource -->
  28. <property name="dataSource" ref="dataSource"></property>
  29. <!-- 配置基本属性 -->
  30. <property name="hibernateProperties">
  31. <props>
  32. <!-- 方言 -->
  33. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  34. <!-- 生成表策略 -->
  35. <prop key="hibernate.hbm2ddl.auto">update</prop>
  36. <!-- 是否生成sql语句 -->
  37. <prop key="hibernate.show_sql">true</prop>
  38. <!-- 是否格式化sql -->
  39. <prop key="hibernate.format_sql">true</prop>
  40. </props>
  41. </property>
  42. <!-- 读取orm元数据 -->
  43. <property name="mappingDirectoryLocations"
  44. value="classpath:com/Gary/domain"></property>
  45. </bean>
  46.  
  47. <!-- 事务的核心管理器 -->
  48. <bean name="transactionManager"
  49. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  50. <!-- 需要sessionFactory -->
  51. <property name="sessionFactory" ref="sessionFactory"></property>
  52. </bean>
  53.  
  54. <!-- 通知 -->
  55. <tx:advice id="advice"
  56. transaction-manager="transactionManager">
  57. <tx:attributes>
  58. <tx:method name="*" />
  59. </tx:attributes>
  60. </tx:advice>
  61.  
  62. <!-- 织入 -->
  63. <aop:config>
  64. <!-- 切入点 -->
  65. <aop:pointcut
  66. expression="execution(* com.Gary.service.*.*(..))" id="pc" />
  67. <!-- 配置切面 -->
  68. <aop:advisor advice-ref="advice" pointcut-ref="pc" />
  69. </aop:config>
  70.  
  71. <!-- 配置userAciton -->
  72. <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype">
  73. <!-- 注入userService -->
  74. <property name="userService" ref="userService"></property>
  75. </bean>
  76.  
  77. <!-- 配置userService -->
  78. <bean name="userService" class="com.Gary.service.UserService">
  79. <!-- 注入userDao -->
  80. <property name="userDao" ref="userDao"></property>
  81. </bean>
  82.  
  83. <!-- 配置userDao -->
  84. <bean name="userDao" class="com.Gary.dao.UserDao">
  85. <!-- 注入sessionFactory (用到了HibernateDaoSupport) -->
  86. <property name="sessionFactory" ref="sessionFactory"></property>
  87. </bean>
  88.  
  89. </beans>

applicationContext.xml

JavaWeb_(SSH)三大框架整合struts+hibernate+spring_Demo的更多相关文章

  1. Maven SSH三大框架整合的加载流程

    <Maven精品教程视频\day02视频\03ssh配置文件加载过程.avi;> 此课程中讲 SSH三大框架整合的加载流程,还可以,初步接触的朋友可以听一听. < \day02视频\ ...

  2. 三大框架:Struts+Hibernate+Spring

    三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...

  3. SSH三大框架整合案例

    SSH三大框架的整合   SSH三个框架的知识点 一.Hibernate框架 1. Hibernate的核心配置文件 1.1 数据库信息.连接池配置 1.2 Hibernate信息 1.3 映射配置 ...

  4. SSH三大框架整合配置详解

    首先,三大框架整合,肯定是要导入相当多的jar包,这是不容置疑的!     这里就不一一列举了,直接截图吧:             (1) 基于配置文件的整合:        第一步:我们需要在we ...

  5. 关于ssh三大框架整合的碎碎念

    三大框架整合,无非就是一个导jar包,修改配置文件的过程.完了就没事了. 还是有很多细节性的问题 比如在spring中写applicationContext.xml文件时不提示: 解决方法如下: 如果 ...

  6. SSH 三大框架整合

    Spring整合web项目 在Servlet当中直接加载配置文件,获取对象 存在问题 每次请求都会创建一个Spring的工厂,这样浪费服务器资源,应该一个项目只有一个Spring的工厂. 在服务器启动 ...

  7. 2018.11.11 Java的 三大框架:Struts+Hibernate+Spring

    ·定义:Java三大框架主要用来做WEN应用.Struts主要负责表示层的显示: Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作): Hibernate主要是数据持久化到数据库. ...

  8. SSH三大框架整合配置详细步骤(3)

    5 配置Spring2.5 5.1 基础配置 1)        导入spring包.下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6" ...

  9. JAVAEE——SSH三大框架整合(spring+struts2+hibernate)

    一.整合原理 二.导包(41个) 1.hibernate (1)hibernate/lib/required (2)hibernate/lib/jpa | java persist api java的 ...

随机推荐

  1. MySQL 聚合函数(二)Group By的修饰符——ROLLUP

    原文为MySQL 5.7 官方手册:12.20.2 GROUP BY Modifiers 一.ROLLUP 修饰符的意义 GROUP BY子句允许添加WITH ROLLUP修饰符,该修饰符可以对分组后 ...

  2. select into from与insert into select区别

    创建一个table2  向table2中插入 table1中name为11的所有行(前提table2不存在) select * into table2 from table1 where name=‘ ...

  3. hdu 1506 最大子矩阵面积

    //写动态规划的题目 要把主要问题提炼出来 这里的问题就是求area=(j-k+1)*a[i]  如果找到j k是解决这个题目的关键 这里暴力求肯定是要超时的 这里用dp来优化 #include< ...

  4. hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c ...

  5. WindowsAPI操作串口

    #include <windows.h> #include <stdio.h> int main() { //1.打开串口 HANDLE hCom; hCom = Create ...

  6. 2.3 使用 dom4j 对 xml文件进行 dom 解析

    // 使用dom4j对XML文档进行解析 CRUD public class Demo1 { //读取XML文档中第二本书的书名 <书名>javaWEB</书名> @Test ...

  7. debian设置limits.conf

    最近已经把自己的游戏框架主要功能完成得差不多了,决定将自己的开发环境从debian7升级到debian9,不然太多第三方依赖都跟不上了.debian10刚出来,MongoDB还没适配,所以暂不考虑. ...

  8. 三次样条插值 cubic spline interpolation

    什么是三次样条插值 插值(interpolation)是在已知部分数据节点(knots)的情况下,求解经过这些已知点的曲线, 然后根据得到的曲线进行未知位置点函数值预测的方法(未知点在上述已知点自变量 ...

  9. Linux基础篇之FTP服务器搭建(二)

    上一篇文章说到了搭建FTP匿名用户的访问,接下来讲解一下本地用户的登录. 一.首先先建立一个用户,这里举例:xiaoming,并为其设置密码.  二.修改配置文件. 文件:ftpusers 文件:us ...

  10. eclipse设置打开java文件目录

    1. 第一步: 2. 第二步: 3. 第三步: Location:C:/WINDOWS/explorer.exe Arguments:${container_loc}