一、如何单独使用Struts2

(1)引入struts2的jar包

commons-fileupload-1.2.1.jar

freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.jar

xwork-core-2.1.6.jar

(2)在项目的src下引入struts.xml文件

<?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="hello1" extends="struts-default">
<action name="hello" class="org.tarena.action.Hello">
<result>
success.jsp
</result>
</action>
</package> </struts>

(3)在项目的web.xml文件下进行struts2的配置

<!-- struts2核心filter -->
<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>

(4)在项目中编写Action

package org.tarena.action;

public class Hello {

    //output
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String execute(){
name = "陈驰";
return "success";
}
}

(5)写两个jsp,hello.jsp跳转到success.jsp

hello.jsp:

<body>
<a href="/spring004/hello.action">Hello</a>
</body>

success.jsp:

 <body>
<p>
${name },你好!!!
</p>
</body>

最终的效果,点击hello,跳转到success.jsp,显示“陈驰,你好!!!”

二、如何用Spring整合Struts2

(1)在刚才的基础上,还需引入新的jar包

struts2-spring-plugin-2.1.8.jar

spring-web-3.2.8.RELEASE.jar

commons-logging.jar

spring-beans-3.2.8.RELEASE.jar

spring-context-3.2.8.RELEASE.jar

spring-core-3.2.8.RELEASE.jar

spring-expression-3.2.8.RELEASE.jar

(2)在刚才的基础上,引入Spring的配置文件applicationContext.xml,放在src下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="helloBean" class="org.tarena.action.Hello" scope="prototype">
</bean> </beans>

(3)struts.xml的配置文件中的<action>需要改一下,这里需要注意一个问题,将class属性和Spring容器中的
<bean>元素的id属性保持一致。即原来是利用struts自己去创建一个action,现在是通过整合包
利用class值当作id标识去Spring容器获取Bean对象。

<?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="hello1" extends="struts-default">
<!-- 利用struts-spring-plugin.jar去spring容器寻找bean对象
寻找的规则:利用class属性当作id值去spring容器中获取 -->
<action name="hello" class="helloBean">
<result>
success.jsp
</result>
</action>
</package> </struts>

(4)最后还要在web.xml中配置一下,添加ContextLoaderListener,在服务器启动时,实例化Spring容器对象。

为什么要实例化Spring容器对象呢?因为原来是创建action,现在是到Spring容器中寻找了,不能找的时候还没实例化,那就找不到了。

 <!-- 指定Spring配置文件位置和名称 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 在tomcat启动时,实例化Spring容器对象,给struts-spring-plugin.jar使用
就不用写ApplicationContext ac = new ... -->
<!-- listener在tomcat启动时加载 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <!-- struts2核心filter -->
<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>

实现的效果,和直接用Struts2一样,说白了,整合之后,就是不让struts2自己创建action了,要在spring容器中找。

Struts2的使用以及Spring整合Struts2的更多相关文章

  1. struts2,hibernate,spring整合笔记(3)

    struts2,hibernate,spring整合笔记(1) struts2,hibernate,spring整合笔记(2) 配好struts和hibernate就要开始spring了 老规矩,还是 ...

  2. struts2,hibernate,spring整合笔记(2)

    上一话struts2,hibernate,spring整合笔记(1) 接下来继续 配置完struts之后就要开始hibernate的配置 hibernate的环境并不依赖web开发环境,在我第一次配置 ...

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

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

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

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

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

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

  6. Spring整合Struts2的方法

    一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...

  7. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

  8. spring整合struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...

  9. Spring 整合 Struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...

随机推荐

  1. Redis安装,mongodb安装,hbase安装,cassandra安装,mysql安装,zookeeper安装,kafka安装,storm安装大数据软件安装部署百科全书

    伟大的程序员版权所有,转载请注明:http://www.lenggirl.com/bigdata/server-sofeware-install.html 一.安装mongodb 官网下载包mongo ...

  2. UESTC 1014 Shot

    这题刚开始没想通,结果发现要解方程,代码如下: #include<iostream> #include<cstdio> #include<cmath> #inclu ...

  3. sql 入门经典(第五版) Ryan Stephens 学习笔记 (第六,七,八,九,十章,十一章,十二章)

    第六章: 管理数据库事务 事务 是 由第五章 数据操作语言完成的  DML ,是对数据库锁做的一个操作或者修改. 所有事务都有开始和结束 事务可以被保存和撤销 如果事务在中途失败,事务中的任何部分都不 ...

  4. SerializeField和Serializable

    Serialize功能 Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系. 简单的说,在没有自定义Inspector的情况下所 ...

  5. uGUI练习(五) Draggable Object

    练习目标 学习制作一个可拖动的UI 一.步骤 监听UI的Drag事件,需要我们写一点点的代码. 1.创建一个Panel ,设置size为(100,100) 2.创建DraggableObjectSce ...

  6. EventBus (四) Sticky事件

    什么是Sticky事件? 关于Sticky事件有的同学可能不是很熟悉,Sticky的意思是粘性的.在Android开 发中,Sticky事件只指事件消费者在事件发布之后才注册的也能接收到该事件的特殊类 ...

  7. Ember模板中的操作指向

    模板中的链接操作指向有三个地方,该模板对应的控制器和路由以及视图,默认是先跳转到控制器,如果控制器里没有定义模板中动作的方法,就去该模板对应的路由里找,如果还没找到,就去父级路由找,直到顶级路由,如果 ...

  8. 好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)

    转载:http://www.cnblogs.com/lyhabc/p/3322437.html 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 这个工具主要就是 ...

  9. 《认识你自己(Archetypes who are you?)》 10种原型的行为模式和性格特征

    转自:http://www.cnblogs.com/richardcuick/p/5627298.html 拥护型原型   你自然而然地就会被社会.政治和环境问题所吸引. 你认为世界需要改变. 你承诺 ...

  10. Caffe学习系列(4):激活层(Activiation Layers)及参数

    在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...