温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring整合Struts2的两种方法小结

发布时间:2020-09-10 19:07:04 来源:脚本之家 阅读:81 作者:jingxian 栏目:编程语言

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>

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI