三大框架整合模板ssh
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的更多相关文章
- JavaWeb_(SSH)三大框架整合struts+hibernate+spring_Demo
三大框架整合 一.SSH导包 二.书写Spring 三.书写Struts 四.整合Spring与Struts 五.书写(与整合)Hibernate.引入c3p0连接池并使用hibernate模板 六. ...
- Maven SSH三大框架整合的加载流程
<Maven精品教程视频\day02视频\03ssh配置文件加载过程.avi;> 此课程中讲 SSH三大框架整合的加载流程,还可以,初步接触的朋友可以听一听. < \day02视频\ ...
- SSH三大框架整合配置详解
首先,三大框架整合,肯定是要导入相当多的jar包,这是不容置疑的! 这里就不一一列举了,直接截图吧: (1) 基于配置文件的整合: 第一步:我们需要在we ...
- 关于ssh三大框架整合的碎碎念
三大框架整合,无非就是一个导jar包,修改配置文件的过程.完了就没事了. 还是有很多细节性的问题 比如在spring中写applicationContext.xml文件时不提示: 解决方法如下: 如果 ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- SSM三大框架整合详细教程
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- SpringMVC详解(四)------SSM三大框架整合之登录功能实现
为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...
随机推荐
- idea设置内存大小
1.打开idea安装路径下bin,编辑.vmoptions两个文件 然后重启一下idea 2.直接打开idea的.vmoptions文件进行编辑
- Jenkins+TestNG+gitlab+maven持续集成
准备工作: 1.安装Jenkins 网上有jenkins安装配置教程 2.jenkins配置 2.1全局工具配置 配置JDK JDK别名:名称可以随意,但是要方便识别 JAVA_HOME:centos ...
- Cannot find bounds of current function
MinGW编译平台的应用程序使用libcef.dll,当调用cef的capi接口时程序崩溃.调试单步到cef capi函数时,调试器报错“Cannot find bounds of current f ...
- Clean code 关于注释、函数、命名的感想
最近在看代码整洁之道(Clean code)这本书,其实看的有点痛苦,因为越看就会越想自己写的代码是什么鬼?一些不知所云的命名,不整洁的代码格式,本想诠释代码的意思却添加了一段段废话,还有那些被强制加 ...
- 学习数据结构Day3
栈和队列 栈是一种线性结构,相比数组 他对应的操作是数组的子集 只能从一端进入,也只能从一端取出 这一端成为栈顶 栈是一种先进后出的数据结构,Last In First Out(LIFO) 程序调用的 ...
- Java设计模式--代理模式+动态代理+CGLib代理
静态代理 抽象主题角色:声明真实主题和代理主题的共同接口. 代理主题角色:代理主题内部含有对真实主题的引用,从而在任何时候操作真实主题对象:代理主题提供一个与真实主题相同的接口,以便在任何时候都可以代 ...
- 影响MySQL的性能(一)磁盘的选择
一.磁盘的选择也是影响MySQL的性能的重大因素之一 1.使用传统的机器硬盘读取数据的过程 2.如何选择传统机器硬盘的因素 二.使用RAID增加传统机器硬盘的性能 1.什么是RAID技术 2.常见的R ...
- Java基础笔试练习(八)
1. 以下关于构造函数的描述错误的是 ( ) A.每个类有且只能有一个构造函数. B.构造函数是类的一种特殊函数,它的方法名必须与类名相同 C.构造函数的主要作用是完成对类的对象的初始化工作 D.一般 ...
- Linux 进程间通信(管道、共享内存、消息队列、信号量)
进程通信 : 不同进程之间传播或交换信息 为什么要进程通信呢? 协同运行,项目模块化 通信原理 : 给多个进程提供一个都能访问到的缓冲区. 根据使用场景,我们能划分为以下几种通信 ...
- fastjson反序列化使用不当导致内存泄露
分析一个线上内存告警的问题时,发现了造成内存告警的原因是使用fastjson不当导致的. 分析dump发现com.alibaba.fastjson.util.IdentityHashMap$Entry ...