4、SpringBoot2之整合SpringMVC

发布时间 2023-12-25 08:19:08作者: Javaer1995

创建名为springboot_springmvc的新module,过程参考3.1节

4.1、重要的配置参数

在 spring boot 中,提供了许多和 web 相关的配置参数(详见官方文档),其中有三个比较重要:

4.1.1、server.port

该配置参数用于设置 web 应用程序的服务端口号,默认值为 8080

4.1.2、server.servlet.context-path

该配置参数用于设置 web 应用程序的上下文路径,默认值为空

4.1.3、spring.resources.static-locations

该配置参数用于设置 web 应用程序的静态资源(图片、js、css和html等)的存放目录(详见4.2节),
默认值为 classpath:/static 、classpath:/public 、classpath:/resources 和 classpath:/META-INF/resources

4.2、静态资源目录的配置

spring boot 定义了静态资源的默认查找路径:
classpath:/static 、classpath:/public 、classpath:/resources 和 classpath:/META-INF/resources

只要将静态资源放在以上的任何一个目录中(习惯会把静态资源放在 classpath:/static 目录下),都能被访问到。

4.2.1、static静态目录示例

image

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

<h1>static.html</h1>

</body>
</html>

image

注意:外部访问静态资源时,不需要写默认(或自定义)的静态资源目录(本例为 static )

4.2.2、public静态目录示例

image

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

<h1>public.html</h1>

</body>
</html>

image

注意:外部访问静态资源时,不需要写默认(或自定义)的静态资源目录(本例为 public )

4.2.3、resources静态目录示例

image

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

<h1>resources.html</h1>

</body>
</html>

image

注意:外部访问静态资源时,不需要写默认(或自定义)的静态资源目录(本例为 resources )

4.2.4、META-INF/resources静态目录示例

image

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

<h1>META-INF_resources.html</h1>

</body>
</html>

image

注意:外部访问静态资源时,不需要写默认(或自定义)的静态资源目录(本例为 META-INF/resources )

4.2.5、自定义的静态目录示例

image

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

<h1>aaa.html</h1>

</body>
</html>

image

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

<h1>bbb.html</h1>

</body>
</html>

image

注意:当设置了自定义的静态资源目录之后,默认的静态目录 classpath:/static 、classpath:/public 、classpath:/resources 失效,
但默认的静态目录 classpath:/META-INF/resources 依然有效。

# 设置自定义的静态资源目录(本例为 aaa 和 bbb 目录)
spring.web.resources.static-locations=classpath:/aaa,classpath:/bbb

image

注意:外部访问静态资源时,不需要写自定义的静态资源目录(本例为 aaa )

image

注意:外部访问静态资源时,不需要写自定义的静态资源目录(本例为 bbb )

4.3、自定义拦截器的配置

4.3.1、创建拦截器

image

package online.liaojy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liaojy
 * @date 2023/12/20 - 6:48
 */
public class TestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("TestInterceptor --> preHandle()");
        return true;
    }

}

4.3.2、创建SpringMVC配置类

image

SpringMVC配置类的更多内容,请参考SpringMVC教程的14.4节

package online.liaojy.config;

import online.liaojy.interceptor.TestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author liaojy
 * @date 2023/12/20 - 7:04
 */
// 配置类只要放在启动类所在的包或者子包即可生效
@Configuration
public class SpringMVCConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        TestInterceptor testInterceptor = new TestInterceptor();
        registry.addInterceptor(testInterceptor).addPathPatterns("/**");
    }

}

4.3.3、测试效果

image

    @RequestMapping("/testInterceptor")
    public String testInterceptor(){
        System.out.println("TestController --> testInterceptor()");
        return "testInterceptor()";
    }

image