Java进阶知识19 Struts2和Spring整合在一起
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整合在一起的更多相关文章
- Java进阶知识04 Struts2的基础配置详解
1.Struts2的原理/流程步骤 简单的理解: 1.客户端发送一个request请求,Tomcat服务器接收到的请求经过web.xml配置文件去处理,进入struts2的核心过滤器,从而进入s ...
- Java进阶知识02 Struts2下的拦截器(interceptor)和 过滤器(Filter)
一.拦截器 1.1.首先创建一个拦截器类 package com.bw.bms.interceptor; import com.opensymphony.xwork2.ActionContext; i ...
- Java进阶知识01 Struts2下的 jquery+ajax+struts 技术实现异步刷新功能
1.效果图示 横线上方的部分不动(没有刷新),下方实现刷新(异步刷新) 2.实现步骤 jquery+ajax+struts技术实现异步刷新功能的步骤: 1.需要用到 jquery+ajax+st ...
- Java进阶知识14 Struts2中的S标签
1.A 开头 <s:a href=""></s:a> //超链接,类似于html里的<a></a> <s:action nam ...
- Struts2学习笔记——Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- 二十六:Struts2 和 spring整合
二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...
- 第一次做的struts2与spring整合
参考:http://www.cnblogs.com/S-E-P/archive/2012/01/18/2325253.html 这篇文章说的关键就是“除了导入Struts2和Spring的核心库之外, ...
- Struts2+Hibernate+Spring 整合示例[转]
原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...
随机推荐
- gin PostForm 方法不起作用
情景: 在httpie post 下,在 axios post下,总的来说,就是在form-data下只有c.Bind()会有用 如果一定要用c.PostForm() headers必须为x-www- ...
- //统计报表-供水量统计主列表分页查询 Element-ui的分页插件
<!-- //分页 --> <div class="pagination">时间(月) <el-pagination @current-change= ...
- Apache2.4+Tomcat7.0整合配置详解
一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Lin ...
- (三)easyUI之树形组件
一.同步树 1.1 概念 所有节点一次性加载完成 1.2 案例 1.2.1 数据库设计 1.2.2 编码 index.jsp <%@ page language="java" ...
- 解决Cannot change version of project facet Dynamic web module to 2.5(转)
我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...
- CAN总线上的消息单帧某个信号的值计算(C#)
public static ulong GetMotorolaSignalValue(byte[] data, int startBit, int bitLength) { ; , j =; i ...
- 表空间 oracle
--create table create table table_name ( column1 varchar2(20) not null, column2 number(8) not null, ...
- luogu题解P1967货车运输--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P1967 分析 NOIp的一道裸题,直接在最大生成树上剖分取最小值一下就完事了,非常好写,常数也比较小,然而题解 ...
- 4、java基础:JDK8接口新特性以及接口和抽象类区别
在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的,所以在JDK8之前, ...
- ConfigParser读取配置文件时报错:ConfigParser.MissingSectionHeaderError
使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...