Spring框架整合Struts2框架的传统方法

1. 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action

  * 将menu.jsp中133行的新增客户的跳转地址改为:href="${pageContext.request.contextPath}/jsp/customer/add.jsp"

  * 将jsp/customer下的add.jsp的提交页面的地址改为:action="${pageContext.request.contextPath }/customer_save"。当点击保存按钮之后,访问customer这个action中的save方法。

2. 创建包结构:

  * ssh1

    * com.huida.dao

    * com.huida.domain

    * com.huida.service

    *com.huida.web

3. 在com.huida.web下创建action类CustomerAction。

 在com.huida.service下创建接口:CustomerService,实现接口的类:CustomerServiceImpl。

 CustomerService接口中的方法为:

package com.huida.service;

public interface CustomerService {

    public void save();
}

   CustomerServiceImpl实现类的内容为:

package com.huida.service;

public class CustomerServiceImpl implements CustomerService {

    @Override
    public void save() {
        
        System.out.println("Service层中的save方法被执行了");
        
    }

}

4. 在applicationContext.xml中配置我们的service。配置内容为:

<?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>
</beans>

5.编写CustomerAction接收请求,在struts.xml中完成Action的配置

   CustomerAction的接收请求为:

package com.huida.web;import com.huida.domain.Customer;
import com.huida.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;public class CustomerAction extends ActionSupport{

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


}

   在struts.xml中对Action的配置为:

<package name="crm" namespace="/" extends="struts-default">
        <action name="customer_*" class="com.huida.web.CustomerAction" method="{1}">
            <result name=""></result>
        </action> 
</package>

6.在Action中获取到service(开发不会使用,因为麻烦)

 * 可以通过 WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext()); 来获取,但是这种方式编写代码太麻烦了!!

 代码如下:

ackage 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;

public class CustomerAction extends ActionSupport {

    /*
     * 保存客户的方法
     */
    public String save(){
        System.out.println("Action中执行了save方法");
        //传统的web工厂方法
        WebApplicationContext webApplicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
        CustomerService customerService=(CustomerService) webApplicationContext.getBean("customerService");
        customerService.save();
        return NONE;
    }
}

    * 还可以通过new ClassPathXmlApplicationContext("applicationContext.xml")来获取。

 代码如下:

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 {

    /*
     * 保存客户的方法
     */
    public String save(){
        System.out.println("Action中执行了save方法");
        //使用spring的工厂
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerService customerService=(CustomerService) ac.getBean("customerService");
        
        customerService.save();
        return NONE;
    }

}

     通过这两种方法我们都可以实现从action中获取service。

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

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

    

   通过以上步骤我们便将struts与spring通过传统的方法整合起来了。但是这种整合很麻烦,所以在下一篇博文中我就对整合的常用方法进行总结。

 

  

 

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值