Spring和Struts2整合
Spring整合Struts2
步骤:1、导入Struts2jar相关包,并且导入Struts2-Spring-plugin-2.0.11.2.jar
2、配置xml文件:配置Struts2过滤器和Spring上下文参数和监听器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name> <welcome-file-list><welcome-file>index.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><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><listener><listener-class>com.pb.listener.SpringContextLoaderListener</listener-class></listener></web-app>
3、配置Spring配置文件。
主要用来配置Struts2中的Action,之后,此Action需要被Struts2配置文件调用。例如:
<!-- 配置一个Struts2中的Action的Bean示例 --><bean id="loginAction" class="Action.LoginAction"><property name="userDao"><ref local="userDao"/></property></bean>
4、配置Struts2配置文件。
1、在Struts2配置文件中,添加一个<constent>标签,指定Action的创建工厂由Spring来完成:
<constant name="struts.objectFactory" value="spring"></constant>
2、调用Spring配置文件中的配置的Action Bean,其中,class属性指向Spring配置文件中的Bean id
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts><constant name="struts.objectFactory" value="spring"></constant><package name="default" namespace="/" extends="struts-default"><action name="login" class="loginAction" ><result name="success">index.jsp</result></action></package> </struts>
5、Struts的Action类需要继承ActionSupport
创建Action类的时候需要继承ActionSupport类
Spring整合Hibernate
1、导入Spring和Hibernate所需jar包
2、注入SessionFactory
Spring配置数据源后,可以通过配置org.springframework.orm.hibernate4.LocalSessionFactoryBean来配置SessionFactory,从而完成整合。示例:
<?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:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" 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.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><!-- 说明:下面有的Bean配置提供了多种方案,请根据需要采用某一种(别忘了注释掉其他同类方案) --><!-- 自动扫描Spring注解配置 --><context:component-scan base-package="com" /><!-- 自动加载属性配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置数据源:方法一,使用C3P0方式(推荐) --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${jdbc.driverClassName}"p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}" /><!-- 配置数据源:方法二,使用DBCP方式(不推荐) --><!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />--><!-- 配置数据源:方法三,使用JNDI方式 --><!-- <jee:jndi-lookup id="dataSource" jndi-name="${jndi.name}" />--><!-- 配置Hibernate的数据源代理工厂:方法一,使用p属性通配符,按文件名搜索匹配的映射文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource" p:mappingLocations="classpath*:/com/**/*.hbm.xml"><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop></props></property></bean><!-- 配置Hibernate的数据源代理工厂:方法二,使用list集合,按文件名搜索匹配的映射文件 --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource"><property name="mappingLocations"><list><value>classpath*:/com/**/*.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop></props></property></bean>--><!-- 配置Hibernate的数据源代理工厂:方法三,使用p属性通配符,按目录搜索映射文件 --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource" p:mappingDirectoryLocations="classpath*:/com/**/domain"><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop></props></property></bean>--><!-- 配置Hibernate的数据源代理工厂:方法四,使用hibernate.cfg.xml --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource" p:configLocation="classpath:hibernate.cfg.xml"></bean>-->
3、删除Hibernate配置文件
转载于:https://www.cnblogs.com/hjiongjiong/p/4567762.html
Spring和Struts2整合相关推荐
- spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action
在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了a ...
- Spring和Struts2整合见问题之一
最近在做一个模拟网上银行小系统时遇到了问题,自己怎么也调不出来,然后各种百度,并在多个论坛上提问,问题终于在传智论坛上被于洋老师给指出来了,真是非常感谢! 下面说一下我遇到的问题: Struts2中A ...
- Spring+Hibernate+Struts2整合所需要的Jar包
struts2.1.6 支持jar包 xwork-2.1.2.jar struts2-core-2.1.6.jar commons-logging-1.0.4.jar freemarker-2.3.1 ...
- Spring与Struts2整合的两种解决方案
http://www.itzhai.com/spring-and-struts2-integration-of-the-two-solutions.html Struts2与Spring整合的方案一: ...
- spring+hibernate+Struts2 整合(全注解及注意事项)
最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...
- 孙宇java_[JAVA] 孙宇老师Struts2+Hibernate4+Maven+EasyUI+SpringMvc+Spring+Mybatis+Maven整合课程...
资源介绍 孙宇老师Struts2+Hibernate4+Maven+EasyUI+SpringMvc+Spring+Mybatis+Maven整合课程 ===============课程目录===== ...
- struts2整合spring应用实例
我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明stru ...
- Struts2+Spring+Hibernate的整合
整体程序结构 1.maven依赖 <!--实现Struts2+Spring+Hibernate的整合 --><dependencies><!--Spring --> ...
- struts2+spring+ibatis框架整合
实现效果 Ajax 涉及框架 Struts2 ibatis Spring JQuery 准备 Jar 包 commons-collections.jar commons-dbcp.jar common ...
最新文章
- static在内存层面的作用_「C++ Primer plus 心得」9.内存模型和名称空间
- 图解虚拟机中CentOS安装使用和编程
- C++编程积累——C++实现十进制与十六进制之间的互相转换
- MessageFormat 格式化String
- 华为P20 Pro销售火爆卖断货?淘宝商家加价卖
- html 进度条roll,js实现增加数字显示的环形进度条效果
- 朋友借我10万并把房产证交给我,写了借条,并在借条上写明了用此房产做抵押,有效吗?
- 也谈Oracle异常: ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小
- 空间复杂度和时间复杂度
- Hi3519AV100开发记录
- 微信小程序设置单个页面自定义头部为背景图
- 用canvas实现一个简易的涂鸦画板
- python实现局域网内使用TCP服务器传输文件
- 微软出品的Python小白神器,真香
- Image.save()
- python配置geany教程视频_Python使用geany工具配置方法
- oracle 统计同比 环比,oracle求同比,环比函数(LAG与LEAD)的详解
- 中国移动光宽带 光猫 超级用户管理员账号密码
- LinkedIn Spark-TFRecord partitionBy案例实战
- 微信缴纳罚款无法连接服务器,为什么我的违章罚款在微信上交不了?原因竟是…...