springboot-配置springmvc


1 springboot對springmvc的默認配置

  • 視圖解析器:Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
  • 支持靜態資源文件夾的路徑,以及webjars: Support for serving static resources, including support for WebJars
  • 轉換器,通用轉換器(這就是我們網頁提交數據到后台自動封裝成為對象的東西,比如把"1"字符串自動轉換為int類型),格式化器(比如頁面給我們了一個2019-8-10,它會給我們自動格式化為Date對象):Automatic registration of Converter, GenericConverter, and Formatter beans.
  • Http信息轉換器(SpringMVC用來轉換Http請求和響應的的,比如我們要把一個User對象轉換為JSON字符串):Support for HttpMessageConverters
  • 定義錯誤代碼生成規則:Automatic registration of MessageCodesResolver
  • 首頁定制:Static index.html support.
  • 圖標定制:Custom Favicon support
  • 初始化數據綁定器(幫我們把請求數據綁定到JavaBean中):Automatic use of a ConfigurableWebBindingInitializer bean

2 自定義mvc配置

  • 如果您想保留 springboot 默認的 mvc 配置 並增加一些自定義的 mvc 配置(攔截器,格式化程序,視圖控制器和其他功能),您可以編寫一個自己的 WebMvcConfigurer 類並添加 @Configuration 注解,但不添加 @EnableWebMvc注解:If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.
  • 如果您想提供 RequestMappingHandlerMapping,RequestMappingHandlerAdapter 或 ExceptionHandlerExceptionResolver 的自定義實例,並且仍然保留 springboot 默認的 mvc 配置,您可以聲明一個 WebMvcRegistrations 類型的 bean 並使用它來提供這些組件的自定義實例:If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.
  • 如果你想完全接管 springmvc(springboot對springmvc的自動配置全部失效,所有配置都是我們自己去編寫),您可以編寫一個自己的 WebMvcConfigurer 類並添加 @Configuration 注解和 @EnableWebMvc注解:If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc

3 測試添加一個自定義視圖解析器

ContentNegotiatingViewResolver 這個視圖解析器就是用來組合所有的視圖解析器的,如果我們給容器中去添加一個視圖解析器,這個類就會幫我們自動的將它組合進來

3.1 編寫一個mvc配置文件

package com.lv.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//擴展springmvc dispatchServlet
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //如果,你想自定義一些定制化功能,只要寫這個組件,然后將它交給springboot,springboot就會幫我們自動裝配
    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }
    //ViewResolver:實現了視圖解析器接口的類,我們就可以把它看作視圖解析器
    //自定義了一個自己的視圖解析器MyViewResolver
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception{
            return null;
        }
    }
}

3.2 debug斷點測試

我們給 DispatcherServlet類 中的 doDispatch方法加個斷點進行調試一下,因為所有的請求都會走到這個方法中,然后啟動項目,隨便訪問一個頁面,查看一下Debug信息

這個就是我們自定義的視圖解析器,說明已經生效了

4 日期格式化

4.1springboot默認配置的日期格式

4.2 我們可以在spingboot配置文件自定義格式來覆蓋默認的

application.properties

spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss

5 擴展springmvc方式

以下使用添加視圖跳轉演示

5.1在src/main/resources/templates新建一個html頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>我自己定義的,嘿嘿嘿</h1>
</body>
</html>

5.2 導入thymeleaf依賴

<!--thymeleaf模板引擎-->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

5.2 編寫一個MyMvcConfig.java

package com.lv.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//如果我們要擴展springmvc,官方建議我們這樣去做
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //視圖跳轉
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/lv").setViewName("test");
    }
}

5.3 啟動程序測試

跳轉成功,結論:我們要擴展SpringMVC,官方就推薦我們使用這種方式,既保SpringBoot留所有的自動配置,也能用我們擴展的配置

6 全面接管springmvc方式

springboot對springmvc的默認配置全部失效,只生效我們自定義的配置,只需要在配置類中加入@EnableWebMvc注解即可,不推薦這種方式,下面測試一下

6.1 在src/main/resources/static 目錄下新建一個 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>我自己寫的首頁,嘿嘿嘿</h1>
</body>
</html>

這是springboot默認定制首頁的方式,會自動在src/main/resources/static目錄下尋找index.html作為首頁,這個行為就代表了springboot對springmvc的默認配置

6.2 啟動項目訪問首頁

訪問成功,說明目前springboot對springmvc的默認配置是生效的

6.3 在MyMvcConfig.java中加入@EnableWebMvc注解

package com.lv.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/lv").setViewName("test");
    }
}

6.4 重啟程序,再次訪問首頁

首頁訪問不到了,說明springboot對springmvc的默認配置失效了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM