24.Spring_与Web应用整合

本文介绍如何将Spring框架与Struts2进行整合,通过配置示例详细展示了整合过程。主要内容包括配置Spring与Struts2的相关文件,如web.xml、applicationContext.xml及struts.xml,并演示了如何在Action类中注入依赖。

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


一、基本思路

1.jar包:

额外加入:spring-web-4.0.0.RELEASE.jar    spring-webmvc-4.0.0.RELEASE.jar

2.Spring配置文件:差别不大

3.如何创建IOC容器:

① 非WEB应用中直接在main直接创建

②WEB应用应该在WEB应用被服务器加载时就创建IOC容器:在ServletContextListener下contextInitialized(ServletContextEvent sce)方法下创建

③在WEB应用的其他组件中如何访问IOC容器:创建后可以放入Application域对象中。

4.使用IOC容器来管理Struts2的Action

①在Spring的IOC容器中配置Struts2的Action


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

	<!-- 配置 Spring 配置文件的名称和位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 启动 IOC 容器的 ServletContextListener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 配置 Struts2 的 Filter -->
	<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-app>

application.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.xsd">

	<bean id="person" 
		class="com.atguigu.spring.struts2.beans.Person">
		<property name="username" value="spring"></property>	
	</bean>
	
	<bean id="personService"
		class="com.atguigu.spring.struts2.services.PersonService"></bean>
	
	<!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype :代表非单例 -->
	<bean id="personAction" 
		class="com.atguigu.spring.struts2.actions.PersonAction"
		scope="prototype">
		<property name="personService" ref="personService"></property>	
	</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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
		
		<!--  
			Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id
		-->
		<action name="person-save" class="personAction">
			<result>/success.jsp</result>
		</action>
		
    </package>

</struts>

Action类


public class PersonAction {
	
	private PersonService personService;
	
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	
	public String execute(){
		System.out.println("execute....");
		personService.save();
		return "success";
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值