springboot-web开发初级

229 阅读4分钟

使用springboot进行web开发,我们需要做的事情

  1. 导入静态资源
  2. 首页
  3. jsp,模板引擎Thymeleaf
  4. 装配扩展SpringMVC
  5. 增删改查
  6. 拦截器

静态资源(三个路径)

SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration这个配置类里面,我们直接去看他的源码,分析这三个路径

image.png image.png

这时候,我们再去WebPropertier.class(第一张图那里)中看

image.png

小结:三种加载静态资源的路径

  1. 通过webjars:需要导入webjar的依赖
  2. 通过默认加载:/**
  3. 通过classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/,即静态资源可以放在

image.png

4. 通过3加载路径有优先级:resourcs>static>public 5. 我们可以在application.properties通过spring.mvc.static-path-pattern= /hello/,classpath:/wu/ 来自定义修改默认的地址,修改后,原来的自动装配就会失效 6. classpath:/META-INF/resources/webjars/ 映射到 localhost:8080/webjars/ 7. "classpath:/resources/", "classpath:/static/", "classpath:/public/,"classpath:/**"映射到 localhost:8080/

首页

同样的,我们还是看WebMvcAutoConfiguration的源码,继续往下看

image.png

image.png

小结:静态资源文件夹下的index.html/**映射,在static文件夹下新建一个index.html,首页的内容就会被改变 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
我是首页
</body>
</html>

image.png

模板引擎Thymeleaf

为什么要使用模板引擎?

前端传给我们的HTML页面,我们通常转成jsp页面,方便传输数据。但是SpringBoot这个项目首先是以jar的方式,不是war;像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。 这时候 ,模板引擎就出现了。

Thymeleaf的使用前提 引入Thymeleaf的依赖

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Thymeleaf的源码分析 image.png 我们可以在其中看到默认的前缀和后缀!

结论:

  1. 我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。
  2. 使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

Thymeleaf的使用

  1. templates里面的测试页test.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>
</body>
</html>

image.png 2. 测试类ThymeleafController.java

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ThymeleafController {
    @RequestMapping("/t1")
    public String test(Model model){
        model.addAttribute("msg","hello,Thymeleaf");
        return "test";
    }
}

  1. 启动服务,运行测试

image.png

装配扩展SpringMVC

SpringBoot对SpringMVC做了很多的配置,我们需要去了解一下,都做了哪些配置,对于这些配置,我们要怎么去定制,怎么去扩展

这时候就要用到官方文档了:装配扩展SpringMVC官方文档

我们在官方文档可以找到SpringMVC的自动配置

image.png

我们以ContentNegotiatingViewResolver(内容协商视图解析器)为例,进行源码分析:

我们先找到WebMvcAutoConfiguration,然后搜索ContentNegotiatingViewResolver

image.png

我们点击ContentNegotiatingViewResolver进去继续查看它是怎么定位视图的

image.png

接下来,我们继续点击getCandidateViews查看怎么获得候选视图的

image.png

所以说,ContentNegotiatingViewResolver组合了所有的视图解析器,我们也可以根据它,自己定义一个视图解析器。

自定义视图解析器

  1. 在主程序编写自定义的视图解析器
package com.example.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;

@Configuration//添加@Configuration注解来标注它是一个配置类
public class MyMVCConfig implements WebMvcConfigurer {

    //ViewResolver 实现了所有视图解析器接口的类
    @Bean
    public ViewResolver MyViewResolver(){
        return new MyViewResolver();
    }

    //自定义视图解析器
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}

  1. DispatcherServlet中的doDispatch方法打断点通过debug来判断我们自定义的视图解析器有没有起作用

image.png

  1. 结果:发现我们的视图解析器有被扫描加载

image.png

结论

通过对自定义视图解析器的源码分析,我们再返回到官网

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.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

官网也很明确的说了,我们要修改SpringBoot的默认配置,只需要:编写@Configuration注解类,而且这个类要继承WebMvcConfigurer

为什么不能加@EnableWebMvc注解呢? 原因在官网也给了出来

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

@EnableWebMvc:全面接管,即SpringBoot对SpringMVC的自动配置失效了,所有的自动配置都需要我们自己配置。

推荐一下更详细的讲解博客:@EnableWebMvc说明