一、开发环境:

(1)    OS:Windows 7

(2)    DB:MySql 5.1.6

(3)    JDK:1.8.0_17

(4)    Server:Apache Tomcat 8.0.26

(5)    IDE:Eclipse

二、SSH框架版本:

(1)    Struts2.3.2

(2)    Spring4.0.2

(3)    Hibernate5.2.5

注意:我根据需要并没有导入所有JAR 包;

三、配置Sturts2框架:

1.在eclipse新建一个项目news;

2.导入Sturts2.X的jar包;

3.然后配置在WebContext/WEB-INF/web.xml:

   <display-name>news</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>

注意:default.jsp代表一个默认首页;

注意:/*代表过滤所有;

注意:struts2的入口是一个过滤器,所以可以把struts2当过滤器来配置;

4.然后配置src/struts.xml

<!-- 第1步:先定义一个包 -->
<package name="mypck001" extends="struts-default">
<!-- myNewsAction用于spring注入 -->
<action name="NewsAction_*" class="myNewsAction" method="{1}">
<result name="success">/WEB-INF/jsp/index.jsp</result>
<result name="deleteOK">/WEB-INF/jsp/ok.jsp</result>
<result name="deleteFailed">/WEB-INF/jsp/error_delete.jsp</result>
</action>
</package>

注意:NewsAction_*代表所有以NewsAction_开头的地址;

注意:{1}代表方法名跟NewsAction_*中*的值一样;

5.然后创建4个jsp文件:default.jsp、Index.jsp、ok.jsp、error.jsp

default.jsp文件放在WEBContext目录下,很方便的跳转运行项目;


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <% response.sendRedirect("myNews2Action_getMyNewsAll.action"); %>
</body>
</html>

其他三个,为了防止用户直接访问JSP页面把它们放在WEB-INF下;

6.然后再src目录myNews2.action包下创建MyNews2Action.action;

四、配置spring框架:

1.添加spring jar包;

注意:spring框架需要通过struts2-spring-plugin-2.3.30 jar 以struts插件的形式连接struts;

2.然后配置web.xml

   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

注意:spring以监听器的形式监听所有实例的创建;

注意:applicationcontext.xml是spring的配置文件,因为它的默认查找路径在WEB—INF/classses下,而我们要把它放到src下就在这配置一下;

3.然后在src目录下创建jdbc.properties文件,目的是希望防止不该碰applicationContext.xml文件的人碰它;

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/news
jdbc.user=root
jdbc.password=123456

4.然后配置applicationContext.xml:

<context:component-scan base-package="myNews2" />

注意:它是spring注解解析器,用注解可以各种<Bean>的创建;

    <context:property-placeholder location="classpath:jdbc.properties" />

!引入jdbc.properties;

5.然后对各个类进行IOC的注入:

 @Repository
@Scope("prototype")
public class MyNewsDaoImpl implements MyNewsDao { @Resource
private SessionFactory sf;

注意:@Repository代表这个类的实例交给spring来管理;

@Controller
@Scope("prototype")
public class MyNews2Action extends ActionSupport{ @Resource
private MyNewsService ms;

注意:@Scope("prototype")代表这个类的作用范围;

@Service
@Scope("prototype")
public class MyNews2ServiceImpl implements MyNewsService { @Resource
private MyNewsDao mn;

注意:模型层用@Service,控制层用@Controller,数据层用@Repository;

注意:@Resource是JDK 解析器;spring的是@Autowired和Qualifier;

五、配置Hibernate框架:

1.添加Hibernate jar包;

2.配置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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <context:component-scan base-package="myNews2" /> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
scope="prototype">
<property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- 禁用自动提交,保护事务的完整性 -->
<prop key="hibernate.connection.autocommit">false</prop>
<!-- 开机自动生成表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>News.hbm.xml</value>
</list>
</property>
</bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!-- 每300秒检查所有连接池中的空闲连接 -->
<property name="idleConnectionTestPeriod" value="300"></property>
<!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
<property name="maxIdleTime" value="900"></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="2"></property>
</bean> </beans>

3.配置News.hbm.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
<class name="myNews2.entity.News" table="news">
<id name="id" column="id">
<generator class="native"/>
</id>
<!-- 属性对应字段 -->
<property name="title" type="string" length="50" column="title" not-null="true"></property>
<property name="content" type="text" length="50000" column="content" not-null="true"></property>
<property name="begintime" type="date" column="begintime" not-null="true"></property>
<property name="username" type="string" length="20" column="username" not-null="true"></property>
</class>
</hibernate-mapping>

测试MyNews2Action.action

启动Tomcat

测试URL:http://localhost:8080/myNews2/MyNews2Action.action

简化SSH框架的整合的更多相关文章

  1. 使用Spring注解来简化ssh框架的代码编写

     目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...

  2. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:测试SSH框架分层整合及验证事务是否有效

    测试框架分层的整合 HibernateTemplate 和 HibernateDaoSupport,这两个类是 Spring 为整合 Hibernate3 提供的两个工具类. HibernateTem ...

  3. SSH框架完全整合

    大三学期渐末,事情也挺多的,上周就开始着手整合SSH框架,到现在才真正的完成,过程中碰到了许多小问题(小问题大折腾,哭脸.jpg).本着善始善终的原则,最终把它给完成了. 本篇文章就在: win7 6 ...

  4. maven项目ssh框架的整合

    1.环境 eclipse版本:Eclipse Mars2 4.5jdk版本:1.8maven版本:apache-maven 3.3.9zhnegs这是主要的开发工具版本,ssh的各种jar包版本就不列 ...

  5. 如何用注解简化SSH框架

    一.简化代码第一步,删除映射文件,给实体类加上注解 @Entity //声明当前类为hibernate映射到数据库中的实体类 @Table(name="news") //声明tab ...

  6. ssh框架 基本整合

    struts的基本配置 <struts> <constant name="struts.devModel" value="true" /> ...

  7. J2EE SSH框架整合教程

    本文仅作为学习和研究的参考,与实际项目使用技术有所不同,由于作者水平有限,错误疏漏在所难免,请各位看官批评指教. 项目的源代码放在:https://github.com/Frank-Pei/SSHIn ...

  8. SSH框架简化

    通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91,Tom ...

  9. SSH框架简化(struts2+spring+hibernate)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...

随机推荐

  1. 菜鸟学Struts2——Interceptors

    昨天学习Struts2的Convention plugin,今天利用Convention plugin进行Interceptor学习,虽然是使用Convention plugin进行零配置开发,这只是 ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(68)-微信公众平台开发- 资源环境准备

    系列目录 前言: 本次将学习扩展企业微信公众号功能,微信公众号也是企业流量及品牌推广的主要途径,所谓工欲善其事必先利其器,调试微信必须把程序发布外网环境,导致调试速度太慢,太麻烦! 我们需要准备妥当才 ...

  3. mybatis_映射查询

    一.一对一映射查询: 第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段.此方法较为简单,企业中使用普遍. <!- ...

  4. iOS开发之再探多线程编程:Grand Central Dispatch详解

    Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...

  5. iOS开发之ReactiveCocoa下的MVVM(干货分享)

    最近工作比较忙,但还是出来更新博客了,今天给大家分享一些ReactiveCocoa以及MVVM的一些东西,干活还是比较足的.在之前发表过一篇博文,名字叫做<iOS开发之浅谈MVVM的架构设计与团 ...

  6. [原] KVM 虚拟化原理探究(5)— 网络IO虚拟化

    KVM 虚拟化原理探究(5)- 网络IO虚拟化 标签(空格分隔): KVM IO 虚拟化简介 前面的文章介绍了KVM的启动过程,CPU虚拟化,内存虚拟化原理.作为一个完整的风诺依曼计算机系统,必然有输 ...

  7. linux服务器开发一 基础

    注:本文仅限交流使用,请务用于商业用途,否则后果自负! Linux 1.Linux介绍 Linux是类Unix计算机操作系统的统称. Linux操作系统的内核的名字也是“Linux”. Linux这个 ...

  8. GJM : C#设计模式(1)——单例模式

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  9. js数组去重几种思路

    在一些后台语言中都内置了一些方法来处理数组或集合中重复的数据.但是js中并没有类似的方法,网上已经有一些方法,但是不够详细.部分代码来源于网络.个人总计如下:大致有4种思路 1)使用两次循环比较原始的 ...

  10. 仿陌陌的ios客户端+服务端源码项目

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...