Spring框架+Struts2框架第一次整合
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框架第一次整合的更多相关文章
- [ SSH框架 ] Struts2框架学习之四(自定义拦截器)
一.Struts2的拦截器 1.1 拦截器概述 拦截器,在AOP( Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截 ...
- [ SSH框架 ] Struts2框架学习之一
一.Struts2框架的概述 Struts2是一种基于MVC模式的轻量级Web框架,它自问世以来,就受到了广大Web开发者的关注,并广泛应用于各种企业系统的开发中.目前掌握 Struts2框架几乎成为 ...
- [ SSH框架 ] Struts2框架学习之二
一.Struts2访问Servlet的API 前面已经对 Struts2的流程已经执行完成了,但是如果表单中有参数如何进行接收又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习 Str ...
- spring+hibernate+struts2零配置整合
说句实话,很久都没使用SSH开发项目了,但是出于各种原因,再次记录一下整合方式,纯注解零配置. 一.前期准备工作 gradle配置文件: group 'com.bdqn.lyrk.ssh.study' ...
- [ SSH框架 ] Struts2框架学习之三(OGNl和ValueStack值栈学习)
一.OGNL概述 1.1 什么是OGNL OGNL的全称是对象图导航语言( object-graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可 ...
- Struts2,Spring, Hibernate三大框架SSH的整合步骤
整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
- Spring与Struts2整合
Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...
随机推荐
- 使用ajax实现form表单的submit事件
需求:如题,需要在登录页面使用ajax提交请求,并在本页面返回请求信息. 主要部分jS如下: //提交表单$("#loginForm").submit(function(){ va ...
- Study 6 —— 字体和段落属性
字体风格{font-style:normal | italic | oblique | inherit字体复合属性{font:font-style font-variant font-weight f ...
- javascript 学习1
1.javaScript允许对任意数据类型做比较,如boolean与number,false==0//true false === 0//false == 比较:自动转换数据类型进行比较,很多时候得到 ...
- C# dll 在注册表中寻找
“{7713F78A-44DE-42BA-A1F6-3FB0BD6CA63B}”就是该Dll的唯一ID啦,每一个Dll文件都会不一样的. 但是,问题又来了,怎么样知道它的唯一ID呢?其实很简单,那就是 ...
- 尚硅谷spring_boot课堂笔记
尚硅谷spring_boot课堂笔记
- Linux - 系统资源
查看剩余内存 free -m #-/+ buffers/cache: #6458M为真实使用内存 1649M为真实剩余内存(剩余内存+缓存+缓冲器) #linux会利用所有的剩余内存作为缓存,所以要保 ...
- JavaScript之创建动态脚本
//option= {type,src,text,isCreateScriptBySrc} function createDynamicScript(option){ var script = doc ...
- luogu P2113 看球泡妹子
2333 这么水的蓝题 f[i][j] 表示看了i场比赛,小♀红的什么东西为j时小♂明的什么值 强行压维蛤蛤 剩下的转移很简单(注意i的循环顺序从后往前,01背包) (具体见代码) #include& ...
- for XX in XX结构
这是一个循环!! in 后面可跟range,enumerate,序列等 for i in [(1,2),(2,3),("gege",5)]: print (i) 执行结果: (1, ...
- 重新学习Servlet
package javax.servlet; import java.io.IOException; public interface Servlet { public void init(Servl ...