Spring整合Struts2的两种方式

Spring提供了一个ContextLoaderListener,该监听类实现了ServletContextListener接口。该类可以作为Listener使用,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件,因此如果只有一个配置文件且配置文件命名为applicationContext.xml,则只需在web.xml文件中增加如下配置片段:

<!-- 使用ContextLoaderListener初始化Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
                            

如果有多个配置文件需要载入,则考虑使用<context-param.../>元素确定配置文件的文件名。,COntextLoaderListener加载时,会查找名为contextConfigLocation的初始化参数,因此配置<context-param.../>时应指定参数名为contextConfigLocation。

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<context-param>
	   <param-name> contectCOnfigLocation </param-name>
	   	<param-value>/WEB-INF/daocontext.xml,/WEB-INF/applicationCotext.xml
		</param-value>
	</context-param>
	<!-- 使用ContextLoaderListener初始化Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>	
</web-app>

Spring根据配置文件创建WebApplicationContext对象,并将其保存在Web应用的ServletContext中。如果要获取应用中的ApplicationContext实例,则可以根据
如下获取:

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext)

让Spring管理控制器

当Struts2将请求转发给指定的Action时,Struts2中的该Action只是一个傀儡,他只是一个代号,并没有指定实际的实现类,当然也不可能创建Action实例,二隐藏在该action下的是Spring容器中的Action实例,他才是真正处理用户请求的控制器。

其中Struts2只是一个伪控制器,这个伪控制器的功能实际由Spring容器中的控制器来完成,这就实现了让核心控制器调用Spring容器中的action来处理用户请求。在这种策略下,处理用户请求的Action由Spring插件负责创建,但Spring插件创建Action实例时。并不是利用配置Action时指定的class属性来创建该action实例,而是从Spring容器中取出对应的Bean实例完成创建。
web.xml

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<!-- 使用ContextLoaderListener初始化Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 定义Struts 2的FilterDispathcer的Filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

applicationcontext.xml

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!-- 定义一个业务逻辑组件,实现类为MyServiceImp -->
	<bean id="myService" 
		class="com.bh.service.impl.MyServiceImpl"/>
	<!-- 让Spring管理的Action实例,因为每个action里包含请求的状态信息,所以必须配置scope不能为单例 -->
	<bean id="loginAction" class="com.bh.action.LoginAction"
		scope="prototype">
		<!-- 依赖注入业务逻辑组件 -->
		<property name="ms" ref="myService"/>
	</bean>
</beans>

struts.xml

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
	<!-- 配置了系列常量 -->
	<constant name="struts.i18n.encoding" value="GBK"/>	
	<constant name="struts.devMode" value="true"/>	
	<package name="lee" extends="struts-default">
		<!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
			, 而是Spring容器中的Bean实例-->
		<action name="loginPro" class="loginAction">
			<!-- 为两个逻辑视图配置视图页面 -->
			<result name="error">/WEB-INF/content/error.jsp</result>
			<result name="success">/WEB-INF/content/welcome.jsp</result>
		</action>
		<!-- 让用户直接访问该应用时列出所有视图页面 -->
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>
	</package>
</struts>

使用自动装配

通过设置struts.objectFactory.spring.autoWire常量可以改变Spring插件额自动装配策略,该常量可以接受如下几个值:

  • Name:根据属性名自动装配。Spring插件会查找容器中全部Bean,找到其中id属性与Action所需的业务逻辑组件同名的Bean,将该bean实例注入到Action实例。
  • Type:根据属性类型自动装配。Spring插件会查找容器中全部Bean,找出其类型恰好与Action所需的业务逻辑组件相同的Bean,将该Bean实例注入到Action实例。
  • Auto:Spring插件会自动检测需要使用哪种自动装配方式。
  • Constructor:与type类似,区别是constructor使用构造器来构造注入的所需参数而不是使用设值注入方式。

web.xml

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
		<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
		<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>

applicationcontext.xml

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!-- 定义一个业务逻辑组件,实现类为MyServiceImp -->
	<bean id="ms" class="com.bh.service.impl.MyServiceImpl"/>
</beans>

struts.xml

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
	<!-- 配置了系列常量 -->
	<constant name="struts.i18n.encoding" value="GBK"/>	
	<constant name="struts.devMode" value="true"/>
	<package name="lee" extends="struts-default">
		<!-- 定义处理用户请求的Action -->
		<action name="loginPro"
			class="com.bh.action.LoginAction">
			<!-- 为两个逻辑视图配置视图页面 -->
			<result name="error">/WEB-INF/content/error.jsp</result>
			<result name="success">/WEB-INF/content/welcome.jsp</result>
		</action>
		<!-- 让用户直接访问该应用时列出所有视图页面 -->
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>
	</package>
</struts>

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值