Spring4---整合Struts2

Web中使用Spring基本思路

  1. jar包,需要添加额外的jar包

    • spring-web-RELEASE.jar
    • spring-webmvc-.RELEASE.jar
  2. Spring配置文件,没什么不同

  3. 创建IOC容器的方式不同

    • 非web引用在main方法中直接创建
    • 在web应用应该在服务器加载时就创建IOC容器.,在ServiceContextListener中的contextInitialized()方法中创建IOC容器.
  4. 在Web应用中其他组件中如何来访问IOC容器?

    • 在创建IOC容器之后,可以将IOC容器设置到ServletContext属性之中.
  5. 将Spring的配置文件名称定义在web.xml文件中.

测试

  1. 创建一个普通的Bean,这个Bean由IOC容器管理
package mao.shu.bean;

public class Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public void setHello(){
        System.out.println("Hello "+ this.name);
    }
    
}

  • 编写Spring配置文件,将Spring配置文件放在ClassPath路径下,就是src目录下,文件名称为:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person" class="mao.shu.bean.Person">
        <property name="name" value="----你好"/>
    </bean>
</beans>

  • 编写web.xml文件
    • 使用初始化属性配置Spring的配置文件名称
    • 定义监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!--设置Spring配置文件的名称-->
    <context-param>
        <param-name>configLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>
    <!--struts2核心处理类的路径-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    <!--配置监听器-->
    <listener>
        <listener-class>mao.shu.listener.SpringContextListener</listener-class>
    </listener>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
  • 编写监听器
    • 在web容器初始化的时候,创建IOC容器,并将IOC容器存储到ServletContext属性之中
package mao.shu.listener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SpringContextListener implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("*************监听器开始初始化****************");
        //获取Spring配置文件名称
        ServletContext servletContext = sce.getServletContext();
        String springConfigName = servletContext.getInitParameter("configLocation");
        //获取IOC容器
        ApplicationContext app = new ClassPathXmlApplicationContext(springConfigName);
        //将IOC容器设置到ServletContext中,也就是application内置对象
        servletContext.setAttribute("ioc",app);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
  • 编写一个Servlet程序,从servlet中得到IOC容器的实例,使用注解的方式,配置Servlet的访问路径
package mao.shu.servlet;


import mao.shu.bean.Person;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = super.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("ioc");

        //获取Person对象
        Person vo = app.getBean("person",Person.class);
        vo.setHello();
    }
}

在这里插入图片描述

在Web中使用Spring

  • Spring中提供了一个监听器,用于创建IOC容器,只需要将这个监听器配置到web.xml文件中即可.
<!--配置Spring提供的监听器,启动IOC容器-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  • 同时需要在web.xml文件中配置Spring配置文件的名称和所在的路径,
    • classpath:表示跟路径
    • contextConfigLocation:的名称最好不要修改
<!--设置Spring配置文件的名称-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
  • 如果要获取IOC容器的实例,可以使用WebApplicationContextUtils类中的方法获取

  • 定义jsp页面,获取IOC容器

<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="mao.shu.bean.Person" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
      //获取IOC容器
      ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(application);

//      获取IOC容器中的Bean
      Person person = app.getBean("person",Person.class);
   		person.setHello();
    %>

  </body>
</html>

  • 测试访问jsp

  • 后台输出

在这里插入图片描述

Spring如何整合Struts2?

整合目标

  1. 使用IOC容器来管理Struts2的Action

如何整合Struts2?

  1. 加入struts2的jar包
  2. 配置web.xml文件配置Struts2的Filter

在这里插入图片描述
3. 加入Struts的配置文件

  • 在IOC容器中使用<bean>标签配置Struts2的Action类
    • 在配置Struts2中的Action时需要配置scope属性,其值必须为prototype,因为Action不是单例的.每一个用户请求都会创建一个Action

在这里插入图片描述
4. 配置Struts2的配置文件
- Spring整合Struts2时,在Struts2中配置的Spring的Action节点的class需要指向IOC容器中的id

在这里插入图片描述
5. Spring于Struts2整合的时候,需要一个struts-spring-plugin.jar包

  • 整合原理:通过添加struts2-spring-plugin.jar包之后Struts会先从IOC容器中获取Action的实例.如果IOC容器中没有,则会通过反射创建

  • 项目的jar包

  • struts2jar包

在这里插入图片描述

  • spring

在这里插入图片描述

  • web.xml文件配置,不同版本的struts2配置的核心处理类路径可能不一样,可以在Struts2开发包中参考示例文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!--设置Spring配置文件的名称-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--struts2核心处理类的路径-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
   
    <!--配置Spring提供的监听器,启动IOC容器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
  • spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="person" class="mao.shu.bean.Person">
        <property name="name" value="----你好"/>
    </bean>

    
    <bean id="personService" class="mao.shu.service.PersonService"/>

    <!--配置Struts2的Action类-->
    <bean id="personAction" class="mao.shu.action.PersonAction" scope="prototype">
        <property name="personService" ref="personService"/>
    </bean>
</beans>
  • struts2.xml文件配置
    • 在Struts2的配置文件中配置Action的class值写的是在Spring的配置文件中定义的Bean的id值.
    • 这需要导入struts-spring-plugin.jar包的支持
    • 使用struts-spring-plugin.jar包之后,会根据传入的id值在IOC容器中寻找有没有对应的实例,如果没有则在通过反射创建.
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!--是否开启动态业务方法-->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!--开启开发者模式,在网页中显示错误-->
    <constant name="struts.devMode" value="true" />

    <!--配置Action-->
    <package name="root" namespace="/" extends="struts-default">
        <!--action中的 class属性中的内容是IOC容器中Bean的id值-->
        <action name="PersonAction" class="personAction">
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值