Dynamic Web Project 的Struts2项目中引入Spring框架

在Struts2项目中引入Spring框架主要需要以下几个步骤:

  1. 引入Spring jar包
  2. 修改web.xml
  3. 创建bean上下文文件

引入Spring.jar包

    Spring相关的文件可以直接在官网上下载,或者使用Struts包里的Spring文件,本博主使用的Struts版本为2.5.17, 在该lib目录下包含了spring jar包, 若要在Struts项目中使用Spring,则需要引入下图选中的jar组件,特别是struts2-spring-plugin-2.5.17,通过该组件,使得Action也支持被Spring容器管理

将以上文件复制到项目的 WebContent/WEB-INF/lib目录,在Eclipse IDE的 Project Explorer对应的节点  WebContent/WEB-INF/lib,刷新,选中刚引入的spring相关组件, 右击, 在弹出的菜单中选中Build Path,将这些组件添加到BuildPath 中

 

修改web.xml

在Web.xml中 添加 以下代码行。

 <context-param>
    <param-name>contextConfigLocation</param-name>
     <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param> 
	<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

创建bean上下文文件

在src根目录下创建文件 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"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

     <bean id="BeanID" class="homesystem.HelloWorldAction" scope="prototype">
    
    <property name="curPeople" ref="people">
    </property>
    </bean>

</beans>

 

调整Action的类属性为 spring中的bean id

 

    	 <action name="hello" class="BeanID"   > 
         	<result name="success" >/second.jsp</result> 
        </action>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值