zoukankan      html  css  js  c++  java
  • 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通过传统的方法整合起来了。但是这种整合很麻烦,所以在下一篇博文中我就对整合的常用方法进行总结。

      

  • 相关阅读:
    Linux 安装 jdk 后 jps 出现问题/usr/jdk1.8.0_151/bin/jps: /lib/ld-linux.so.2: bad ELF interpreter: No such
    Jackson 注解
    Git 右键添加Git Bash
    No validator could be found for constraint
    rror querying database. Cause: java.sql.SQLException: null, message from server: "Host '192.168.30.1' is not allowed to connect to this MySQL server"
    Linux 安装 Mysql-5.7.23-linux-glibc2
    Promise
    PAT(B) 1094 谷歌的招聘(Java)
    PAT(B) 1074 宇宙无敌加法器(Java)
    PAT(B) 1078 字符串压缩与解压(Java)
  • 原文地址:https://www.cnblogs.com/wyhluckdog/p/10140588.html
Copyright © 2011-2022 走看看