ssh整合之五struts和spring整合
1.首先,我们需要先分析一下,我们的spring容器在web环境中,只需要一份就可以了
另外,就是我们的spring容器,要在我们tomcat启动的时候就创建好了(包括其中的spring的对象),怎么保证我们的spring容器能创建呢?
我们可以配合监听器来创建我们的spring容器,然后我们怎么实现我们的监听器呢?
当ServletContext创建成功,就说明tomcat正常启动了,我们使用监听器监听我们的ServletContext,如果创建成功,加载配置文件,创建spring容器
2.我们先在我们的web.xml中加入我们的监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssh_spring</display-name>
<!-- 配置spring的监听器
加载spring的配置文件,根据配置文件创建我们的spring对象
默认加载的spring配置文件位置:WEB-INF下的applicationContext.xml
-->
<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>
<filter>
<filter-name>ssh</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ssh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. 在我们的struts中根据容器获取对象
第一种是使用struts的 插件包(这种方式使用的比较少)
第二种是把struts的动作类交给spring进行管理
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring的配置文件:导入约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
"> <!-- 自定义的java对象交给spring进行管理 -->
<bean id="customerAction" scope="prototype" class="com.itheima.action.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 第三方的jar包中的java对象交给spring进行管理 -->
<!-- 创建一个hibernateTemplate对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建sessionFactory对象 -->
<!-- 当spring和hibernate整合时,这个sessionFactory的实现类是由spring提供的:LocalSessionFactoryBean,
创建sessionFactory是根据hibernate的核心配置文件
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--配置hibernate的事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理通知 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的AOP -->
<aop:config>
<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
在struts.xml中配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="customer" extends="struts-default" namespace="/customer">
<!--
1.class: 全限定类名
反射的请示创建动作类对象
class:需要修改为容器中动作类的bean的唯一标识
-->
<action name="addCustomerUI" class="customerAction" method="addCustomerUI">
<result name="success">/jsp/customer/add.jsp</result>
</action>
<action name="getAllCustomer" class="customerAction" method="getAllCustomer">
<result name="success">/jsp/customer/list.jsp</result>
</action>
</package>
</struts>
我们在CustomerAction中
package com.itheima.action; import java.util.List; import com.itheima.entity.Customer;
import com.itheima.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer(); private CustomerService customerService;
private List<Customer> customers; public void setCustomers(List<Customer> customers) {
this.customers = customers;
} public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
} public List<Customer> getCustomers() {
return customers;
} public Customer getModel() {
return customer;
} //进入添加页面
public String addCustomerUI(){
return this.SUCCESS;
}
//进入列表页面
public String getAllCustomer(){
customers = customerService.getAllCustomer();
return this.SUCCESS;
} }
最后.测试我们的结果,访问http://localhost:8080/ssh_spring/
每天进步一点点,今天就到这儿吧!
ssh整合之五struts和spring整合的更多相关文章
- 【SSH框架】系列之 Spring 整合 Hibernate 框架
1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...
- Struts2框架04 struts和spring整合
目录 1 servlet 和 filter 的异同 2 内存中的字符编码 3 gbk和utf-8的特点 4 struts和spring的整合 5 struts和spring的整合步骤 6 spring ...
- ssh整合之三hibernate和spring整合
1.拷贝我们的spring事务控制所需的jar包 2.在spring容器中配置我们的hibernateTemplate以及事务管理器 <?xml version="1.0" ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- 从0开始整合SSM框架--2.spring整合mybatis
依赖:<properties> <!-- spring版本号 --> <spring.version>4.1.3.RELEASE</spring.versio ...
- SSH开发实践part4:Spring整合Struts
1 好了,前面spring与hibernate的整合开发我们基本上讲完了,现在要开始服务层的开发,也就是处理事务的action,在这里我们需要引入spring与struts的整合.也就是将action ...
- struts与spring整合
Spring与Struts框架整合 Spring,负责对象对象创建 Struts, 用Action处理请求 Spring与Struts框架整合, 关键点:让struts框架action对象的创建,交给 ...
- struts和spring整合
开发流程: 1)引jar包,可以在配置工程中设置用户libarary,然后直接引入.如果在web-inf/lib没有用户导入的lib文件,可以参考问题0的解决方案 需要的是struts_core,sp ...
- struts+hibernate+spring整合过程常见问题收集
1.java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor缺少asm-3.3.jar2.java.lang.NoClassDefF ...
随机推荐
- H5页面基于接口实现数据交互
对于现在APP开发来说,目前流行的两个方式是原生和H5.就如同之前业界程序猿争论的BS和CS之争一样,业界对于H5和原生也有不小的争论.对于前者的争论在于PC端,后者在于移动端上体现. 那一个APP适 ...
- 原生js移动端滑动事件
移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的touch事件.处理touch事件 ...
- gdb命令调试技巧
gdb命令调试技巧 一.信息显示1.显示gdb版本 (gdb) show version2.显示gdb版权 (gdb) show version or show warranty3.启动时不显示提示信 ...
- 笔记:Hibernate 持久化类标注说明
持久化类标注 标注 @Entity:注解声明该类是一个Hibernate的持久化类 标注 @Table:指定该类映射的表 参数 name:指定映射数据库表的名称 参数 uniqueConstraint ...
- Webpack的基本配置
一.优化项目结构,创建相关的文件,项目结构如下:src文件夹存放相关js文件,index.html项目的首页面,dist文件夹是webpack 打包 目录. index.js内容为: alert('我 ...
- JVM学习五:JVM之类加载器之编译常量和主动使用
在学习了前面几节的内容后,相信大家已经对JAVA 虚拟机 加载类的过程有了一个认识和了解,那么本节,我们就继续进一步巩固前面所学知识和特殊点. 一.类的初始化回顾 类在初始化的时候,静态变量的声明语句 ...
- django “如何”系列4:如何编写自定义模板标签和过滤器
django的模板系统自带了一系列的内建标签和过滤器,一般情况下可以满足你的要求,如果觉得需更精准的模板标签或者过滤器,你可以自己编写模板标签和过滤器,然后使用{% load %}标签使用他们. 代码 ...
- linux --> 获取系统启动时间
获取系统启动时间 一.前言 时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同.linux内核里面用一个名为jiffes的常量来计算时间戳.应用层有time.getdaytim ...
- python入门(Python和Pycharm安装)
Python简介 Python是一种计算机程序设计语言,它结合了解释性.编译性.互动性和面向对象的脚本语言,非常简单易用.Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他 ...
- System V IPC 之共享内存
IPC 是进程间通信(Interprocess Communication)的缩写,通常指允许用户态进程执行系列操作的一组机制: 通过信号量与其他进程进行同步 向其他进程发送消息或者从其他进程接收消息 ...