3.1、Spring和Struts2的结合使用
一、配置Struts2:
1、新建一个web项目,在src目录下新建com.st.bean/dao/service/action包,并在该包下面添加相应的接口及接口的实现类:
a)、在bean下新建一个UserBean,包含userName、password、sex属性名,并添加set、get方法及toString方法。
b)、dao层新建UserDao接口,并添加该接口是实现类UserDaoIm:
public class UserDaoIm implements UserDao {
public UserBean queryUser(UserBean user) {
System.out.println("************"+user);
return user;
}
}
c)、在service层新建UserService接口,并添加该接口的实现类UserServiceIm:
package com.st.service; import com.st.bean.UserBean;
import com.st.dao.UserDao; public class UserServiceIm implements UserService { UserDao dao;
public UserBean queryUser(UserBean user) {
System.out.println("------------------------");
return dao.queryUser(user);
}
public void setDao(UserDao dao) {
this.dao = dao;
}
}
2、在lib下面引入Struts2所需要的jar包,在web.xml文件中添加Struts2的拦截器:
<!-- ********************************** -->
<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>
<!-- ********************************** -->
3、复制一个struts.xml文件到src目录下,并编辑<package>标签中的类容:
<struts>
<package name="user" namespace="/user" extends="struts-default" >
<action name="userAction" class="com.st.action.UserAction" method="login">
<result name="success">/main.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
4、新建一个lojin.jsp文件
<body>
<form method="post" action="<%=request.getContextPath() %>/user/userAction" >
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" value=""/></td>
</tr>
<tr>
<td>性 别:</td>
<td>男<input type="radio" name="sex" value="男"/> 女<input type="radio" name="sex" value="女"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td><input type="submit" value="登陆"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
5、新建一个main.jsp文件,<body>标签中的类容为:<body> 登陆成功!<br> </body>。
6、在action包中新建一个UserAction类(注意UserAction类要继承ActionSupport类,并实现ModelDriven接口):
public class UserAction extends ActionSupport implements ModelDriven<UserBean>{
private UserBean user;
UserService service ;
public String login(){
System.out.println("--------------"+user);
return SUCCESS;
}
public void setService(UserService service) {
this.service = service;
}
public UserBean getModel() {
this.user = new UserBean();
return user;
}
}
7、配置好tomcat服务器后,进入login.jsp页面后,点击登录按钮,页面能成功跳转到man.jsp页面则表示Struts2已配置成功。
二、配置Spring:
1、将Spring需要的基本jar包引入到lib下面(注意宁少勿多的原则),将applicationContext.xml文件引入到src目录下面,并清空xml文件中原有的类容。在web.xml文件中添加下面的类容用来配置监听和加载applicationContext.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> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- 注意:多个 .xml 文件中间用逗号分开 -->
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- ********************************** -->
<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>
2、编辑applicationContext.xml中的类容:
<bean id="userDao" class="com.st.dao.UserDaoIm">
</bean>
<bean id="userService" class="com.st.service.UserServiceIm">
<property name="dao" ref="userDao" />
</bean>
<bean id="userAction" class="com.st.action.UserAction">
<property name="service" ref="userService"/>
</bean>
3、现在因为有了Spring,所以UserAction这个类将由Spring的容器生成,所以在struts.xml文件中的UserAction这个类就直接可以使用了,故修改struts.xml文件中的内容为:
<package name="user" namespace="/user" extends="struts-default" >
<!-- 注意:现在的class引用的是applicationContext.xml中的userAction -->
<action name="userAction" class="userAction" method="login">
<result name="success">/main.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
4、修改UserAction中的类容,使dao-service-action层之间存在联系(即模拟了一个用户登录的过程):
public class UserAction extends ActionSupport implements ModelDriven<UserBean>{
private UserBean user;
private UserService service ;
public String login(){
service.queryUser(user);
return SUCCESS;
}
public void setService(UserService service) {
this.service = service;
}
public UserBean getModel() {
this.user = new UserBean();
return user;
}
}
5、现在启动Tomcat服务器,他会报这样一个错误:
Caused by: Action class [userAction] not found - action - file:/D:/JAVA/JAVA/tomcat/tomcat-7.0.65-windows-x64/apache-tomcat-7.0.65/webapps/Spring_Struts2/WEB-INF/classes/struts.xml:9:68
即struts.xml文件中的第9行中找不到userAction这个类,这是因为Spring个Struts之间还没有建立相应的联系,这时引入一个“struts2-spring-plugin-2.1.8.1.jar”包(在Struts2里面有),即链接Spring的和Struts的支持包。这时在开启服务器,如果还有缺少的jar包按照提示引入。
6、在浏览器中打开login.jsp页面,填写相应的登陆信息后按登录按钮,如果页面跳转成功,且在控制台打印了UserDaoIm中的提示信息,则表示Spring和Struts2的融合成功。
可以看下UserDaoIm中的类容为:

按登录按钮后控制台打印的信息为:

3.1、Spring和Struts2的结合使用的更多相关文章
- Struts2的使用以及Spring整合Struts2
一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- Spring与Struts2整合
Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...
- Spring整合Struts2,Hibernate的xml方式
作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...
- Spring框架+Struts2框架第一次整合
1:Spring框架和Struts2框架如何整合??? Spring 负责对象创建 Struts2 用Action处理请求 2:Spring与Struts2框架整合的关键点: 让struts2框架ac ...
- 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框架学习(5)spring整合struts2
内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...
- Maven环境下搭建SSH框架之Spring整合Struts2
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Struts2:2.5.10 Spring:4.3.8.RELEASE 注意:其他版本在某些特性的使用上可能稍微存在差别 2.准备工作 ...
随机推荐
- 前端开发必备!Emmet使用手册
介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具: 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片 ...
- spring bean的生命周期
掌握好spring bean的生命周期,对spring的扩展大有帮助. spring bean的生命周期(推荐看) spring bean的生命周期
- WORD中字数和字符
在WORD中,一个汉字算1个字符,也算是1个字,一个标点符号也算1个字符,也算是1个字,WORD中字符数的统计分为(不计空格)和(计空格)的两种. 如果一篇文章仅由汉字和标点符号组成,那么字数=字符数 ...
- plain framework 1 网络流 缓存数据详解
网络流是什么?为什么网络流中需要存在缓存数据?为什么PF中要采用缓存网络数据的机制?带着这几个疑问,让我们好好详细的了解一下在网络数据交互中我们容易忽视以及薄弱的一块.该部分为PF现有的网络流模型,但 ...
- OpenStack 企业私有云的若干需求(4):混合云支持 (Hybrid Cloud Support)
本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...
- centos系统编译安装nginx+php环境另加独立mysql教程
以前看过的安装nginx+php环境都带了mysql数据库了,这个是因为很多站长都是nginx+php+mysql都在同一台服务器了,那么今天我们是单独处理了,一个是nginx+php环境,然后mys ...
- 低版本GCC程序向高版本移植的兼容性问题
将低版本gcc编译过的程序移植到高版本GCC时, 可能会出现一些兼容性问题. 原因是, 为了适应新的标准,一些旧的语法规则被废弃了. 关于这方面的一些具体资料可从该处查询. 这里只是自己遇到的其中一个 ...
- ELK日志系统:Filebeat使用及Kibana如何设置登录认证
根据elastic上的说法: Filebeat is a lightweight, open source shipper for log file data. As the next-generat ...
- [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- CentOS配置git和maven自动部署java
#安装Git yum install git #测试是否成功 git -version #正确 #git version 1.7.1 #配置git config --global user.name ...