1:Spring框架和Struts2框架如何整合???

  Spring 负责对象创建

  Struts2 用Action处理请求

2:Spring与Struts2框架整合的关键点:

  让struts2框架action对象的创建,交给Spring完成

3:Spring框架和Struts2框架开发步骤:

  (1):引入Struts2框架的相关jar包

  (2):引入Spring框架的相关jar包

  (3):引入spring-web支持的jar包

      spring-web-3.2.5.RELEASE.jar    【去spring的lib里面找即可】
      struts2-spring-plugin-2.3.4.1.jar 【去struts2的lib里面找即可】

4:配置XML
   (1):struts.xml配置  【struts2路径与action映射配置】

      易错点:注意action的class属性是直接使用spring的IoC容器里面创建的userAction的名称即可。千万别再使用com....

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<!-- 创建包 -->
<package name="struts-spring" extends="struts-default">
<action name="user" class="userAction">
<result name="success">success.jsp</result>
</action> </package> </struts>

    (2):applicationContext.xml/bean.xml配置 【spring IoC容器配置】

 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 -->
<bean id="userDao" class="com.bie.dao.UserDao"></bean> </beans>
 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 --> <bean id="userService" class="com.bie.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 --> <bean id="userAction" class="com.bie.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>

  (3):web.xml配置  【一:核心过滤器,引入struts2功能,二:初始化spring IoC容器】 

       web.xml的配置真的很重要,也很容易出错:

       易错点:初始化spring IoC容器的时候param-value的值一定注意路径,不然一直报404~~~

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring_Struts2_20170313</display-name>
<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.xml分两部分进行配置 -->
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<!-- 如何找到这块,ctrl+shift+t 搜索StrutsPrepareAndExecuteFilter -->
<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> <!-- 1:spring配置 ,在spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle
搜索context-param找到下面这段话即可。记得就该param-value的值,如下所示;
2:param-value的值最好使用bean,这样方便引用如/WEB-INF/classes/bean-*.xml
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

5:配置好配置文件,基本算是完成了开始准备的工作,下面可以进行开发了,这里简单写了一个例子,如下所示:

  分别实现了dao层,service层,action层,记住都是使用Spring的IoC容器进行初始化对象的。

 package com.bie.dao;
/**
* @author BieHongLi
* @version 创建时间:2017年3月13日 下午1:44:27
*
*/
public class UserDao { public void daoTest(){
System.out.println("struts-spring整合的第一个项目");
}
}
 package com.bie.service;

 import com.bie.dao.UserDao;

 /**
* @author BieHongLi
* @version 创建时间:2017年3月13日 下午1:44:37
*
*/
public class UserService { private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void serviceTest(){
userDao.daoTest();
}
}
 package com.bie.action;

 import com.bie.service.UserService;
import com.opensymphony.xwork2.ActionSupport; /**
* @author BieHongLi
* @version 创建时间:2017年3月13日 下午1:43:42
*
*/
public class UserAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
} @Override
public String execute() throws Exception {
userService.serviceTest();
return SUCCESS;
} }

6:最后写一个简单的成功页面:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>struts2-spring第一次整合</title>
</head>
<body> <h1>struts2-spring第一次整合</h1> </body>
</html>

效果如下所示:

学会拆分,学会整合,开发一定要保持清醒的大脑和逻辑性,加油~~~

Spring框架+Struts2框架第一次整合的更多相关文章

  1. [ SSH框架 ] Struts2框架学习之四(自定义拦截器)

    一.Struts2的拦截器 1.1 拦截器概述 拦截器,在AOP( Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截 ...

  2. [ SSH框架 ] Struts2框架学习之一

    一.Struts2框架的概述 Struts2是一种基于MVC模式的轻量级Web框架,它自问世以来,就受到了广大Web开发者的关注,并广泛应用于各种企业系统的开发中.目前掌握 Struts2框架几乎成为 ...

  3. [ SSH框架 ] Struts2框架学习之二

    一.Struts2访问Servlet的API 前面已经对 Struts2的流程已经执行完成了,但是如果表单中有参数如何进行接收又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习 Str ...

  4. spring+hibernate+struts2零配置整合

    说句实话,很久都没使用SSH开发项目了,但是出于各种原因,再次记录一下整合方式,纯注解零配置. 一.前期准备工作 gradle配置文件: group 'com.bdqn.lyrk.ssh.study' ...

  5. [ SSH框架 ] Struts2框架学习之三(OGNl和ValueStack值栈学习)

    一.OGNL概述 1.1 什么是OGNL OGNL的全称是对象图导航语言( object-graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可 ...

  6. Struts2,Spring, Hibernate三大框架SSH的整合步骤

    整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...

  7. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  8. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  9. Spring与Struts2整合

    Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...

随机推荐

  1. FastReport.Net报表故障排除方法

    有不少开发人员在使用fastreport报表时遇到过这样的问题,报表设计器工作时,一些工具栏或者工具窗口被损坏了.此时,你应该删除配置文件,该文件是在你启动fastreport时创建的.它位于以下文件 ...

  2. LINQ to SQL 实现 GROUP BY、聚合、ORDER BY

    Ø  前言 本示例主要实现 LINQ 查询,先分组,再聚合,最后在排序.示例很简单,但是使用 LINQ 却生成了不同的 SQL 实现. 1)   采用手动编写 SQL 实现 SELECT ROW_NU ...

  3. 第15月第29天 ffmpeg AVERROR_EOF

    1. 在直播时返回AVERROR_EOF代表流结束吗?但对方还在直播,没有结束. int ret = av_read_frame(mContext, pkt); if (ret == AVERROR_ ...

  4. SpringBoot在IDEA中实现热部署

    gradle构建形式 添加依赖 compile("org.springframework.boot:spring-boot-devtools") 其他设置 步骤1 步骤2 按下 C ...

  5. Python中的部分特殊属性

    __name__ is the class name;   返回类名 __module__ is the module name in which the class was defined;   定 ...

  6. Struts局部异常与全局异常处理

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAowAAAG3CAIAAACxBJNyAAAgAElEQVR4nOy9z6tk15Ymdv4B0eRU8O

  7. Python中【__all__】的用法

    Python中[__all__]的用法 转:http://python-china.org/t/725 用 __all__ 暴露接口 Python 可以在模块级别暴露接口: __all__ = [&q ...

  8. spring mvc常用配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. H3C SNMP OID

    有两种mib-style [1]老些的设备 cpu 使用率OID: .1.3.6.1.4.1.25506.2.6.1.1.1.1.6.slot  内存使用率OID: .1.3.6.1.4.1.2550 ...

  10. mybatis异常分析jdbcType

    Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: Error setti ...