0
点赞
收藏
分享

微信扫一扫

Spring集成Web环境 & SpringMVC组件

晚安大世界 2022-03-11 阅读 70

文章目录

Spring集成Web环境 & SpringMVC组件

一、Spring集成Web环境(解耦)

首先在web.xml配置全局参数(spring配置文件名称)和spring监听器(用于服务开始的时候加载springxml文件)

通过这个方法来减少spring配置文件的加载次数,从而提高程序的加载速度

   <!--web.xml-->
   <!--全局参数:配置spring的xml配置文件名字-->
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicatonContext.xml</param-value>
  </context-param>

  <!--Spring监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
  </listener>
package com.kang.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取ServletContext对象,用于读取全局参数
        ServletContext servletContext = request.getServletContext();
		//获取监听器返回的WebApplicationContext对象
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        UserService userService = app.getBean(UserService.class);

        userService.save();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

二、SpringMVC快速入门


1.、开发步骤

<!--spring-mvc的配置文件(该注释要删)-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!--包扫描-->
    <context:component-scan base-package="com.kang.controller"/>

</beans>

访问

http://localhost:8080/SpringMVC/quick

就可以看见控制台输出

quickMethod running.......

在这里插入图片描述

在这里插入图片描述


三、SpringMVC的执行流程

在这里插入图片描述


四、SpringMVC组件解析

  1. 前端控制器:DispatcherServlet

    用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由 它调用其它组件处理用户的请求,DispatcherServlet 的存在降低了组件之间的耦合性。

  2. 处理器映射器:HandlerMapping

    HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC 提供了不同的映射器实现不同的 映射方式,例如:配置文件方式,实现接口方式,注解方式等。

  3. 处理器适配器:HandlerAdapter

通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理 器进行执行

  1. 处理器:Handler

它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由 Handler 对具体的用户请求进行处理。

  1. 视图解析器:View Resolver

View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名,即 具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。

  1. 视图:View

    SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最 常用的视图就是 jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程 序员根据业务需求开发具体的页面


五、SpringMVC注解解析


六、SpringMVC组件扫描的扩展


七、SpringMVC的XML配置解析——视图解析器

    <!--配置内部资源视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/jsp/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
举报

相关推荐

0 条评论