struts2+spring+mybatis入门教程三之配置spring2

本文详细介绍了如何将Spring相关jar包与Struts2整合,通过配置web.xml、spring.xml文件以及修改struts.xml,实现了Web应用的开发流程。通过创建DemoHelper和DemoAction类,并利用注解完成自动注入,最终成功部署并运行Web应用。

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

(1)将spring相关jar包及struts2-spring-plugin-2.0.11.2.jar拷贝到web-inf/lib下,在web.xml中设置spring监听器、配置文件路径。web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>SSM</display-name>

	<welcome-file-list>
		<welcome-file>/demo/demo.jsp</welcome-file>
	</welcome-file-list>

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

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring.xml</param-value>
	</context-param>

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

</web-app>

(2)在web-inf根目录下新建文件spring.xml,配置要扫描的包路径,及需要注册的实体bean。spring.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-3.0.xsd
    http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 	<!-- 采用注释的方式配置bean -->
	<context:annotation-config />

 	 <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

	<!-- 配置要扫描的包 -->
	<context:component-scan base-package="com.xy6"></context:component-scan>

	<bean id="demoAction" class="com.xy6.DemoAction" />

</beans>

(3)修改struts.xml,将com.xy6.DemoAction替换为demoAction。struts.xml代码:

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

	<constant name="struts.objectFactory" value="spring"/>

	<package name="demo" extends="struts-default">
		<action name="demo" class="demoAction" method="execute">
			<result name="success">/demo/demo.jsp</result>
		</action>
	</package>

</struts>

(4)在包com.xy6下新建类DemoHelper.java,以测试spring的自动注入功能,由于该类使用@Component标记,则无需在spring.xml中注册该实体bean。DemoHelper.java代码:

package com.xy6;

import org.springframework.stereotype.Component;

@Component
public class DemoHelper {

	public String getCurYear(){
		return "2014";
	}
}

(6)在DemoAction类中调用DemoHelper类的getCurYear()方法。DemoAction.java代码:

package com.xy6;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

public class DemoAction extends ActionSupport {

	public static final long serialVersionUID = 1L;

	@Autowired
	private DemoHelper demoHelper = new DemoHelper();

	public String execute(){
		System.out.println("---curren year:"+demoHelper.getCurYear());
		return "success";
	}
}

(7)重启weblogic域服务,浏览器中访问http://localhost:9001/web/demo.action,页面显示demo.jsp,控制台输出2014。如下图:

(8)至此,spring配置结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值