Springboot对静态资源的映射(WebMvcConfigurerAdapter 方法过时)

3 篇文章 0 订阅
2 篇文章 0 订阅

1.对webjars的映射

  • 什么是webjars?

webjars:通俗点就是用jar包的方式引入静态资源

比如我们要引入jquery的jar包,那我们只需要在springBoot的pom.xml中加上依赖即可


<dependency>
	<groupId>org.webjars</groupId>
	<artifactId>jquery</artifactId>
	<version>3.3.1</version>
</dependency>

在这里插入图片描述
到时如果我们访问的路径中是localhost:8080/webjars/xxx/xx的形式,那么就会访问到该jar包下的资源

2.对 /** 形式的访问

对访问路径中是localhost:8080/**的形式的访问,那么会去当前项目下的静态资源文件夹中寻找;
`所谓静态资源文件夹就是类路径下的文件夹;

springboot为我们提供了一下几种默认的文件夹;
当我们访问html/css/js/image等静态资源文件时都会默认到这以下文件夹中寻找
`

“classpath:/META‐INF/resources/”,
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”
“/”:当前项目的根路径

其中classpath是类路径
在这里插入图片描述
比如这里的java和resources就是类路径

3.对欢迎页的访问

所谓欢迎页就是我们所说的主页(以index.html)结尾的
如果我们访问形式是localhost:8080/

那么会默认访问静态资源文件夹下的index.html页面

4.模板引擎Template

在这里插入图片描述
这是springboot推荐的thymeleaf模板\引擎,它比jsp更加方便,更加强大,语法更简单;

模板引擎:就是通过一些表达式将静态页面的静态数据(html中的数据)转为动态数据;(从服务器中获取)
使用时我们只需在pom.xml中引入依赖即可

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

在这里插入图片描述
这里的templates文件夹就是springboot为我们提供的模板引擎文件夹;

比如我们在controller中写了一个方法

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String test(Map<String ,Object> map){
        map.put("hello","<h1>你好</h1>");
        map.put("users", Arrays.asList("张三","李四","王五"));
        return "success";
    }

}

返回值为"success",那么此时模板引擎会自动为我们拼串,默认前缀是template,也就是返回的字符串会到template文件夹中去寻找资源

那这里就有个问题了:

如果我们在静态资源文件夹中有一个index.html
在template文件夹中也有一个index.html
那此时我在浏览器输入localhost://8080/会访问哪个页面呢?

没错,会访问静态资源文件夹下的index.html
那如果我们想要访templates中index.html,那怎么办呢?
我们可以如下:

1.写一个方法映射请求

写一个方法,拦截 localhost://8080/请求和index.html请求,并返回index;
在这里插入图片描述
上面是一种解决办法,但是我们只是为了实现页面跳转,单独写一个方法有点麻烦,因此我们采用下面的方式,用viewController视图映射;

2.实现接口

首先我们要自定义一个配置类加上@configuration注解
同时实现webMvcConfigurer接口

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
    }
}

3.SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法

直接实现WebMvcConfigurer接口即可

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
    }
}

5.对图标的映射

有时我们会看到官网上的那些图标,比如
在这里插入图片描述
在这里插入图片描述
那这些我们也是可以设置的,我们只需要把我们需要的图标图片拖到静态资源文件夹中即可;springboot会自动为我们设置网站图标;
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值