关于SpringBoot下关于访问templates跟static目录问题

 


一、前言

  springboot整合了springmvc的拦截功能。拦截了所有的请求。默认放行的资源是:resources/static/ 目录下所有静态资源。(不走controller控制器就能直接访问到资源)。

html页面如果放在resources/templates目录下,则需要走controller控制器,controller放行,允许该资源访问,该资源才能被访问到。否则就会报404错误(它不可以直接被访问)。

有时候我们只需要简单在templates下两个页面进行跳转,再写controller免得有些多余,那怎么解决呢?


 

我们创建一个SpringBoot项目的时候默认会是这样的目录结构:

但是我在今天测试的时候(templates/index.html),发现并不能访问到它的同级目录:

我就不复原案发现场了,直接来说问题以及解决办法:

二、问题:

1、index.html页面中无法跳转/访问同级目录下/templates/xxx.html文件

2、index.html页面中无法访问到static目录下的xxx.xx静态文件

3、index.html页面中无法通过a标签访问到controller

三、解决

第一种:通过写配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class MyConfig implements WebMvcConfigurer {
 
     @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //这里是指在url上面打的内容
        registry.addResourceHandler("/**")
                //下面的是指可以对应resources文件下那些内容
                .addResourceLocations("classpath:/")
                .addResourceLocations("classpath:/templates/")
                .addResourceLocations("classpath:/static");
    }
}

第二种:通过添加配置文件属性(推荐)

在application.properties中添加属性:

1
2
3
4
# 静态文件请求匹配方式
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源目录
spring.resources.static-locations=classpath:/templates/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

四、测试:

pom.xml:

复制代码
<dependencies>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
View Code
复制代码

controller代码:

复制代码
package com.zhixi.controller;

/**
 * @author zhangzhixi
 * @version 1.0
 * @date 2021-7-11 14:35
 */

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Component
@Controller
public class MyController {

    @RequestMapping(value = "/some", method = RequestMethod.POST)
    @ResponseBody
    public String test1(@RequestParam("name") String name, @RequestParam("age") String age) {
        return "姓名是:" + name + " 年龄是:" + age;
    }

    @RequestMapping("/zzx")
    @ResponseBody
    public String test2(){
        return "你好,这是controller测试页面";
    }
}
View Code
复制代码

index.html代码:

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

<a href="/zzx">跳转到controller</a><br>
<a href="Test.html">跳转到templates下的Test.html页面</a><br>
<a href="css/test.css">跳转到static目录下的css文件</a>
<br>

<br>
<form action="/some" method="post">
    姓名:
    <label>
        <input type="text" name="name"/>
    </label> <br>
    年龄:
    <label>
        <input type="text" name="age"/>
    </label> <br>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
复制代码

成功访问:

posted @   Java小白的搬砖路  阅读(3456)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示