Spring集成struts (1)

Spring整合集成Struts1.2最简单例子 立即下载

为了集成struts,首先要进行Spring上下文的装载,有两种上下文的装载,

(1) 在struts-config.xml中装载Spring上下文,此种装载Spring上下文的方法是利用struts的Plugin机制在 struts-config.xml中声明一个Plugin类org.spring.springframework.web.struts.ContextLoaderPlugIn,代码如下,

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <global-forwards>
    </global-forwards>

    <action-mappings>
          <action path="/chartAction" type="lai.chart.ChartAction"
                scope="request"
                validate="false">
     <forward name="diaplay" path="/DisplayChart"/>
    </action>
    </action-mappings>
   <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

    <message-resources parameter="MessageResources" />

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation"
      value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>

</struts-config>

(2)在web.xml中装载Spring上下文,此种方法利用J2EE的Servlet2.4规范中的ContextListener机制,代码如下,

在装载了上下文后,我们就可以通过Spring提供的WebApplicationContextUtils工具类,在Web应用的任何场所访问该上下文了,如下,

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>Spring JPetStore</display-name>
 <description>Spring JPetStore sample application</description>
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>/WEB-INF/log4j.properties</param-value>
 </context-param>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   /WEB-INF/dataAccessContext-local.xml
                    /WEB-INF/applicationContext.xml
                    /WEB-INF/struts-actionmapping.xml
  </param-value>
 </context-param>
 <listener>
 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <!--
   - Loads the root application context of this web app at startup,
   - by default from "/WEB-INF/applicationContext.xml".
  - Note that you need to fall back to Spring's ContextLoaderServlet for
  - J2EE servers that do not follow the Servlet 2.4 initialization order.
   - Use WebApplicationContextUtils.getWebApplicationContext(servletContext)
   - to access it anywhere in the web application, outside of the framework.
   - The root context is the parent of all servlet-specific contexts.
   - This means that its beans are automatically available in these child contexts,
   - both for getBean(name) calls and (external) bean references.
   -->
 <listener>
  <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>petstore</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>axis</servlet-name>
  <url-pattern>/axis/*</url-pattern>
 </servlet-mapping>
</web-app>

如果不支持Servlet2.4则使用Spring提供的ContextLoaderServlet,如下,

<servlet>

<servlet-name>contextLoader</servlet-name>

<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

<load-on-startup>1<load-on-startup>

</servlet>

ApplicationContext  ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

另外,还有三种整合手法,分别是

(1)使用Spring的ActionSuport类整合Struts

(2)使用Spring的DelegatingRequestProcessor覆写Struts的RequestProcessor.

(3)使用Spring委托管理Struts的Action

1. 使用 Spring 的 ActionSupport类整合Struts

  手动创建一个 Spring 环境是一种整合 Struts 和 Spring 的最直观的方式。为了使它变得更简单,Spring 提供了一些帮助。为了方便地获得 Spring 环境,org.springframework.web.struts.ActionSupport 类提供了一个 getWebApplicationContext() 方法。您所做的只是从 Spring 的 ActionSupport 而不是 Struts Action 类扩展您的动作,如清单 1 所示:

  清单 1. 使用 ActionSupport 整合 Struts

package ca.nexcel.books.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;

import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;

public class SearchSubmit extends ActionSupport {   |(1)


  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    DynaActionForm searchForm = (DynaActionForm) form;
    String isbn = (String) searchForm.get("isbn");
  
    //the old fashion way
    //BookService bookService = new BookServiceImpl();
  
    ApplicationContext ctx =
      getWebApplicationContext();    |(2)
    BookService bookService =
      (BookService) ctx.getBean("bookService");   |(3)
       
  Book book = bookService.read(isbn.trim());

    if (null == book) {
      ActionErrors errors = new ActionErrors();
      errors.add(ActionErrors.GLOBAL_ERROR,new ActionError
        ("message.notfound"));
      saveErrors(request, errors);
      return mapping.findForward("failure") ;
  }

    request.setAttribute("book", book);
    return mapping.findForward("success");
  }
}

  让我们快速思考一下这里到底发生了什么。在 (1) 处,我通过从 Spring 的 ActionSupport 类而不是 Struts 的 Action 类进行扩展,创建了一个新的 Action。在 (2) 处,我使用 getWebApplicationContext() 方法获得一个 ApplicationContext。为了获得业务服务,我使用在 (2) 处获得的环境在 (3) 处查找一个 Spring bean。这种技术很简单并且易于理解。不幸的是,它将 Struts 动作与 Spring 框架耦合在一起。如果您想替换掉 Spring,那么您必须重写代码。并且,由于 Struts 动作不在 Spring 的控制之下,所以它不能获得 Spring AOP 的优势。当使用多重独立的 Spring 环境时,这种技术可能有用,但是在大多数情况下,这种方法不如另外两种方法合适。

 
struts1和spring整合 NULL 博文链接:https://xiaoxuejie.iteye.com/blog/1202313 立即下载

相关推荐

告别兼容性困扰:在F-Droid中安全部署Termux的完整指南

本文提供了一份在F-Droid中安全部署Termux的完整指南,旨在彻底解决Android设备上的兼容性困扰。文章详细阐述了为何选择F-Droid而非Google Play商店,并逐步指导用户完成从彻底清理旧版本、安装F-Droid客户端、授予权限到最终安装配置Termux的全过程。指南还涵盖了进阶的稳定性配置、数据迁移方法以及常见问题排查,确保用户获得一个稳定、无冲突的命令行工作环境。

weixin_29224309的博客 948

深入探讨Spring与Struts集成方案

引言Spring是一个轻量级(大小和系统开支的角度)的IoC和AOP容器.它力图简化J2EE开发即J2EE without EJB.而且作为帮助企业级开发的核心支柱,Spring为模型层(OR持久层:Hibernate、JDO、iBatis等)服务层(EJB、JNDI、WebService)以及表现层(Struts、JSF、Velocity)都提供了良好的支持和集成方案. 访问Spring官方站J

hbrqlpf的专栏 790

为何LPDDR4支持on-die ECC,但是DDR4却不支持呢?

DRAM的ECC,目前的编码方式都是128bit data,产生8bit parity或者ECC code. 而且必须要128bit才行。这8bit的ECC code是要被写入到DRAM内存里面的,从而read的时候,可以读取这8bit ECC code来判定read data是否有错,如果是SBF,就可以纠错了,如果是double bit,大部分时候是可以detect到错误,即所谓的SEC(Single Error Correct) + DED(Double Error Detect).

一个DRAM从业者的告白 1557

spring整合struts1三种方式

spring和struts整合方式 1、  最简单方式 ①struts来装载spring的应用环境。就是在 struts 中加入一个插件。 struts-config.xml中 ="/WEB-INF/applicationContext.xml"/> ②几乎与struts无关了,相当于为整合项目加入spring支持,将①的配置放在web.xml中了 context-pa

ryan1214的专栏 760

工厂自动化布线实战:从功率计算到线槽布局的完整避坑指南

本文详细介绍了工厂自动化布线的实战经验,从功率计算到线槽布局的完整避坑指南。通过三步计算法确定线径需求,强调电压降控制的重要性,并分享线槽布局的50%法则和强弱电分离技巧。特别针对移动设备和多芯信号线提供专业解决方案,帮助工程师避免常见错误,提升布线效率和质量。

weixin_29266007的博客 355

C#与欧姆龙PLC 通信——fins udp协议

该调试助手专门为了帮助大家学习以及测试Fins-Udp通信,下面进行详细介绍。

qq_34059233的博客 2876

Spring与Struts1的集成

<br />  <br />Spring和Struts整合的价值在于将Struts使用的BO或DAO 乃至Action交给Spring管理,从而充分利用Spring强大的IoC和AOP 特性。<br /> <br />无论使用哪种方式整合,都需要为 Struts装载 Spring 应用上下文环境。有以下三种方式:<br /> <br /><br />1) 在struts-config.xml中使用Struts Plugin<br /><plug-in className="org.springframew

java journey 1501

【教程】OpenClaw交互入口解析WebChat与message CLI

WebChat和message CLI作为OpenClaw的直接交互入口,大大丰富了系统的使用场景和灵活性。理解它们的工作原理、适用场景和设计哲学,对于充分利用OpenClaw的全部功能至关重要。WebChat作为Gateway WebSocket上的聊天UI,提供了直观的图形界面,适合人工调试和系统监控;作为命令行工具,通过HTTP协议与Gateway交互,适合自动化和脚本化场景。两者相辅相成,共同构成了OpenClaw的"控制平面"。掌握正确的使用顺序。

海兰 302

struts1和spring的整合

1.先要在struts中配置 将spring的配置文件加载进actionservlet总控中以便等会进行使用 这样可以避免在web.xml下进行配置       2.然后有三种方式,一种是继承actionsupport,另一种是使用DelegatingRequestProcessor进行配置, 最后一种是使用DelegatingAcionProxy,官方推荐最后一种 3.我们

sdtxz951023的博客 1602

【DEIM创新改进】独家下采样创新改进篇 | ECCV 2026 | DEIM 引入FSD-Down频域-空间动态下采样,在下采样过程中保留小目标的高频信息、助力小目标检测、遥感小目标检测有效涨点

🔥本文提出了一种名为FSD-Down的频域-空间动态下采样模块,用于改进DEIM网络模型在小目标检测中的性能。FSD-Down通过小波分解显式保留低频语义和高频细节,结合分组卷积、动态通道权重和可学习缩放因子,自适应强化目标边缘与纹理特征。该模块能有效减轻传统下采样造成的频率混叠和细节丢失,提升对小目标、密集目标和低对比度目标的检测能力。实验表明,FSD-Down计算开销可控,在保持实时性的同时显著提升检测精度,特别是在VisDrone和AITODv2数据集上对小目标的检测性能提升显著。

Ai缝合怪、助力小伙伴高效发论文 83

如何跑通SuperPoint+LightGlue的训练代码使用gluefactory进行训练和评估?

🏆本文收录于专栏,主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家!持续更新中,up!up!up!!

**My Coding Family** 1786

spring 与struts的集成

Struts与Spring的集成主要有两种方式,即DelegatingRequestProcessor和DelegatingActionProxy方式,这两种方式应用了Spring IOC来管理Struts的Action,顾名思义,两者即是在代理某对象,熟释Struts的必然知晓RequestProcessor和Action,Spring在这两处切入到页面层的请求链中(通过扩展Struts中的Re...

jc_dreaming2的博客 193

【智能车Code review】——小S与中S道路判断

基本思想: 找到中线的左拐点和右拐点,并且计算出两点之间的空间距离。通过限制空间距离,分为中S和小S。 注意这种情况:当20行以下的中线斜率比较陡,此时可以通过人为限制设定,将第0行的中线设置为拐点。 if (flag_centre_right_point == 0 && flag_centre_left_point == 1 && parameterB >= 0.5 && centre_left_point[0] >= 33) { .

hanhan的博客 6106

【第十章】集成其它Web框架 之 10.1 概述 集成Struts1.x ——跟我学spring3

10.1  概述 10.1.1  Spring和Web框架        Spring框架不仅提供了一套自己的Web框架实现,还支持集成第三方Web框架(如Struts1x、Struts2x)。          Spring实现的SpringMVC Web框架将在第十八章详细介绍。          由于现在有很大部分公司在使用第三方Web框架,对于并不熟悉Spr

huazaichang的专栏 817

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

struts1.x+spring+hibernate集成

struts1.x+spring+hibernate集成(Action交给spring来创建,这时候要注意代理,实现了接口的类才能使用JDK动态代理,没有实现接口的类则只能使用CGLIB)  * 将所有的jar包拷贝到WEB-INF/lib下,如果jar包有重复的则删除一些,去版本新的 * 将spring,struts,hibernate的配置文件拷贝过来,spring和hiberna

wlxtaking的专栏 633

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

struts1和2集成spring

struts1.x与spring集成方式   方式一:Action交给spring管理,将业务类注入action           引入spring-webmvc-struts.jar包到lib下,             struts配置: &lt;action path="/searchAction" type="org.springframework.web.s...

Happy learning 186

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Struts1 Spring2 iBatis2 框架的集成

这个是属于比较老的框架了,奈何现在公司用的产品就是如此,闲来就搭一个集成框架吧 依赖jar包 antlr-2.7.6.jar aspectj-1.8.2.jar aspectjrt.jar aspectjweaver-1.6.12.jar bsf-2.3.0.jar cglib-nodep-2.1_3.jar commons-beanutils-1.8.0.jar co...

weixin_30483495的博客 97

Struts1.X与Spring集成——第二种方案

上篇博客介绍了Struts1.X与Spring集成的一种方案。http://blog.youkuaiyun.com/hanxuemin12345/article/details/38065551 第一种方案集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象 此种方案的缺点:从严格意义的分层上来看,Action上看到了Spring的相关东西,依赖Spring

葵歌小妖 2363

【第十章】集成其它Web框架 之 10.2 集成Struts1.x ——跟我学spring3

先进行通用配置, 【第十章】集成其它Web框架 之 10.1 概述  10.2  集成Struts1.x 10.2.1  概述        Struts1.x是最早实现MVC(模型-视图-控制器)模式的Web框架之一,其使用非常广泛,虽然目前已经有Struts2.x等其他Web框架,但仍有很多公司使用Struts1.x框架。        集成Struts1.x也非常简单

❀蓝❤尐❤调❀ 391

Struts1集成Spring2时要注意!

Struts1集成Spring2时要注意,如果要由Spring2管理Action的话,需要在/WEB-INF下面建立文件action-servlet.xml文件,然后加入action对应的,其中name与的path属性一致,此处不能id。Spring1中可以放在默认的applicationContext.xml中,而Spring2须放到单独的action-servlet.xml中这

490

spring和struts1和jpa的集成

spring和jpa的集成 在src目录下建立beans.xml文件 其中beans.xml内容如下 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="htt

Senssic 869

Struts1.x/2.x 与 Spring集成方式

struts1.x与spring集成方式 方式一:Action交给spring管理,将业务类注入action struts配置: forward name="success"path="/result.jsp"/> /action spring配置: !--这里SearchAction继承了DelegatingActionProxy--

ETHAN IN IT 803

java struts1_Struts1.X与Spring集成

spring+struts(第一种方案)集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象,调用业务逻辑方法。一,新建一个项目Spring_Struts_01二,步骤1、spring和Struts的依赖包配置*Struts(1)拷贝Struts和jstl的依赖包(2)在web.xml文件中配置ActionServletxmlns="http://jav...

weixin_39655085的博客 234

Spring分别与Struts1.x 和Struts2.x集成方法

[align=center][size=xx-large][b][color=red]Spring分别与Struts1.x 和Struts2.x集成方法[/color][/b][/size][/align] 1.整合struts1: a.可以通过action继承spring提供的ActionSupport来获得spring中的bean, b.配置struts的过程处理器cotro...

mohaiyong的专栏 141
上一篇: Spring2.0压缩包内容
下一篇: Spring集成struts(2)
eyejava
博客等级 码龄20年 11粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值