整合目标:使用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. 004. 线程间操作无效: 从不是创建控件“textBox1”的线程访问它

    最简单的方法(不推荐): 在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false; 为什么不推荐上面的方法: 为避免空间造成死锁, .net f ...

  2. window.showModalDialog两次加载问题清除缓存方法

    问题: window.showModalDialog两次加载问题:你第一次打开窗口后,第二次浏览器没有从服务器端取数据,而直接找到了你已经下载的文件,也就是不再走后台的Action方法(即使数据已经更 ...

  3. Sql2008 全文索引应用(错误7625)

    在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查服务 ...

  4. python之路之正则表达式

    匹配格式^ 匹配字符串的开头$ 匹配字符串的结尾. 除了换行符外的所有字符[...] 用来表示一组字符,,单独列出:[amk]匹配'a','m'或'k'[^..] 不在[]中的字符:[^abc]匹配除 ...

  5. MYSQL 分组排名

    今天遇到一个MYSQL排序的问题,要求按某列进行分组,组内进行排序. 百度一下发现MYSQL不支持row_number(),rank()等函数. 采用的办法如下,我们首先创建一个测试表: --创建表 ...

  6. js页面取值的三种方式

    <input id=""<radio <checkbox<div<img对于这些标签内参数取值,一般分为三种类型:一.有关id取值用 #:取id处的v ...

  7. 微博一键分享主要通过对指定 URL 添加各种参数来实现;

    微博一键分享主要通过对指定 URL 添加各种参数来实现:也可以用在线生成器自动生成. 示例: 搜狐微博一键分享 URL,只需三个参数: http://t.sohu.com/third/post.jsp ...

  8. web几个高性能框架的简单测试

    参考的这里 压测工具 wrk -t16 -c100 -d30s http://127.0.0.1:8080/rest/hello 测试代码 package main import ( "st ...

  9. 【gradle】之maven主库找不到Could not find org.restlet.jee:org.restlet:2.1.1

    Could not find org.restlet.jee:org.restlet:2.1.1. 我是用gradle构建solr的时候出现的这个错误,通过google查询到这么一段解释 For th ...

  10. "unresolved external symbol __imp__WSACleanup@0"

    编译时出现这种问题怎么解决:"unresolved external symbol __imp__WSACleanup@0"出现此类问题一般是ws2_32.lib这个lib没有li ...