Spring整合Struts2(实操)

本文详细介绍Spring与Struts框架的整合步骤,包括如何配置Spring管理Struts的Action,使用不同方式向Action传递数据,以及整合过程中的关键配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于如何配置Spring和Struts网上有很多解析,如下:

http://blog.youkuaiyun.com/riyunzhu/article/details/7887901

这里主要记录一下实际操作的时候的一些问题。


如何生成页面:

 1. 在webapp的文件夹下创建前段文件,如jsp,httl或html等。
 2. 在struts.xml文件中指定对应的action类
 3. 编写action代码

如何向action中传送数据?

方法1:通过form表单

<form id="addNews" method="post" action="action中的方法名">
    <input id="" name="变量名.属性" />
    <input type="submit" name="button" value="提交"/>
</form>

该form表单会将input中的数据提交到action对应的方法中进行处理。
action中需要创建一个变量,而且该要生成get和set方法,才可以接受参数传递。

方法2:通过ajax函数

var 参数1 = 对应的值1
var 参数2 = 对应的值2
$.ajax({
    type:'post',
    url:'action中的方法名.do',
    data:{'参数1':对应的值1,'参数2':对应的值2},
    dataType:'json',
    cache:false,
    success:function(result) {
        //返回数据的处理逻辑
    }
});

如何让Spring管理action?

也就是如何把spring和struts整合到一起:
1.在struts.xml文件中添加配置:

 <constant name="struts.objectFactory" value="spring" />

2.在applicationContent.xml中添加action的注解类,以生成自动扫描:

<context:component-scan base-package="action的类" />

3.再action的类上添加注解@controller,和@Scope(“prototype”),因为这种配置方式,是让spring创建action的bean所以需要指定prototype属性。


关于Spring和Struts2的详细解析

配置文件

applicationContext.xml:

用来描述依赖关系,初始化连接池,并对其进行参数配置,事物处理,初始化注解驱动,加载注解的类。

        <!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl"
        value="jdbc:mysql://你的mysql数据库?characterEncoding=UTF8&amp;zeroDateTimeBehavior=convertToNull" />
    <property name="user" value="root" />
    <property name="password" value="密码" />
    <property name="initialPoolSize" value="2" />
    <property name="minPoolSize" value="2" />
    <property name="maxPoolSize" value="10" />
    <property name="maxIdleTime" value="1800" />
    <property name="acquireIncrement" value="3" />
    <property name="maxStatements" value="0" />
    <property name="idleConnectionTestPeriod" value="1800" />
    <property name="acquireRetryAttempts" value="30" />
    <property name="testConnectionOnCheckout" value="false" />
</bean>
<!-- simpleJdbcTemplate -->
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
    <constructor-arg ref="dataSource" />
</bean>

    <!-- 事务处理 -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
    <!-- 初始化注解驱动 -->
<context:annotation-config />
<!-- 加载注解的类 -->
<context:component-scan base-package="加载注解的包名.类名" />

Struts2.xml:

<package name="pkg" namespace="/" extends="httl-default">
     <!-- 全局Result -->
    <global-results>
        <result name="exception-error" type="redirect">/error.htm</result>
    </global-results>
    <action name="*/*/*" class="{2}Action" method="{3}">
        <result name="{3}" type="httl">/{1}/{2}.httl</result>
    </action> 
</package>

这样可以让WEB-INF文件夹下的jsp或者httl页面可以找到指定的action:

<action name="*/*/*" class="{2}Action" method="{3}">
    <result name="{3}" type="httl">/{1}/{2}.httl</result>
</action>

这个是web的文件结构:
这里写图片描述
这个是action类的文件结构:
这里写图片描述
这样访问地址就为:

http://localhost:8080/工程名/car/carAddNews/view.do

view.do是action中的方法,再输入连接地址的时候会直接调用该方法,并执行。

web.xml:

  • 配置核心拦截器filter
  • 配置监听器listener
  • 容器启动时自动加载applicationContext,实现执行listener方法。
  • 当然还有定义Servlet

附上一张action和Spring整合的图解:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值