1、概述

1、Spring负责对象创建
  2、Struts2负责用Action处理请求
  3、整合的关键点:让Struts2框架Action对象的创建交给Spring完成。

2、整合实例

需要用到的 jar包

       Spring的配置文件(aaa-bbb.xml)建议分层,方便维护。

配置web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- Spring 监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beans-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 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>
</web-app>

DAO层

 //接口
public interface IUserDao {
public String getByName();
} //实现
public class UserDaoImpl implements IUserDao { public String getByName() {
return "DSHORE";
}
}

Service层

 //接口
public interface IUserService {
public String getByName();
} //实现
public class UserServiceImpl implements IUserService { //注入值,如果没有使用spring框架,则此处需要new对象,否则拿不到值。
private IUserDao userDao;// = new UserDaoImpl(); public String getByName() {
return userDao.getByName();
} public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
}

Action层

 package com.shore.action;

 import com.opensymphony.xwork2.ActionSupport;
import com.shore.service.IUserService; /**
* @author DSHORE/2019-10-26
*
*/
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -8197510116737054459L; //注入值,如果没有使用spring框架,则此处需要new对象,否则拿不到值。
private IUserService userService;// = new UserServiceImpl(); public String login() {
System.out.println("用户名:" + userService.getByName());
return SUCCESS;
} public IUserService getUserService() {
return userService;
} public void setUserService(IUserService userService) {
this.userService = userService;
}
}

struts.xml 配置文件      注意:struts.xml 中 action name 和 bean-action.xml 中的 bean id 名称,一定要保持一致!!!

 <?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>
<!-- true支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <!-- true --> <package name="front" namespace="/front" extends="struts-default">
<action name="userAction" class="com.shore.action.UserAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

beans-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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userDao" class="com.shore.dao.impl.UserDaoImpl"></bean>
</beans>

beans-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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.shore.service.impl.UserServiceImpl" p:userDao-ref="userDao"></bean>
</beans>

beans-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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- scope="prototype":多例。默认是单例(不用写即是默认) -->
<bean id="userAction" class="com.shore.action.UserAction" scope="prototype" p:userService-ref="userService"></bean>
</beans>

index.jsp 页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<s:form action="userAction!login" method="post" namespace="/front">
<s:textfield name="name" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</body>
</html>

success.jsp 页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
登录成功!
</body>
</html>

测试结果图:

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11745460.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

Java进阶知识19 Struts2和Spring整合在一起的更多相关文章

  1. Java进阶知识04 Struts2的基础配置详解

    1.Struts2的原理/流程步骤 简单的理解:    1.客户端发送一个request请求,Tomcat服务器接收到的请求经过web.xml配置文件去处理,进入struts2的核心过滤器,从而进入s ...

  2. Java进阶知识02 Struts2下的拦截器(interceptor)和 过滤器(Filter)

    一.拦截器 1.1.首先创建一个拦截器类 package com.bw.bms.interceptor; import com.opensymphony.xwork2.ActionContext; i ...

  3. Java进阶知识01 Struts2下的 jquery+ajax+struts 技术实现异步刷新功能

    1.效果图示 横线上方的部分不动(没有刷新),下方实现刷新(异步刷新) 2.实现步骤 jquery+ajax+struts技术实现异步刷新功能的步骤:    1.需要用到 jquery+ajax+st ...

  4. Java进阶知识14 Struts2中的S标签

    1.A 开头 <s:a href=""></s:a> //超链接,类似于html里的<a></a> <s:action nam ...

  5. Struts2学习笔记——Struts2与Spring整合

      Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...

  6. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  7. 二十六:Struts2 和 spring整合

    二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...

  8. 第一次做的struts2与spring整合

    参考:http://www.cnblogs.com/S-E-P/archive/2012/01/18/2325253.html 这篇文章说的关键就是“除了导入Struts2和Spring的核心库之外, ...

  9. Struts2+Hibernate+Spring 整合示例[转]

    原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...

随机推荐

  1. golang goroutine进行通信 channel

    1.channel的读取与声明 //goroutine之间利用channel进行通信 package main import ( "fmt" "time" ) ...

  2. Optional 理解

    目录 Optional 理解 1. 含义 2. Optional 类中方法 3. Optional 对象不应该作为方法参数 Optional 理解 1. 含义 Optional 是一个容器对象,该容器 ...

  3. 给初学PHP的学习线路和建议

    直入主题  1.熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作简单的网页,对元素属性相对熟悉. 2.理解动态语言的概念和运做机制,熟悉基本的PHP语法. 3.学习如何将PHP与HTML结 ...

  4. .htaccess 转 SAE AppConfig

    新浪的SAE不支持 htaccess,但是他们开发了 AppConfig,可以完全代替 htaccess 的常见功能,AppConfig采用类自然语言的规则描述,还是很人性化的. 这里来写一个短网址的 ...

  5. 使用ef core自动生成mysql表和数据编码的问题

    mysql默认的编码是不支持中文的,需要改成utf8编码格式. 而我使用的Pomelo.EntityFrameworkCore.MySql组件生成mysql库和表,他是使用默认编码的. 网上大多说修改 ...

  6. DataGridView绑定数据、删除数据

    定义学生类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  7. 【web】Chrome 浏览器中查看 webSocket 连接信息

    1.以下代码实现一个webSocket连接,在文本输入框中输入内容,点击发送,通过服务器,返回相同的内容显示在下方. 1 <!DOCTYPE html> 2 <html lang=& ...

  8. pickle 和 base64 模块的使用

    pickle pickle模块是python的标准模块,提供了对于python数据的序列化操作,可以将数据转换为bytes类型,其序列化速度比json模块要高. pickle.dumps() 将pyt ...

  9. iOS有哪些数据类型/基本数据类型?

    简述 本文主要探究使用OC作为iOS开发语言时,我们能使用哪些数据类型. 一切类型始于C. C语言的类型 基本数据类型: 基本数据类型(fundamental data types)也叫原始数据类型( ...

  10. pytorch训练模型的一些坑

    1. 图像读取 opencv的python和c++读取的图像结果不一致,是因为python和c++采用的opencv版本不一样,从而使用的解码库不同,导致读取的结果不同. 详细内容参考:https:/ ...