如何利用静态资源配置将/**映射到**.html
时间: 2024-06-21 17:02:00 浏览: 3
可以通过在Spring Boot的配置文件中添加以下内容实现将/**映射到.html:
```
spring.mvc.view.prefix: /
spring.mvc.view.suffix: .html
```
这样配置后,Spring Boot会自动将访问路径中以/结尾的请求路径和.html后缀结合起来寻找对应的HTML文件,并返回给前端页面展示。
相关问题
package com.de.debook.config; import com.de.debook.interceptor.AuthorityInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class ConfigRegistCanter implements WebMvcConfigurer { //注册拦截器 //addPathPatterns 用来设置拦截路径 //excludePathPatterns 用来设置白名单,也就是不需要触发这个拦截器的路径。 //addPathPatterns("/**") 表示拦截所有的请求 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new AuthorityInterceptor()) .excludePathPatterns("/user/login.html") .excludePathPatterns("/user/login") .excludePathPatterns("/user/login.out") .excludePathPatterns("/user/register.html") .excludePathPatterns("/user/register") .excludePathPatterns("/admin/register.html") .excludePathPatterns("/index.html") .excludePathPatterns("/js/**") .excludePathPatterns("/css/**") .excludePathPatterns("/images/**") .excludePathPatterns("/layui/**") .excludePathPatterns("/wangEditor/**"); } //这个方法是用来配置静态资源的,比如html,js,css等 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } }
这是一个 Spring Boot 配置类,用于注册拦截器和配置静态资源的映射路径。具体来说,代码中的 `AuthorityInterceptor` 是自定义的拦截器类,用来实现用户权限的控制。在 `addInterceptors` 方法中,通过 `registry.addInterceptor()` 方法添加拦截器,并使用 `excludePathPatterns` 方法设置白名单,即不需要被拦截的路径。在这个例子中,白名单包括登录、注册等页面、静态资源等。在 `addResourceHandlers` 方法中,可以配置静态资源的映射路径。
public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("spash.html"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**").addResourceLocations("file:D:\\bishe\\qiangjunstudypic/"); //静态资源释放 registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/","classpath:/static/**","classpath:/templates/"); }
这段代码是一个Spring MVC的配置类,实现了WebMvcConfigurer接口,用于配置视图控制器和静态资源处理器。
其中addViewControllers方法用于注册一个视图控制器,将访问根路径"/"的请求映射到名为"spash.html"的视图上。
addResourceHandlers方法用于注册一个静态资源处理器,将以"/upload"开头的请求映射到指定的本地文件目录下,将以"/"开头的请求映射到classpath下的静态资源文件目录下,这些静态资源可以是html、css、js、图片等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)