Struts2 + Spring 整合

本文介绍如何将Struts2与Spring框架进行整合,包括必要的Jar文件、web.xml配置、struts.properties设置、Spring配置文件、struts.xml配置及Action类实现。通过这些步骤,可以实现Spring对Struts2组件的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1. Jar 文件   整合除了Struts2必须 的jar文件:

     > struts2-core-xxx.jar

     > xwork-xxx.jar

     > ognl-xxx.jar

     > freemarker-xxxjar

     > commons-logging-xxx.jar

以外,还需要Spring的核心jar文件:

  • spring-core.jar
  • spring-beans.jar
  • spring-context.jar
  • spring-web.jar
  • struts2-spring-plugin-xxx.jar                          //  Spring与struts2的插件jar
注: xxx 是具体的版本号.对于其他扩展的jar文件,可自行添加


2. web.xml 配置,可参考:

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


    <!-- Struts cleanup filter -->
    <filter>
        <filter-name>struts_cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts_cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- struts2 core filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring context listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>


3. struts.properties. 位于类路径下,可自行创建,配置如下:

#spring context
struts.objectFactory = spring


4. applicationContext.xml. Spring配置文件,位于WEB-INF目录,可参考:

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="lotteryService" class="com.mkk.struts2.service.LotteryServiceImpl" />

    <bean id="springAction" class="com.mkk.struts2.spring.SpringAction"
        scope="prototype">
        <property name="lotteryService" ref="lotteryService" />
    </bean>

</beans>


5. struts.xml. Struts2配置文件,位于类路径下.可参考:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!-- Spring action -->
    <package name="spring_package" extends="struts-default" namespace="/spring">
        <action name="spring" class="springAction">
            <result>/spring/spring.jsp</result>
        </action>
    </package>

</struts>

注意: action的class值是对应的Spring配置文件中bean的id.


6. 对应的Action类.可参考:

public class SpringAction extends ActionSupport implements InitializingBean {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -1506018657717304870L;

    private LotteryService lotteryService;

    public String init() throws Exception {
        getLottery();
        return super.execute();
    }

    /**
     * Lottery action
     *
     * @return
     * @throws Exception
     */
    public String lottery() throws Exception {
        getLottery();
        return SUCCESS;
    }

    private void getLottery() {
        String res = lotteryService.getNextRandomLotteryNumber();
        ServletActionContext.getRequest().setAttribute("result", res);
    }

    public void setLotteryService(LotteryService lotteryService) {
        this.lotteryService = lotteryService;
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(lotteryService);
    }

}


Ok, 半个SSH2(Spring + Struts2 )整合完毕. 方便自己在工作中查询,也方便 别人.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值