Spring Struts2 整合
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页面
运行:
Spring Struts2 整合的更多相关文章
- spring struts2整合
把struts2的action交给spring管理 一.导入相应jar包 导入与spring有关的基本jar包,和与struts2有关的基本jar包 还需要导入 struts2-spring整合jar ...
- struts2 spring mybatis 整合(test)
这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...
- ssh 框架整合试例 (spring+struts2+hibernate)
1.首先用Eclipse创建一个web项目(Eclipse EE 版) new->Other-> 输入web 然后选择Dynamic Web Project->next-> 输 ...
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...
- struts2整合spring的思路
struts2整合spring有有两种策略: >sping容器负责管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件. >利用spring的自动装配,Action将自动会从Sp ...
- struts2整合spring出现的Unable to instantiate Action异常
在struts2整合spring的时候,完全一步步按照官方文档上去做的,最后发现出现 Unable to instantiate Action,网上一搜发现很多人和我一样的问题,配置什么都没有错误,就 ...
- struts2整合spring应用实例
我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明stru ...
- SSH(Spring Struts2 Hibernate)框架整合(注解版)
案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 框架:Spring Struts2 Hibernate 案例架构: 1.依赖jar包 pom.xml < ...
随机推荐
- DRF(4) - 认证、权限组件
一.引入 通过前面三节课的学习,我们已经详细了解了DRF提供的几个重要的工具,DRF充分利用了面向对象编程的思想,对Django的View类进行了继承,并封装了其as_view方法和dispatch方 ...
- MySQL具体解释(20)-----------数据库备份和还原
数据备份: 使用mysqldump命令备份 mysqldump命令能够讲数据库中的数据备份成一个文本文件. 表结果和表中的数据将存储在生成的文本中.mysqldump的工作原理非常easy. 他先查出 ...
- 001-前端系列-react系列
一.概述 原文地址:http://www.ruanyifeng.com/blog/2016/09/react-technology-stack.html 二.摘要 ES6 语法:教程 [可以了解] B ...
- springcloud 开发时快速剔除无用服务
注册中心配置: # 关闭保护机制 eureka.server.enable-self-preservation=false #剔除失效服务间隔 eureka.server.eviction-inter ...
- 关于python中的查询数据库内容中用到的fetchone()函数和fetchall()函数(转)还有fetchmany()
最近在用python操作mysql数据库时,碰到了下面这两个函数,标记一下: fetchone() : 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None fetchall() ...
- PAT 天梯赛 L1-021. 重要的话说三遍 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-021 AC代码 #include <iostream> #include <cstdio&g ...
- CodeForces - 987E Petr and Permutations (思维+逆序对)
题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...
- linux中相关服务不能访问的排错技巧
Linux相关服务不能访问的排错步骤,以HTTP服务为例: 一.服务端排查思路: 1.检查SELinux是否关闭(针对CentOS6系统) (1)临时关闭 setenforce 0 (2 ...
- Vue-router学习(一)- 路由匹配
一.Vue-router引入使用 Vue-router就是一个vue路由类,通过new一个Vue路由实例,然后Vue.use()嵌入即可. 一般分为以下步骤: 1.引入 (1).方法一:npm包嵌入, ...
- HDU4628
/*状态转移f[i]=min(f[i],f[j]+f[i^j]); 就是j状态+i^j状态=i状态,f[i]记录的是从i删除1要的最小步数*/ #include<string.h> #in ...