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. google Guava包的ListenableFuture解析

     一. ListenableFuture是用来增强Future的功能的. 我们知道Future表示一个异步计算任务,当任务完成时可以得到计算结果.如果我们希望一旦计算完成就拿到结果展示给用户或者做另外 ...

  2. mips体系堆栈回溯分析与实现

    转载:http://www.sohu.com/a/118385096_468740 mips栈帧原理 Call stack 是指存放某个程序的正在运行的函数的信息的栈.Call stack 由 sta ...

  3. new和delete

    和 sizeof 类似,sizeof不是函数,它是一个操作符,它在编译期就完成了计算,在函数运行期间它已经是一个常数值了. int a; sizeof(int) = 4; sizeof(a) = 4; ...

  4. JavaScript之调试工具之断言assert

    1.单点断言 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  5. POJ3304 Segments 【线段直线相交】

    题意: 给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!. 思路: 计算几何.这道题要思考到两点: 1:把问题转化为是否存在一条直线 ...

  6. POJ1251 Jungle Roads【最小生成树】

    题意: 首先给你一个图,需要你求出最小生成树,首先输入n个节点,用大写字母表示各节点,接着说有几个点和它相连,然后给出节点与节点之间的权值.拿第二个样例举例:比如有3个节点,然后接下来有3-1行表示了 ...

  7. 第17月第7天 iOS 数组越界,防Crash处理

    1. 上面方法已经可以避免crash,为了避免冗余的代码,写一个NSArray的分类,利用runtime替换NSArray的对象方法objectAtIndex:,在这里进行判断,捕获异常: #impo ...

  8. 后端python基础

  9. extern的作用

    #include <stdio.h>extern int a;static int a;extern int b;int b;static int c;extern int c;

  10. JS直接if参数的用法

    经常在JS中见一些代码直接if(参数),然后参数调用的时候是将元素自己传下去.例如下面代码: <body> <input type="text" name=&qu ...