整合目标:使用spring的bean管理struts action service。

整合步骤:

一、加入spring

1、加入spring jar包

2、配置web.xml文件

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

3、加入spring配置文件

二、加入struts2

1、加入struts2 jar包

2、在web.xml配置

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

2、加入struts2 对spring支持的插件包struts2-spring-plugin-2.3.31.jar

3、加入struts2配置文件

4、创建一个简单service和action

package com.hy.service;

public class PersonService {
public void save() {
System.out.println("personService save...");
}
}
package com.hy.action;

import com.hy.service.PersonService;

public class PersonAction {

    private PersonService personService;

    public PersonService getPersonService() {
return personService;
} public void setPersonService(PersonService personService) {
this.personService = personService;
} public String execute() {
System.out.println("execute...");
personService.save();
return "success";
}
}

5、在spring的配置文件中配置action和service

<bean name="personService" class="com.hy.service.PersonService"></bean>
<bean name="personAction" class="com.hy.action.PersonAction" scope="prototype">
<property name="personService" ref="personService"></property>
</bean>

注意action的scope必须是prototype(非单例模式)

6、在struts2配置文件中配置action

<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>

7、创建测试页面

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<a href="person-save">person-save</a>
</body>
</html>

附录:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <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> </web-app>

applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="person" class="com.hy.bean.Person">
<property name="name" value="wanghai"></property>
</bean> <bean name="personService" class="com.hy.service.PersonService"></bean>
<bean name="personAction" class="com.hy.action.PersonAction" scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
</beans>

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="false" /> <package name="default" namespace="/" extends="struts-default">
<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>
</package> </struts>

spring整合struts的更多相关文章

  1. spring 整合struts

    1.例子:未被spring整合 struts.xml 的配置文件 <constant name="struts.enable.DynamicMethodInvocation" ...

  2. SSH开发实践part4:Spring整合Struts

    1 好了,前面spring与hibernate的整合开发我们基本上讲完了,现在要开始服务层的开发,也就是处理事务的action,在这里我们需要引入spring与struts的整合.也就是将action ...

  3. Spring整合Struts的两种方式介绍

    1 使用Spring托管Struts Action 该种方式就是将Struts Action也视为一种Bean交给Spring来进行托管,使用时Struts的配置文件中配置的Action的classs ...

  4. 8 -- 深入使用Spring -- 7... Spring 整合 Struts 2

    8.7 Spring 整合 Struts2 8.7.1 启动Spring 容器 8.7.2 MVC框架与Spring整合的思考 8.7.3 让Spring管理控制器 8.7.4 使用自动装配

  5. Spring整合struts的配置文件存放问题

    只使用Spring的时候,我把applicationContext.xml是放在项目的src路径下的,这样使用ClassPathXmlApplicationContext很方便嘛 整合了struts之 ...

  6. spring 整合 struts

    struts配置 objectFactory 在struts.xml添加,用spring工厂管理action对象 <constant name="struts.objectFactor ...

  7. 【SSH】Spring 整合 Struts

    添加 spring-struts-3.2.9.RELEASE.jar struts-config.xml 添加 <controller> <set-property property ...

  8. Spring入门(四)— 整合Struts和Hibernate

    一.Spring整合Struts 1. 初步整合 只要在项目里面体现spring和 strut即可,不做任何的优化. struts 环境搭建 创建action public class UserAct ...

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

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

随机推荐

  1. C++ typedef详解

    1.typedef的用途1)定义一种类型的别名注意typedef并不是简单的宏替换,如下例所示: int main() { char *pa,pb;//声明了一个指向字符变量的指针pa,和一个字符变量 ...

  2. java之yield(),sleep(),wait()区别详解-备忘笔记

    备注:转载地址,http://dylanxu.iteye.com/blog/1322066,谢谢作者 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但 ...

  3. Oracle NoLogging Append 方式减少批量insert的redo_size

    业务处理中,很多时候使用实表临时表处理中间结果,而实表的Insert操作缺省会记录redo log,针对此问题收集相关测试总结信息如下: [转] 常见dml.ddl语句使用nologging选项所生成 ...

  4. log4j的针对包和类的配置方法

  5. ASP.NET MVC 中实现View与Controller分离

    一.开篇题外话 我经常会在博客园逛来逛去,看过很多大牛们的Blog,我很少在这块技术天地活动,之前有发表过几篇日志,好像大部分是和电商有关,作为一个多年的开发人员,很少在这里分享,之前一直在CSDN上 ...

  6. rsync实现免密码操作的一种实现方式

    rsync是远程文件同步协议,在linux系统下,操作服务器之间的文件同步,是非常方便高效的. 但是,简单的rsync操作,往往需要和用户交互,需要用户输入密码,这个对于结合应用系统使用,比如Java ...

  7. JS代码实现网站设为首页加入收藏功能

    <script language="javascript"> //加入收藏 function AddFavorite(sURL, sTitle) { try { win ...

  8. Servlet连接数据库

    测试连接数据库为MS Sql Server 2008 步骤一:去微软下载sqljdbc_4.0 步骤二:无需安装,解压出来,把sqljdbc4.jar包copy to Tomcat的lib目录下 步骤 ...

  9. 【设计模式】工厂方法模式(Factory Method)

    工厂方法模式 定义了一个创建对象的接口,但由子类决定要实现的类是哪一个.工厂方法让类把实例化推迟到子类.所有的工厂模式都用来封装对象的创建.工厂方法模式通过让子类决定改创建的对象是什么,来达到将对象创 ...

  10. Saltstack系列2:Saltstack远程执行命令

    命令 命令格式: salt '<操作目标>' <方法>[参数] 例: salt 'wx' cmd.run 'free -m' #查看被控主机内存使用情况 常用参数 针对< ...