1.web.xml配置

<!-- 让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>

2.db.properties配置

jdbc.jdbcUrl=jdbc:mysql:///数据库名称?characterEncoding=UTF-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=用户名
jdbc.password=密码

3.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="user" namespace="/" extends="struts-default" >

<interceptors>
<!-- 注册拦截器 -->
<interceptor name="privilegeInterceptor" class="cn/hr/web/interceptor/PrivilegeInterceptor"></interceptor> <!-- 配置拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="privilegeInterceptor">
<param name="excludeMethods">login,register</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 指定默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- 全局结果集配置 -->
<global-results>
<result name="toLogin" type="redirect" >/login.jsp</result>
</global-results>
    <global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
    <action name="action类的名字_*" class="spring注解的名称" method="{1}" >

    </action>
</package>
</struts>

4.applicationContext.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.hr"></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>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<!-- 可选配置 -->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!--自动去扫描domain下面的实体类.hbm.xml文件-->
<property name="mappingDirectoryLocations" value="classpath:cn/hr/domain" ></property>
</bean>
</beans>

5.dao层的基本配置和书写

@Repository("dao层的属性注入名称")
public class 实现类名 extends HibernateDaoSupport implements 接口名{
//固定格式
@Resource(name="sessionFactory")
public void setSF(SessionFactory sf) {
super.setSessionFactory(sf);
} }

6.service层基本配置和书写

@Service("service层的属性注入名称")
//下面的属性是设置是否只读,对于不只读的方法将true改为false
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class 实现类名 implements 接口名{
@Resource(name="dao层的属性注入名称")
private StartPageDao spd;
}

7.action层基本配置和书写

@Scope("prototype")
//设置多例
@Controller("action层的属性注入名称")
public class 实现类名 extends ActionSupport{
@Resource(name="service层的属性注入名称")
private StartPageService startpageservice;
//提供了一个供给发送前端ajax的方法
public String index() throws Exception{

// 1.访问service层的方法
List<StartPage> sp = (List<StartPage>) startpageservice.findStartImage();
// 2.将数据放回给前端
Gson gson = new Gson();
String json = gson.toJson(sp);
ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
ServletActionContext.getResponse().getWriter().write(json);
return null;
}
}

三大框架整合模板ssh的更多相关文章

  1. JavaWeb_(SSH)三大框架整合struts+hibernate+spring_Demo

    三大框架整合 一.SSH导包 二.书写Spring 三.书写Struts 四.整合Spring与Struts 五.书写(与整合)Hibernate.引入c3p0连接池并使用hibernate模板 六. ...

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

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

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

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

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

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

  5. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  6. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  7. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  8. SSM三大框架整合详细教程

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  9. SpringMVC详解(四)------SSM三大框架整合之登录功能实现

    为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...

随机推荐

  1. (转)JVM原理讲解和调优

    背景:jvm实际调优在面试时候经常被问到,所以有必要认真总结一番. 转自:JVM原理讲解和调优 四.JVM内存调优 首先需要注意的是在对JVM内存调优的时候不能只看操作系统级别Java进程所占用的内存 ...

  2. 怎么又出错了?盘点java中最容易出现的错误

    现如今,java已经广泛应用各种软件开发领域.基于面向对象的设计,java屏蔽了诸如C,C++等语言的一些复杂性,提供了垃圾回收机制,平台无关的虚拟机技术,Java创造了一种前所未有的开发方式.所以, ...

  3. 1、Ant和分布式介绍

    { 1.Maven 简介 2.配置Maven运行环境 3.Maven项目 4.Ant和Maven项目的简单对比 5.Maven项目之间的关系 6.War类型项目的创建(tomcat插件和资源拷贝插件) ...

  4. Echarts数据可视化grid直角坐标系(xAxis、yAxis)

    mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...

  5. c++ 通过sizeof运算符看内存对齐

    一.基础数据类型 基础数据类型的sizeof,包括char.short,int,long,float,double 注意:实际数值有所偏差,与系统相关 二.数组及字符串 包括字符数组.字符指针.字符串 ...

  6. 基于Hexo搭建自己的博客主页

    搭建自己博客分为两类,一种是托管到github上的,以hexo为代表,另一种是需要自己购买服务器,主要使用wordpress框架.有不花钱的效果也很不错,就没必要自己再购买服务器了,下边主要介绍下使用 ...

  7. laravel中一些非常常用的php artisan命令

    php artisan 命令在开发laravel项目中非常常用,下面是一些总结 composer config -g repo.packagist composer https://mirrors.a ...

  8. 记一次stm8l程序跑飞

    项目使用stm8l051f3做主控,CC2500做数据接收,不发送. 跑飞的现象就是,刚开始能运行,经过一段未知长度的时间,有可能是3分钟,有可能是30分钟,指示灯不再闪烁,中断按键单片机无反应. 接 ...

  9. shell分享

    shell脚本分享 一.介绍shell Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序, ...

  10. promise实现

    目录 promise实现 Promise 是 ES6 新增的语法,解决了回调地狱的问题. 可以把 Promise 看成一个状态机.初始是 pending 状态,可以通过函数 resolve 和 rej ...