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. MongoDB添加删除节点

    副本集添加删除节点 sharding添加删除节点 先将节点设置为hidden,再remove

  2. CentOS 系统 MySQL 5.7 开启远程连接

    CentOS 系统安装好 MySQL 后,默认情况下不支持用户通过非本机连接上数据库服务器,下面是解决方法: 1.在控制台执行 mysql -u root -p 系统提示输入数据库 root 用户的密 ...

  3. centos mysql数据库问题:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'(转)

    问题描述: 安装好数据库MySQL,进入mysql,设置号密码后,退出的时候,利用密码无法进入,直接回车后可进入,无法看到数据库mysql,use mysql返回错误:ERROR 1044 (4200 ...

  4. 软件打包 Inno

    官网 http://www.jrsoftware.org/ 新建 点击工具栏第一项"新建".输入产品的名称.版本号.公司网址等信息 添加应用程序文件 应用程序图标 应用程序文档 许 ...

  5. 从零开始配置一个简单的webpack打包

    一.基础配置 1.创建一个名为demo-webpack的文件夹(名称随意) 2.初始化一个package.json文件 //在cmd窗口中使用以下命令快速创建 npm init -y 3.安装webp ...

  6. redis slot 槽点

    Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求 ...

  7. MySQL无法启动问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored

    今天重启一台内网服务器,发现mysql无法正常重启,执行systemctl start mysql,报错如下 Starting LSB: start and stop MySQL... Dec 11 ...

  8. XSS学习收集

    XSS platform supporting HTTPShttps://www.w0ai1uo.org/xss/xss.php?do=loginhttps://x.secbox.cn/index.p ...

  9. libssh

    1.SSH概念 ssh(secure shell),安全外壳协议,由IETF的网络小组所制定.ssh为建立在应用层基础上的安全协议.SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议. ...

  10. Ubuntu系统---安NVIDIA 驱动后 CUDA+cuDNN 安装

    Ubuntu系统---安NVIDIA 驱动后  CUDA+cuDNN 安装 --------------------------------------------@20190726--------- ...