目标
vWeb应用中初始化Spring容器
v整合Struts1.x
vSpring提供的Web工具类
v整合Struts2
初始化WEBAPPLICATIONCONTEXT
在Web应用中,需要从WebApplicationContext中获取Spring管理的bean,所以需要对它进行初始化,Spring提供了两种方式:
ContextLoaderListener
ContextLoaderServlet
在web.xml中的配置:
WEBAPPLICATIONTEXTUTILS类
使用Spring提供WebApplicationContextUtils类的静态方法可以获得WebApplicationContext,然后就可以从应用上下文中获取所需要的Bean了。
SPRING整合STRUTS1.X
在应用的类路径下添加spring-webmvc-struts.jar
¡Spring2.5对Struts 1.x的支持被独立成spring-webmvc-struts.jar了。
两种集成Struts的方式
¡继承 Spring的ActionSupport 类
¡委托Spring配置的动作 推荐
继承SPRING的ACTIONSUPPORT类
继承org.springframework.web.struts包下的
ActionSupport、
DsipatchActionSupport、
LookupDispatchActionSupport、
MappingDispatchActionSupport 之一:
使用请求委托
在struts配置文件中配置使用Spring的请求处理器:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
如果使用tiles,则要用DelegatingTilesRequestProcessor类。
这样,Struts就会使用Spring的DelegatingRequestProcessor来作为请求处理器了。来自Struts的ActionServlet分发的请求处理将发送到Spring 应用上下文环境中配置的Struts Action类。
配置
若struts-config.xml文件中action是如下声明
则在spring的web配置文件中进行如下配置:
其他工具
Spring为Web应用提供了两个实用类
OpenSessionInViewFilter
CharacterEncodingFilter