gpt4 book ai didi

Spring整合Struts2的两种方法小结

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Spring整合Struts2的两种方法小结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

 
?
1
 
2
3
4
5
<!-- 使用ContextLoaderListener初始化Spring容器 -->
  < listener >
   < listener-class >org.springframework.web.context.ContextLoaderListener
   </ listener-class >
  </ listener >

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

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? 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实例,则可以根据 。

如下获取:

 
?
1
 
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 。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<? 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 。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? 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 。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<? 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 。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? 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 。

 
?
1
 
2
3
4
5
6
7
8
9
<? 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 。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<? 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 >

以上这篇Spring整合Struts2的两种方法小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

最后此篇关于Spring整合Struts2的两种方法小结的文章就讲到这里了,如果你想了解更多关于Spring整合Struts2的两种方法小结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com