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

整合步骤:

一、加入spring

1、加入spring jar包

2、配置web.xml文件

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:applicationContext.xml</param-value>
  4. </context-param>
  5.  
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>

3、加入spring配置文件

二、加入struts2

1、加入struts2 jar包

2、在web.xml配置

  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  4. </filter>
  5.  
  6. <filter-mapping>
  7. <filter-name>struts2</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

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

3、加入struts2配置文件

4、创建一个简单service和action

  1. package com.hy.service;
  2.  
  3. public class PersonService {
  4. public void save() {
  5. System.out.println("personService save...");
  6. }
  7. }
  1. package com.hy.action;
  2.  
  3. import com.hy.service.PersonService;
  4.  
  5. public class PersonAction {
  6.  
  7. private PersonService personService;
  8.  
  9. public PersonService getPersonService() {
  10. return personService;
  11. }
  12.  
  13. public void setPersonService(PersonService personService) {
  14. this.personService = personService;
  15. }
  16.  
  17. public String execute() {
  18. System.out.println("execute...");
  19. personService.save();
  20. return "success";
  21. }
  22. }

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

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

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

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

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

7、创建测试页面

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

附录:

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. version="3.0">
  7.  
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:applicationContext.xml</param-value>
  11. </context-param>
  12.  
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16.  
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. </filter>
  21.  
  22. <filter-mapping>
  23. <filter-name>struts2</filter-name>
  24. <url-pattern>/*</url-pattern>
  25. </filter-mapping>
  26.  
  27. </web-app>

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <bean name="person" class="com.hy.bean.Person">
  7. <property name="name" value="wanghai"></property>
  8. </bean>
  9.  
  10. <bean name="personService" class="com.hy.service.PersonService"></bean>
  11. <bean name="personAction" class="com.hy.action.PersonAction" scope="prototype">
  12. <property name="personService" ref="personService"></property>
  13. </bean>
  14. </beans>

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7.  
  8. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  9. <constant name="struts.devMode" value="false" />
  10.  
  11. <package name="default" namespace="/" extends="struts-default">
  12. <action name="person-save" class="personAction">
  13. <result>/success.jsp</result>
  14. </action>
  15. </package>
  16.  
  17. </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. kubernetes集群部署

    鉴于Docker如此火爆,Google推出kubernetes管理docker集群,不少人估计会进行尝试.kubernetes得到了很多大公司的支持,kubernetes集群部署工具也集成了gce,c ...

  2. 图片加载js类库

    Picturefill Picturefill.WP插件利用picturefill.js脚本展示Responsive图片,即根据视口宽度选择尺寸合适的图片加载,节省带宽,提高网站载入速度.例如用户用手 ...

  3. class的使用

    class test(object): """ get被称之为test对象的方法 """ def __init__(self,var1): ...

  4. js中的prototype和constructor

    本文正确性有待商榷,高手路过请不吝指教 1.js中只有对象,包括对象,函数,常量等. 对象不用解释.函数也有属性,常见之一就是prototype.常量也有属性: (3).__proto__;//Num ...

  5. exp命令ORACLCE10G导出ORACLE11G的数据1455错误

    异常:1455 解决: 命令:exp youruser/password@192.xxx.x.xx:1521/orcl owner=youruser file=c:\export.dmp trigge ...

  6. 基于HDInsight 3.4 HBase集群规划参考

    基于linux 创建HDInsight HBase集群,选择最小配置,zk(3).NN(2).WN(2),集群节点默认组件服务规划如下 NN0: Active NameNode /HDFS ZKFai ...

  7. [转]StringUtils方法

    摘自http://blog.sina.com.cn/s/blog_4550f3ca0100qrsd.html org.apache.commons.lang.StringUtils中方法的操作对象是j ...

  8. Spring实战4:面向切面编程

    主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...

  9. Java-Thread

    1. 线程的创建和启动 1.1 继承Thread 在run方法里,通过this获取当前线程. 多个线程不能共享实例变量. 1.2 通过实现接口 1.2.1 实现Runable接口 在run方法里,只能 ...

  10. Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x

    This howto guide shows you’ll how to enable EPEL repository under RHEL/CentOS 6/5 to install additio ...