Spring整合Struts2(Lee-Day12-9)

(1)把Struts2的核心控制器配置到Web.xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>Day1103_OA</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.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>
	
</web-app>

(2)配置Struts2.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="自定义包名" namespace="/" extends="struts-default">
        <action name="*">
            <!--<result type="redirectAction"></result>-->
            <result>/WEB-INF/content/{0}.jsp</result>
        </action>
    </package>
    <include file="example.xml"/>
</struts>

(3)配置Web应用在启动时自动创建Spring容器:

//在讲Spring的时候,创建Spring容器使用这个API,形象上把它当成Map处理,会预初始化容器中的所有的singletonBean

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

用ContextLoaderListener,所实现的接口中的contextInitialized(ServletContextEvent sec)方法,在该方法中 new XxxApplicationContext("beans.xml");

监听器的机制是(因为ContextLoaderListener继承了ContextLoader,实现了ServletContextListener接口)在Web应用启动和关闭时会调用ServletContextListener接口的void contextDestroved(ServletContextEvent sec) 和 void contextInitialized(ServletContextEvent sec)方法

所以监听器在 contextInitialized(ServletContextEvent sec)方法中 调用了new XxxApplicationContext("beans.xml");

创建容器对象。

默认加载WEB-INF/applicationContext.xml作为配置文件,为了让new ClassPathXmlApplicationContext("applicationContext.xml");配置指定位置的xml文件,在Web.xml文件下配置如下内容

<param-value>标签里的位置文件可以配置多个,用逗号隔开即可

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
</context-param>

(4)为第三部提供配置文件

(5)导入Struts2-Spring整合包该插件包可完成

1.将action交给spring管理

2.用byName的自动装配策略(调用set方法)为action注入Service

-----------------------------------------------以上,是安装步骤,只要做一次-------------------------------------------------

开发过程:

(1)Action不再主动获取Service组件

(2)Action为Service提供的setter方法务必与Service的配置id要对应,因为是byName

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值