Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 162|回复: 0

[默认分类] Struts2学习笔记——Struts2与Spring整合

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-22 13:12:23 | 显示全部楼层 |阅读模式
    Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的bean。
      
    1、导入依赖包
    除了导入Struts2和Spring的核心库之外,还要导入commons-logging和struts2-spring-plugin包,否则启动会出异常
      
    2、web.xml的配置
    既然有Struts2,核心拦截器的配置是不可少的

      
    1. <filter>
    2.      <filter-name>struts2</filter-name>
    3.      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    4. </filter>
    5. <filter-mapping>
    6.      <filter-name>struts2</filter-name>
    7.      <url-pattern>/*</url-pattern>
    8. </filter-mapping>
    复制代码

      

      
    通过配置ContextLoaderListener监听器,使容器启动时,自动加载applicationContext配置,
    因为它实现了ServletContextListener这个接口,容器启动时会自动执行它实现的方法。

      
    1. <listener>
    2.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    3. </listener>
    复制代码

      

      
    默认情况下,会加载WEB-INF/applicationContext.xml这个文件,我们可以通过配置contextConfigLocation参数改变配置文件的路径
      
      

      
    1. <context-param>
    2.      <param-name>contextConfigLocation</param-name>
    3.      <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    4. </context-param>
    复制代码

      

    以上配置均在web.xml文件的<web-app></web-app>区域  
      
    3、测试类
    在浏览器请求一个Action方法,在Action方法内向一个对象请求一个List,然后转到index.jsp页面,在页面中输出Action请求到的List。
    通过Spring依赖配置,控制Action请求的对象。
    首先要编写一个接口,Action方法依赖这个接口,通过调用接口中的方法获取List

      
    1. public interface IocTestInterface {
    2.      public List getList();
    3. }
    复制代码

      

    下面编写Action类,这个类继承ActionSupport类,并覆盖其中的execute方法,
    execute方法执行时,调用实现了上述接口对象的getList方法获取List

      
    1. public class IocAction extends ActionSupport {
    2.         private IocTestInterface iti;
    3.         private List list;
    4.        
    5.         public List getList() {
    6.                 return list;
    7.         }
    8.         public void setList(List list) {
    9.                 this.list = list;
    10.         }
    11.         public IocTestInterface getIti() {
    12.                 return iti;
    13.         }
    14.         public void setIti(IocTestInterface iti) {
    15.                 this.iti = iti;
    16.         }
    17.        
    18.         public String execute() throws Exception {
    19.                 this.setList(iti.getList());
    20.                 return super.execute();
    21.         }
    22. }
    复制代码

      

    编写用来显示运行结果的jsp文件
    遍历list,并将每个元素作为一行来显示

      
    1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    2. <%@ taglib prefix="s" uri="/struts-tags" %>
    3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    4. <html>
    5.   
    6.   <body>
    7.     This is my JSP page. <br><br>
    8.    
    9.     <s:iterator value="list" id="current">
    10.             <li><s:property value="current"/></li>
    11.     </s:iterator>
    12.    
    13.   </body>
    14. </html>
    复制代码

      

    系统的结构就是这样。下面编写两个实现IocTestInterface接口的类,用来提供数据

      
    1. public class IocTestImpl implements IocTestInterface {
    2.         public List getList() {
    3.                 List l = new ArrayList();
    4.                 l.add("abc");
    5.                 l.add("def");
    6.                 l.add("hig");
    7.                 return l;
    8.         }
    9. }
    复制代码

      

      

      
    1. public class IocTest2Impl implements IocTestInterface {
    2.         public List getList() {
    3.                 List l = new ArrayList();
    4.                 l.add("123");
    5.                 l.add("456");
    6.                 l.add("789");
    7.                 return l;
    8.         }
    9. }
    复制代码

      

    4、编写applicationContext.xml配置依赖关系

      
    1. <beans xmlns ="http://www.springframework.org/schema/beans"
    2.     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
    3.     xsi:schemaLocation ="http://www.springframework.org/schema/beans
    4.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    5.    
    6.     <bean name="IocAction" class="sy.struts2.ioc.IocAction">
    7.             <property name="iti">
    8.                     <bean class="sy.struts2.ioc.IocTestImpl"></bean>
    9.             </property>
    10.     </bean>
    11.    
    12. </beans>
    复制代码

      

    文件配置了id为IocAction的bean,路径为刚刚编写的Action类的路径,其中iti对象(请求数据的对象)配置为IocTestImpl(这里使用了匿名bean)
      
    5、编写struts.xml配置文件
    首先要告知Struts 2运行时使用Spring来创建对象
    在<struts></struts>区域加入以下配置

      
    1. <constant name="struts.objectFactory" value="spring" />
    复制代码

      

    创建package并配置Action

      
    1. <package name="hs" extends="struts-default">
    2.      <action name="ioc" class="IocAction">
    3.           <result>/index.jsp</result>
    4.      </action>
    5. </package>
    复制代码

      

      
    6、发布并运行
    发布后启动Tomcat,用浏览器打开地址http://localhost:8080/StrutsIoc/ioc.action,获得了下面的页面


    修改spring配置文件applicationContext.xml中配置

      
    1. <bean name="IocAction" class="sy.struts2.ioc.IocAction">
    2.      <property name="iti">
    3.           <bean class="sy.struts2.ioc.IocTest2Impl"></bean>
    4.      </property>
    5. </bean>
    复制代码

      

    只是将注入到IocAction中的IocTestImpl修改为IocTest2Impl,也就是使用了另一个实现了IocTestInterface接口的类
    重启服务器,再次打开刚才的地址

      

    这也就是spring的“控制反转”
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-17 02:13 , Processed in 0.469285 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表