struts2和spring3.2的整合 详细演示
1、首先我们新建一个Web工程,如下:

2、导入Spring和Struts2的jar包。


其中,struts2-spring-plugin-2.1.8.jar是struts2、spring整合的关键。
3、首先新建一个业务代码LoginAction,演示登录处理。
package action; import server.MyServer;
import server.MyServerImpl; import com.opensymphony.xwork2.Action; public class LoginAction implements Action { private String username;
private String password;
private String tip;
private MyServer ms; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getTip() {
return tip;
} public void setTip(String tip) {
this.tip = tip;
} public void setMs(MyServer ms) {
this.ms = ms;
} public String execute() throws Exception {
//setMs(new MyServerImpl());
if (ms.valid(getUsername(), getPassword())) {
setTip("登录成功");
return "success";
} else {
return "error";
}
} }
4、然后新建一个接口MyServer,如下:
package server;
public interface MyServer {
public boolean valid(String username,String password);
}
5、然后新建一个实现类,如下:(这里为了演示方便,没有分包)
package server;
public class MyServerImpl implements MyServer {
public boolean valid(String username, String password) {
if(username.equals("cat")&&password.equals("123")){
return true;
}
return false;
}
}
6、在web.xml文件中对struts2和spring进行配置,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>
7、在WebRoot中的WEB-INF下新建一个applicationContext.xml文件,配置spring,如下:
(注意,这个文件不能直接在src下配置,必须在这里配置,不然web容器找不到)
<?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-3.2.xsd"> <bean id="myServer" class="server.MyServerImpl"></bean>
<bean id="loginAction" class="action.LoginAction" scope="prototype">
<property name="ms" ref="myServer"></property>
</bean>
</beans>
8、然后在src下新建一个struts.xml,配置struts2,如下:
(注意文件中action的class属性,不是一个类,而是spring配置中bean的id,属性由spring来注入)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts>
<package name="login" namespace="/login" extends="struts-default"> <action name="loginPro" class="loginAction">
<result name="success">
/WEB-INF/content/welcome.jsp
</result>
<result name="error">
/WEB-INF/content/error.jsp
</result>
</action>
<!-- 让用户直接访问该应用时列出所有视图页面 -->
<action name="*">
<result name="success">/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
9、至此,基本配置完毕,再加上三个视图文件login.jsp、welcome.jsp、error.jsp,如下:
login.jsp
<%@ 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>登录</title>
</head>
<body>
<form action="loginPro" method="post">
<table>
<caption>用户登录</caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td><input value="提交" type="submit"/></td>
<td><input value="重置" type="reset"/></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>welcome</title>
</head>
<body>
这是欢迎页面。
<s:property value="tip" />
</body>
</html>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
出现错误啦!
</body>
</html>
10、整个工程结构如下:

11、测试如下:
启动服务器,地址栏输入:http://127.0.0.1:8080/Struts2AndSpring/login/login
页面如下:

输入用户名和密码:

提交后如下:

参数传递正确。
输入错误用户名和密码:

显示如下:

至此,演示成功。
struts2和spring3.2的整合 详细演示的更多相关文章
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...
- struts2 + spring + mybatis 框架整合详细介绍
struts2 + spring + mybatis 框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...
- struts2.3+spring3.2+hibernate4.2例子
有些教程比较老,可是版本更新不等人,基于马士兵老师小例子,自己重新引用了新的包,调试确实有点烦人,但是通过英文文档和google解决问题.官网的更新超快,struts2.3+spring3.2+hib ...
- struts2+spring的两种整合方式
也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...
- Struts2的使用以及Spring整合Struts2
一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...
- struts2,hibernate,spring整合笔记(3)
struts2,hibernate,spring整合笔记(1) struts2,hibernate,spring整合笔记(2) 配好struts和hibernate就要开始spring了 老规矩,还是 ...
- struts2,hibernate,spring整合笔记(2)
上一话struts2,hibernate,spring整合笔记(1) 接下来继续 配置完struts之后就要开始hibernate的配置 hibernate的环境并不依赖web开发环境,在我第一次配置 ...
- Struts2.0+Spring3+Hibernate3(SSH~Demo)
Struts2.0+Spring3+Hibernate3(SSH~Demo) 前言:整理一些集成框架,发现网上都是一些半成品,都是共享一部分出来(确实让人很纠结),这是整理了一份SSH的测试案例,完全 ...
随机推荐
- samba服务配置
使用yum或者apt-get安装 # yum install samba samba-client samba-swat Samba开发环境配置 Acl权限设置 [不是必须.只要保证web目录的所有 ...
- 自定义标签 与 JSTL(JSP Standard Tag Library)
1.自定义标签 [理解] [1]简介 > 在JSP2.0以后,在jsp页面中不建议使用脚本片段<% %>和JSP表达式<%= %> ...
- Asp.Net Web Form 前后台传值
1,后台往前台传值----单个变量直接传递到页面元素 前台代码 <b><%=strCompanyName%>费用明细</b> 后台代码 public partial ...
- SQL Server 使用游标更新数据库中的数据(使用存储过程)
ALTER PROCEDURE [dbo].[POR_CURSOR_FOR_UPDATE] --创建存储过程 AS BEGIN SET nocount ON --忽略行数显示 DECLARE Upda ...
- 安装 gcc-c++ 时报错和原有 gcc 版本冲突
Centos 6.7 安装 gcc-c++时报下面的错误: Resolving Dependencies --> Running transaction check ---> :-.el6 ...
- linux enc28j60网卡驱动移植(硬件spi和模拟spi)
本来想移植DM9000网卡的驱动,无奈硬件出了点问题,通过杜邦线链接开发板和DM9000网卡模块,系统上电,还没加载网卡驱动就直接崩溃了,找不到原因...刚好手上有一个enc28j60的网卡模块,于是 ...
- linux下文件的特殊权限s和t
先看看这两个文件的权限:[root@localhost ~]# ls -ld /usr/bin/passwd /tmpdrwxrwxrwt 4 root root 4096 Jun 2 17:33 / ...
- 中科院分词ICTCLAS5.0_JNI 使用方法
1.简介 中国科学院计算技术研究所在多年研究基础上,耗时一年研制出了基于多层隐码模型的汉语词法分析系统 ICTCLAS(Institute of Computing Technology, Chine ...
- MIT 6.824 : Spring 2015 lab2 训练笔记
源代码参见我的github:https://github.com/YaoZengzeng/MIT-6.824 Lab 2:Primary/Backup Key/Value Service Overvi ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...