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.准备工作 ...
随机推荐
- MYSQL 导入Excel数据到数据库中
1,先把excel的数据整理整齐,如每列都要保持同样的格式:就一列一列的数据: 2,导出excel的数据为CSV格式,即把excel的数据另存为xxxx.csv;: 3,用EditPlus工具将xxx ...
- Windows批处理:请求远程协助
公司办公场地分别在两个不同的楼层,处理问题要来回跑,所以思考使用远程访问的方式解决问题.同事大多对电脑不熟悉,使用「通讯软件」和「电话」教同事开启远程桌面沟通成本挺高,另外公司IP地址.账号密码都没统 ...
- Unity中使用Attribute
Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...
- AC日记——忠诚 洛谷 P1816
题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是由于一些人的挑拨,财主还是对管家产生了 ...
- 新建structs2 web应用及structs.xml常用基础配置
建立一个structs2 web应用程序 1. 创建一个基本的web应用程序 2. 添加structs2的jar文件到Class Path 将structs2的最小jar包拷到WEB-INF/lib目 ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- ant windows环境配置
详见如下链接,小蚂蚁builder.xml--apache-ant的配置 http://blog.csdn.net/gaohuanjie/article/details/40142687
- CentOS安装Redis
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...
- iOS开发--JS调用原生OC篇
JS调用原生OC篇 方式一(反正我不用) 第一种方式是用JS发起一个假的URL请求,然后利用UIWebView的代理方法拦截这次请求,然后再做相应的处理. 我写了一个简单的HTML网页和一个btn点击 ...
- SpringMVC的小总结
---恢复内容开始--- 前言: springMVC是我接触的第一个框架,当时在学校学习的时候还是各种懂,最简单的springMVC框架的配置还是比较熟,后来工作之后,虽然主要用的确实是springM ...