关于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

三、解决

第一种:通过写配置类

@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中添加属性:

# 静态文件请求匹配方式
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 @ 2021-07-11 18:18  Java小白的搬砖路  阅读(3002)  评论(0编辑  收藏  举报