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

1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整合起来,并且实现了action获取service,说明spring与struts2框架已经建立联系,互通了,但是这种使用web工厂的方式太麻烦了,在开发中并不会使用这种方法,所以我就要介绍spring整合struts2框架的另外一种方法。目前有两种方法,现在介绍第二种方式,第一种方式见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10143918.html

2. 把具体的Action类配置文件applicatonContext.xml的配置文件中,但是注意:struts.xml需要做修改:

  我们需要在applicationContext.xml中定义action,action由spring来进行管理,在原有配置applicationContext.xml中添加下面这组语句:

<!-- 将action由spring来管理 -->
    <!-- 通过scope设置来设置action为多例 -->
    <bean id="customerAction" class="com.huida.web.CustomerAction" scope="prototype">
        <!-- 必须手动注入service属性 -->
        <property name="customerService" ref="customerService"></property>
    </bean>

  完整的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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <bean id="customerService" class="com.huida.service.CustomerServiceImpl">
        
    </bean>
    
    <!-- 
        需要主要的两点:
        1.struts.xml对应的action标签中的class属性填充的是spring中applicationContext.xml中对应Bean的Id
        2.一定要给我们的action配置多例
     -->
    <!-- 将action由spring来管理 -->
    <!-- 通过scope设置来设置action为多例 -->
    <bean id="customerAction" class="com.huida.web.CustomerAction" scope="prototype">
        <!-- 必须手动注入service属性 -->
        <property name="customerService" ref="customerService"></property>
    </bean>
    
    
</beans>

3. 更改完applicationContext.xml后,由于我们在这里面定义了Action,所以我们在struts.xml中定义action的时候,在action标签中的class属性不用写全路径了,而填充的是spring中applicationContext.xml中对应Bean的Id。更改后的配置内容为:

<?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>
    <package name="crm" namespace="/" extends="struts-default">
        <!-- action的全路径写在applicationContext.xml中 -->
        <action name="customer_*" class="customerAction" method="{1}">
            <result name=""></result>
        </action>
    </package>
</struts>

4. 我们的Service同样使用上一篇博文的方式来进行获取,在Action中使用数据封装的方式进行获取,所以我们只需要在CustomerAction中定义CustomerService对象,然后定义其Set方法即可:

package com.huida.web;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.huida.domain.Customer;
import com.huida.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{

    //手动实例化对象
    //private Customer customer=new Customer();
    
    private CustomerService customerService;
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    /*
     * 保存客户的方法
     */
    public String save(){
        System.out.println("Action中执行了save方法");
        customerService.save();
        return NONE;
    }

    @Override
    public Customer getModel() {
        
        return null;
    }
}

5. 我们可以验证一下struts与spring整合是否成功。

启动服务器-->在浏览器中输入http://localhost:8080/ssh1-->在页面中点击客户管理-->新增客户-->点击保存按钮。在控制台上输出如下内容:

 

通过以上步骤我们便将struts与spring通过传统的方法整合起来了。

6.第二种方式需要有几个注意的地方:

  (1)struts.xml对应的action标签中的class属性填充的是spring中applicationContext.xml中对应Bean的Id

  (2)pring框架默认生成CustomerAction是单例的,而Struts2框架是多例的。所以需要配置 scope="prototype"

  (3)CustomerService现在必须自己手动注入了。

转载于:https://www.cnblogs.com/wyhluckdog/p/10144175.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值