Spring框架之Spring与WEB

1>不使用监听器创建WEB

  • 1、新建Maven项目
    在这里插入图片描述

  • 2、将 spring-mybatis 项目中以下内容复制到当前项目中:

    • (1)Service 层、Dao 层全部代码
    • (2)配置文件 applicationContext.xml 及 jdbc.properties,mybatis.xml
    • (3)pom.xml
    • (4)加入 servlet ,jsp 依赖
  • 在之前原有的 pom.xml 文件中再加入以下的内容:

<!-- servlet依赖 -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖 -->
<dependency>
	<groupId>javax.servlet.jsp</groupId>
	<artifactId>jsp-api</artifactId>
	<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
  • 3、定义 index 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
index.jsp<br>
<form action="regservlet" method="post">
    姓名:<input type="text" name="name"><br>
    年龄:<input type="text" name="age"><br>
    <input type="submit" name="submit"><br>
</form>
</body>
</html>
  • 4、创建Servlet接口并配置web.xml文件
package com.bjpowernode.controller;

import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService service = (StudentService) ac.getBean("studentService");
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        service.addStudent(student);
        service.findAllStudent().forEach(stu-> System.out.println(stu));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

  • web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>RegisterServlet</servlet-name>
    <servlet-class>com.bjpowernode.controller.RegisterServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RegisterServlet</servlet-name>
    <url-pattern>/regservlet</url-pattern>
  </servlet-mapping>

</web-app>

  • 启动服务器在页面中输入参数
    在这里插入图片描述
  • 成功运行

2>使用监听器创建WEB

  • 对于不使用监听器创建WEB来说,每次通过Post请求到达Http服务器时,都会创建一个新的Spring容器,我们知道浏览器每天有大量请求到Http服务器中,所以把容器的创建放在doGet()、doPost()方法中不好
  • 此时,可以考虑,将 Spring 容器的创建放在 Servlet 进行初始化时进行,即执行 init()方
    法时执行。并且,Servlet 还是单例多线程的,即一个业务只有一个 Servlet 实例,所有执行
    该业务的用户执行的都是这一个 Servlet 实例。这样,Spring 容器就具有了唯一性了。
    但是,Servlet 是一个业务一个 Servlet 实例,即 LoginServlet 只有一个,但还会有
    StudentServlet、TeacherServlet 等。每个业务都会有一个 Servlet,都会执行自己的 init()方法,也就都会创建一个 Spring 容器了。这样一来,Spring 容器就又不唯一了。

2.1、 使用Spring监听器ContextLoaderListener(掌握)

  • 对于 Web 应用来说,ServletContext 对象是唯一的,一个 Web 应用,只有一个
    ServletContext 对象,该对象是在 Web 应用装载时初始化的。若将 Spring 容器的创建时机,放在 ServletContext 初始化时,就可以保证 Spring 容器的创建只会执行一次,也就保证了Spring 容器在整个应用中的唯一性。
  • 当 Spring 容器创建好后,在整个应用的生命周期过程中,Spring 容器应该是随时可以被 访问的。即,Spring容器应具有全局性。而放入ServletContext对象的属性,就具有应用的 全局性。所以,将创建好的 Spring容器,以属性的形式放入到ServletContext的空间中,就保证了 Spring 容器的全局性。
  • 上述的这些工作,已经被封装在了如下的 Spring 的 Jar 包的相关 API 中: spring-web-5.2.5.RELEASE

2.2、具体步骤

  • 1、maven依赖pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
  • 2、注册监听器ContextLoaderListener
    • 若要在ServletContext初始化时创建Spring容器,就需要使用监听器接口ServletContextListener对ServletContext进行监听。在 web.xml 中注册该监听器。
      在这里插入图片描述
  • Spring 为该监听器接口定义了一个实现类ContextLoaderListener,完成了两个很重要的
    工作:创建容器对象,并将容器对象放入到了ServletContext的空间中
  • 3、指定 Spring 配置文件的位置<context-param>
    • ContextLoaderListener 在对 Spring 容器进行创建时,需要加载 Spring 配置文件。其默认的 Spring 配置文件位置与名称为:WEB-INF/applicationContext.xml。但,一般会将该配置文件放置于项目的 classpath 下,即 src 下,所以需要在 web.xml 中对 Spring 配置文件的位置及名称进行指定。
      在这里插入图片描述
  • 从监听器ContextLoaderListener的父类ContextLoader的源码中可以看到其要读取的配
    置文件位置参数名称contextConfigLocation。

在这里插入图片描述

  • 4、获取Spring容器对象
  • 1)直接从ServletContext中获取
    • 从对监听器 ContextLoaderListener 的源码分析可知,容器对象在 ServletContext 的中存放的key为 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。所以,可以直接通过 ServletContext 的 getAttribute()方法,按照指定的 key 将容器对象获取到。
      在这里插入图片描述
  • 2)通过WebApplicationContextUtils获取
    • 工具类 WebApplicationContextUtils 有一个方法专门用于从 ServletContext 中获取 Spring容器对象:getRequiredWebApplicationContext(ServletContext sc)
    • 调用Spring提供的方法获取容器对象:
      在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值