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 ...
随机推荐
- 通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars)
通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars) 译者: FreeBlues 修订版本: 1.00 最新链接: http://www.cnblogs.com ...
- 浅谈分词算法(5)基于字的分词方法(bi-LSTM)
目录 前言 目录 循环神经网络 基于LSTM的分词 Embedding 数据预处理 模型 如何添加用户词典 前言 很早便规划的浅谈分词算法,总共分为了五个部分,想聊聊自己在各种场景中使用到的分词方法做 ...
- 【转】Robot Framework作者建议如何选择自动化测试框架
原文:http://www.infoq.com/cn/news/2012/06/robot-author-suggest-autotest 软件自动化测试,作为手工测试的替代,越来越受到关注.Pekk ...
- python(nmap模块、多线程模块)
http://xael.org/pages/python-nmap-en.html nmap模块 http://www.tutorialspoint.com/python/python_m ...
- QMouseEvent鼠标事件
Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理
- django错误笔记——TypeError: view must be a callable or a list/tuple in the case of include().解决办法
django增加用户认证模块时,总是提醒模块的url.py中 url(r'^login/$', 'django.contrib.auth.views.login', name='login'),出错: ...
- CF912E Prime Gift
传送门 看到\(n\)只有16,可以把这些质数分成两半,然后预处理出这些数相乘得出的小于\(10^{18}\)的所有数,排个序,然后二分最终答案,再用两个指针从前往后和从后往前扫,进行\(two-po ...
- mysql 原理 ~ sql执行
一 普通sql执行的具体过程1 连接器 管理连接,权限验证2 分析器 词法分析,语法分析 比如 数据表和数据列是否存在, 别名是否有歧义,是否符合标准sql语法等3 优化器检测 执行计划生 ...
- Java调整JVM内存大小——(八)
JVM内存大小是有限的,有的时候程序过大时候会导致超出最大内存,产生下面异常: Exception in thread "main" java.lang.OutOfMemoryEr ...
- [转] bss段、data段、text段
1.前言 一个程序本质上都是由 BSS 段.DATA段.TEXT段三个组成的. 本文主要分编译时和运行时分别对 对data段 bss段 text段 堆 栈作一简要说明 2. 程序编译时概念说明 程序与 ...