Spring与Struts框架整合

Struts,用Action处理请求

Hibernate,操作数据库

Spring,负责对象创建

Spring与Struts框架整合的关键点在与:让Struts框架action对象的创建,交给Spring来完成

整合步骤:

1.引入jar文件

  • 引入struts相关jar文件
  • 引入spring-core相关jar文件
  • spring-web支持jar包

spring-web-3.2.5.RELEASE.jar 【Spring源码】

struts2-spring-plugin-2.3.4.1.jar 【Struts源码】

2.dao/service/action代码

dao代码:

public class UserDao {
public void save() {
System.out.println("DB:保存用户");
}
}

service代码:

public class UserService {
//IOC容器注入
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void save() {
userDao.save();
}
}

action代码:

public class UserAction extends ActionSupport {

    // springIOC容器注入
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
} public String execute() {
userService.save();
return SUCCESS;
}
}

3.配置xml

Struts.xml:struts路径与action映射配置

<?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>
<package name="user" extends="struts-default">
<action name="user" class="userAction" method="execute">
<result name="success">/index.jsp</result>
</action> </package> </struts>

bean.xml:spring ioc容器配置

三层bean.xml分开写:

bean-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="dao.UserDao"/>
</beans>

bean-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userService" class="service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
</beans>

bean-action.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userAction" class="action.UserAction">
<property name="userService" ref="userService"/>
</bean>
</beans>

web.xml:要实现两个功能:核心过滤器: 引入struts功能以及初始化spring的ioc容器

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!--1.struts配置-->
<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> <!--2.Spring配置-->
<!--以下通过查询源码获得-->
<context-param>
<!--固定值-->
<param-name>contextConfigLocation</param-name>
<!--spring配置文件的位置-->
<param-value>/WEB-INF/classes/bean*.xml</param-value>
</context-param> <!--实现以上功能的过滤器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

配置项目,运行即可

Spring与Struts框架整合的更多相关文章

  1. Spring+mybatis+struts框架整合的配置具体解释

    学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以 ...

  2. spring与struts简单整合案例

    Spring,负责对象对象创建 Struts, 用Action处理请求 Spring与Struts框架整合, 关键点:让struts框架action对象的创建,交给spring完成! 步骤: 1)引入 ...

  3. SSH(Spring SpringMVC Hibernate)框架整合

    项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构   1.导入依赖jar包 <!--单测--> <dependency&g ...

  4. 框架基础学习之--详解web+maven+spring+mybatis+struts框架文件夹作用

    详解web+maven+spring+mybatis+struts框架文件夹作用 1.程序名 2.Mybatis,mybatis是数据持久层,就是和对象类有关系的东西 3.存放java文件,xml,p ...

  5. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  6. Spring+Struts2+Hibernate框架整合流程

    一:基本步骤 新建Maven项目,导入相关依赖(推荐) 在WEB-INF的web.xml中进行配置 ————–Hibernate配置 —————- 创建entity包,创建数据库相关实体类 根据实体类 ...

  7. Spring + Spring MVC+Hibernate框架整合详细配置

    来源于:http://www.jianshu.com/p/8e2f92d0838c 具体配置参数: Spring: spring-framework-4.2.2Hibernate: hibernate ...

  8. spring+struts2+ibatis 框架整合以及解析

    一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...

  9. 框架整合——Spring与MyBatis框架整合

    Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...

随机推荐

  1. 转:Jmeter进行分布式性能测试

    由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...

  2. php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式浅谈

    做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...

  3. Android Studio的使用(九)--设置IDE编码格式

    1.打开设置 2.勾选编码格式,在这里可以设置分别设置IDE.Project.File等级别的编码格式. 3.查看.修改各个文件的编码 4.当右击编辑界面时,可以直接设置当前文件的编码

  4. html input密码显示为“*”

    1. 功能需求:HTML中,在input password输入框中输入字符将默认显示为“实体圆点”,但这里要求将实体圆点字符替换成“*”号显示. 2. 局限:鼠标光标非IE浏览器不一定显示,选择多个字 ...

  5. AutoMapper使用简单总结

    近期,在用AutoMapper整理一些模型对象映射,顺便小结一下使用的体会.难免有写得不对的地方,谢谢指出! 1. AutoMapper是一个.NET的对象映射工具,可以方便地进行对象间的赋值处理. ...

  6. VirtualBox 复制vdi文件和修改vdi的uuid

    1.复制vdi文件:VBoxManage clonehd 因为VirtualBox不允许注册重复的uuid,而每个vdi文件都有一个唯一的uuid.所以要想拷贝一份vdi文件再次在VBOX中注册,简单 ...

  7. STM32|4-20mA输出电路

    源:STM32|4-20mA输出电路 为工业场合开发的设备通常情况下都会具有4-20mA输出接口,在以往没有DAC模块的单片机系统,需要外加一主片DAC实现模拟量的控制,或者采用PWM来摸拟DA,但也 ...

  8. j2ee常用包的作用

    1.antlr-2.7.7.jar 呵呵 一句话,没有此包,hibernate不会执行hql语句   2.aopalliance-1.0.jar 这个包是AOP联盟的API包,里面包含了针对面向切面的 ...

  9. Java正则表达式细节1

     Java中使用特定的字符类别比如 \d \s \w \d 匹配数字 \s 匹配空白字符 \w 匹配数字或者字符或者下划线[a-zA-Z0-9_] 比如使员正则的时候: 使用的是2个 斜杠 @Test ...

  10. DNS与获取

    今天翻看twitter的源码的时候看到了一下内容: <link rel=”dns-prefetch” href=”http://a0.twimg.com”/> <link rel=” ...