Spring整合Struts2

整合什么?——用IoC容器管理Struts2的Action
如何整合?
第一步:配置Struts2
1.加入Struts2的jar包。
2.配置web.xml文件。
3.加入Struts2的配置文件struts.xml

第二步:配置Spring
1.加入Spring的jar包
Spring的标准 jar包。
struts-spring-plugin-plugin-2.3.31.jar包。

2.添加Spring的配置文件——beans.xml

3.在Spring的IoC容器中配置Struts2的Action——注意配置中需要把Action配置成非单例模式scope=prototype.

<beans id="infoAction" class="com.itnba.maya.controller.InfoAction" scope="prototype">
<property name="infoService" ref="com.itnba.maya.services.InfoService"></property>
</beans>

4.配置struts.xml。把<action>的class属性指向IoC容器相应bean的id。

        <action name="*_*" class="infoAction" method="{2}">
<result>
{1}/{2}.jsp
</result>
</action>

5.在Web.xml中添加配置

<!--配置Spring配置文件的名称和位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!--启动IoC容器的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

web.xml配置的作用:
1)创建IoC容器的对象。
2)把对象放到Application中。

 实例:

导入前面说的各种包

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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"> <display-name>Struts Blank</display-name> <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> <!--配置Spring配置文件的名称和位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <!--启动IoC容器的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="{1}Action" method="{2}">
<result>
{1}/{2}.jsp
</result>
</action>
</package> <include file="example.xml"/> <!-- Add packages here --> </struts>

Action类

package com.itnba.maya.controller;

import com.opensymphony.xwork2.ActionSupport;

public class InfoAction extends ActionSupport {

    public String show(){
return SUCCESS;
} }

beans.xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean class="com.itnba.maya.controller.InfoAction" id="infoAction"></bean> </beans>

jsp页面

运行:

最新文章

  1. 【引】objective-c,5:Associated Objects 的原理
  2. Mac下如何配置环境变量
  3. Java数据结构——有序链表
  4. js判断手机端Android手机还是iPhone手机
  5. 数据库中is null(is not null)与=null(!=null)的区别
  6. django-url调度器-初级篇
  7. How Do I Declare A Block in Objective-C?
  8. 注册表删除chrome插件
  9. ios学习:页面跳转(present)
  10. UVA 11624 Fire!(二次BFS)
  11. 认识ptrace函数
  12. Ajax调用返回json,xml数据类型(0517--pm)
  13. RDS最佳实践(一)—如何选择你的RDS
  14. .net core identity集成微信授权登录
  15. 基于ionic4、cordova搭建android开发环境
  16. .net下的缓存技术
  17. java生成TXT
  18. ie8 报错:意外地调用了方法或属性访问
  19. python中pop()函数的用法
  20. 用Java操纵HBase数据库(新建表,插入,删除,查找)

热门文章

  1. PyNest&mdash;&mdash;part 4: topologically structured networks
  2. win7开启特定端口
  3. C# 创建单例你会几种方式?
  4. list comprehension &amp; generator expression
  5. Differences between Python2 and Python3
  6. 第六课 GDB调试 (上)
  7. 微信小程序组件button
  8. org.springframework-jdbc
  9. msys2安装开发工具
  10. HDU - 6397 Character Encoding 2018 Multi-University Training Contest 8 (容斥原理)