Struts2和Spring进行整合

版权声明: https://blog.csdn.net/NewstarCSDN/article/details/72824515

Struts2整合Spring,整合方式一:

动作类还是Struts2负责管理,只是向Spring容器要service的实例

  • 0、拷贝jar包

这里写图片描述

  • 1、搭建Spring的web环境
    WEB-INF下web.xml文件
 <!-- web中继承Spring核心容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  • 2、搭建Struts2的开发环境
    WEB-INF下web.xml文件
<!-- 配置struts环境 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

WEb-INF下的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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean name="myservice" class="com.itstar.service.impl.MyserviceImpl">
        <property name="dao" ref="personDao"></property>
    </bean>
    <bean name="personDao" class="com.itstar.dao.impl.PersonDaoImpl"></bean>
</beans>

struts.xml文件

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

    <constant name="struts.devMode" value="true"></constant>
<package name="p1" extends="struts-default">
<action name="demo1" class="com.itstar.action.Demo1Action" method="demo1">
<result name="success" > success.jsp</result>
</action>
</package>


</struts>
  • **3、拷贝Struts2和Spring的插件过来。struts2-spring-plugin-2.3.15.3.jar
    **
  • *4、通过配置文件替换调用ObjectFactory(不用做,struts2-spring-plugin-2.3.15.3.jar)
  • **
  • 业务层接口
package com.itstar.service;

public interface Myservice {
    void save();
}
  • 业务接口实现
package com.itstar.service.impl;

import com.itstar.dao.PersonDao;
import com.itstar.dao.impl.PersonDaoImpl;
import com.itstar.service.Myservice;

public class MyserviceImpl implements Myservice {
    private PersonDao dao;

    public void setDao(PersonDaoImpl dao) {
        this.dao = dao;
    }

    @Override
    public void save() {

        dao.save();

    }

}
  • dao层接口
package com.itstar.dao;

public interface PersonDao {
    void save();
}
  • dao层实现
package com.itstar.dao.impl;

import com.itstar.dao.PersonDao;

public class PersonDaoImpl implements PersonDao {

    @Override
    public void save() {
        System.out.println("保存到了数据库");

    }

}
  • Action
package com.itstar.action;


import com.itstar.service.Myservice;
import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport {
    private Myservice myservice;


    public Myservice getMyservice() {
        return myservice;
    }


    public void setMyservice(Myservice myservice) {
        this.myservice = myservice;
    }

    public String demo1() {
        myservice.save();
        return SUCCESS;
    }
}
  • WebRoot下创建
    -success.jsp

-运行到tomcat,访问http://localhost:8080/struts_StrutsAndSpring/demo1

这里写图片描述

-整合方式二:动作类也交给Spring管理
在applicationContext.xml中加入

<bean name="demo1Action" class="com.itstar.action.Demo1Action"
        scope="prototype">
        <property name="myservice" ref="myservice"></property>
    </bean>`

struts.xml文件修改一下

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

    <constant name="struts.devMode" value="true"></constant>
    <package name="p1" extends="struts-default">
        <action name="demo1" class="demo1Action" method="demo1">
            <result name="success"> success.jsp</result>
        </action>
    </package>


</struts>

执行结果一样
-运行到tomcat,访问http://localhost:8080/struts_StrutsAndSpring/demo1

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页