内容源自:spring整合struts2

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

延伸:spring对持久层框架/技术的整合原理 (封装) : 
提供模板类封装对应技术/框架的开发流程 
通过对模板类的使用,实现对传统开发流程的”代替”。

二、整合方式: 
插件方式 
struts2为了实现对spring框架整合,也提供了一个插件的配置文件struts-plugin.xml 
struts2-spring-plugin.jar

三、整合过程 
a 创建web工程,添加struts2框架支持(导入struts2自带必需包并在web.xml中配置 Filter)

b web工程添加spring框架支持(导入spring核心层jar包,以及struts2文件下下的包,在web.xml中配置Listener监听器和容器配置文件参数)

四、具体实例 

页面:

<form action="login.action" method="post">
        登录名:<input type="text"  name="loginname"/><br/>
        密码:<Input type="password" name="password"/><br/>
        <Input type="submit" value="登录"/>
    </form>

web.xml

<?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">

  <filter>
    <filter-name>etoak</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>etoak</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    <!--
        每在工程添加一个用于处理请求的Action
        就需要将其配置成ioc容器中的bean

        1 action何时实例化?  容器启动时
            单独使用struts2时,action何时实例化?
                客户端每次提交请求时
        2 action实例化状态?  单例
            单独使用struts2时,非单例

        注意 : 为了保证action状态的一致性;在将Action
        配置成ioc容器中的bean对象时,需要将其状态设置为~非单例状态
     -->
    <bean id="action" class="com.etoak.action.LoginAction"
        scope="prototype"></bean>
</beans>

struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!--
        请求解析器 ActionProxy "login"
        请求调度者 ActionInvocation
     -->
    <package name="etoak" extends="struts-default">
        <!--
            请求和处理之间的映射关系如何形成
            <action name="请求" class="Action">
            class属性:指向的是ioc容器中某个bean的ID值
         -->
        <action name="login" class="action">
            <result name="success" type="redirect">/success.jsp</result>
        </action>
    </package>
</struts>

LoginAction.java

public class LoginAction extends ActionSupport {
    private String loginname;
    private String password;
    public String getLoginname() {
        return loginname;
    }
    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String execute() throws Exception {
        // 处理login.action请求 - DAO
        System.out.println("loginname--"+loginname);
        System.out.println("password--"+password);
        // 调用DAO    ioc依赖注入 (setter注入)
        return this.SUCCESS;
    }
}

Spring框架学习(5)spring整合struts2的更多相关文章

  1. Spring框架学习03——Spring Bean 的详解

    1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...

  2. Spring框架学习02——Spring IOC 详解

    1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...

  3. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  4. Spring框架学习之IOC(一)

    Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...

  5. Spring框架学习总结(上)

    目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...

  6. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

  7. Spring框架学习笔记(5)——Spring Boot创建与使用

    Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...

  8. Spring框架学习笔记(1)

    Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...

  9. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

  10. Spring框架学习1

    AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理   Spring框架学习(一)   阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...

随机推荐

  1. centos 7 开机启动配置

    centos 7 开机启动 1 开机启动配置文件位于/usr/lib/systemd/system/ 2 nginx的配置[Unit]Description=nginx - high performa ...

  2. 在Pygtk和Glade使用Gtkbuilder

    最近开始学习python的GUI,选择了Pygtk,试着用Glade设计界面,项目文件采用Gtkbuilder格式,网上的教程大部分是使用Libglade,所以用xml方式读取.glade文件: wT ...

  3. 转:攻击JavaWeb应用[3]-SQL注入

    转:http://static.hx99.net/static/drops/tips-236.html 攻击JavaWeb应用[3]-SQL注入 园长 · 2013/07/16 18:28 注:本节重 ...

  4. HDU 1011 Starship Troopers【树形DP/有依赖的01背包】

    You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...

  5. git "Could not read from remote repository.Please make sure you have the correct access rights."解决方案

    我们在使用git clone 或其他命令的时候,有时候会遇到这类问题,如图: fatal: Could not read from remote repository.Please make sure ...

  6. 在CentOS6或RHEL6恢复上ext4文件系统误删除的文件

    首先说明: [root@CentOS6 ~]# rm -rf / //这条命令不可以执行 [root@CentOS6 ~]# rm -rf /* //这条命令可以执行,别去试 ext4文件系统上误删除 ...

  7. 【Go】windows下搭建go语言编译环境

    主要是协助杨哥做Kubernetes相关工作,由于Kubernetes和Docker都是由Go语言编写,因此改源码后还是需要go语言编译器来编译运行.所以打算先在windows上安装一下go语言环境. ...

  8. leetcode136 Single Number

    题意:数组中每个数字都出现了两次,只有一个出现一次,找出这个数 思路:很明显不能从头到位遍历来找,首先是超时的原因,再次就是这样很没意思·····但是却没想到什么好办法,因为不了解按位异或(XOR). ...

  9. JZYZOJ1445 [noip2014day1-T3]飞扬的小鸟 动态规划 完全背包

    http://172.20.6.3/Problem_Show.asp?id=1445 很容易看出来动态规划的本质,但是之前写的时候被卡了一下(不止一下),还是写一下题解. 直接暴力O(n*m^2)大概 ...

  10. SQL 中 HAVING 用法

    现在 Student表有 如下数据 现需求如下: 查找每个老师的学生的平均年龄且平均年齿大于12 不使用 HAVING SELECT * FROM (SELECT TeacherID, AVG(Age ...