一、配置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>性&nbsp;别:</td>
<td>男<input type="radio" name="sex" value="男"/>&nbsp;&nbsp;女<input type="radio" name="sex" value="女"/></td>
</tr>
<tr>
<td>密&nbsp;码:</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的结合使用的更多相关文章

  1. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  2. Spring与Struts2整合VS Spring与Spring MVC整合

    Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...

  3. Spring与Struts2整合

    Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...

  4. Spring整合Struts2,Hibernate的xml方式

    作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...

  5. Spring框架+Struts2框架第一次整合

    1:Spring框架和Struts2框架如何整合??? Spring 负责对象创建 Struts2 用Action处理请求 2:Spring与Struts2框架整合的关键点: 让struts2框架ac ...

  6. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  7. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  8. Spring框架学习(5)spring整合struts2

    内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...

  9. Maven环境下搭建SSH框架之Spring整合Struts2

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Struts2:2.5.10 Spring:4.3.8.RELEASE 注意:其他版本在某些特性的使用上可能稍微存在差别 2.准备工作 ...

随机推荐

  1. 关于 AVI 的一些代码

    #ifndef __HSS_AUTO_REVISE_AVI_FRAMERATE_HSS__ #define __HSS_AUTO_REVISE_AVI_FRAMERATE_HSS__ /******* ...

  2. sql语句返回值的问题

    由于执行sql语句的时候执行成功或者失败会返回执行的影响函数,用list是因为查询的结果可能为null也可能set后放到集合里去: 所以返回值类型用int

  3. Several anatomical structure pics 一些大脑解剖结构图

    Source: Wikipedia

  4. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  5. [LeetCode] Patching Array 补丁数组

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  6. 较为完整的meta

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. CentOS安装Maven

    现有的一个项目使用了Maven来管理,源代码放到了Subversion中.虽然Maven管理项目很方便,但是部署起来还是很麻烦的.先要在本地生成项目jar包,上传到服务器,然后再重启服务.如果在服务器 ...

  8. iframe 跨域相互操作

    我们在开发后台管理系统时可能会经常要跟 iframe 打交道,因为现在大部分后台管理系统都是页面内嵌iframe,所以有时候两者之间就难免要互相通信,但浏览器为了安全的原因,所以就禁止了不同域的访问, ...

  9. BZOJ 1221: [HNOI2001] 软件开发

    1221: [HNOI2001] 软件开发 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1428  Solved: 791[Submit][Stat ...

  10. [WP8.1开发]RSA 使用BouncyCastle 公钥解密

    写应用的时候遇到个服务器返回私钥加密过的数据 ,然后要在客户端用公钥解密的需求 ,一直没找到方法,应用搁置了一个学期,多方搜索,结论就是.net没有实现公钥解密的方法,要自己实现,于是硬着头皮开始看B ...