Spring与Struts框架整合

Spring,负责对象对象创建

Struts, 用Action处理请求

Spring与Struts框架整合,

         关键点:让struts框架action对象的创建,交给spring完成!

Spring与Hibernate整合:

【SSH整合:

                   Spring与Struts

                            关键点: action交给spring创建!

                   Spring与Hibernate

                            关键点: sessionFactory对象交给spring创建!

步骤:

引入jar文件

         1)引入struts .jar相关文件

         2)spring-core  相关jar文件

         3)spring-web 支持jar包

                   spring-web-3.2.5.RELEASE.jar                            【Spring源码】

struts2-spring-plugin-2.3.4.1.jar      【Struts源码】

配置:

         4)配置XML

struts.xml                  【struts路径与action映射配置】

bean.xml               【spring ioc容器配置】

web.xml                    

【核心过滤器: 引入struts功能】

【初始化spring的ioc容器】


package loaderman.action;

import com.opensymphony.xwork2.ActionSupport;
import loaderman.service.UserService;


public class UserAction extends ActionSupport {


    private UserService userService;
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    

    public String execute() {
        userService.save();
        return SUCCESS;
    }
}
package loaderman.dao;

public class UserDao {

    public void save() {
        System.out.println("DB:保存用户");
    }
}
package loaderman.service;


import loaderman.dao.UserDao;

public class UserService {

    //IOC容器注入
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save() {
        userDao.save();
    }
}
<?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.xsd">

    
    <!-- 指定action多例 -->
    <bean id="userAction" class="loaderman.action.UserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    
    
</beans>     
<?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.xsd

     ">

    
    <bean id="userDao" class="loaderman.dao.UserDao" scope="singleton" lazy-init="false"></bean>
    
    
</beans>     
<?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.xsd
         ">

    
    <bean id="userService" class="loaderman.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
    
</beans>     
<?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="user" extends="struts-default">

        <action name="user" class="userAction" method="execute">
            <result name="success">/index.jsp</result>
        </action>

    </package>

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">



    <!-- 1. 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>

    <!-- 2. spring 配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/bean-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
原文地址:https://www.cnblogs.com/loaderman/p/10042313.html