springbooot2 thymeleaf 配置以及加载资源文件解决方案

9 篇文章 1 订阅

最近在学习springbooot2 和 thymeleaf,程序运行是提示如下错误:

Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

程序文件

application.properties文件配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#thymeleaf

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

spring.thymeleaf.cache=false

spring.thymeleaf.servlet.content-type=text/html

spring.thymeleaf.enabled=true

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.mode=HTML

spring.thymeleaf.check-template-location=true

# 静态文件请求匹配方式

spring.mvc.static-path-pattern=/**

# 修改默认的静态寻址资源目录

spring.resources.static-locations = classpath:/templates/,classpath:/resources/,classpath:/static/,classpath:/public/

#热部署生效

spring.devtools.restart.enabled=true

 在编译的时候,发现一直报这个错误: 

1

Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

之后再网上找了各种答案,发现都不能使用

-------------------------------------------------------------------------------------------------------------------

下面是正确的方案:

在pom.xml中引入如下配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

        <resources>

            <resource>

                <!--加载资源目录-->

                <directory>src/main/resources</directory>

                <includes>

                    <!--加载配置文件-->

                    <include>**/*.xml</include>

                    <include>**/*.properties</include>

                    <!--加载模板文件-->

                    <include>**/*.html</include>

                    <!--加载静态文件-->

                    <include>/static/</include>

                </includes>

            </resource>

        </resources>

    </build>

在pom.xml中引入的文件,这样application.properties文件中可注释文件路径配置。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

server.port=9099

#thymeleaf

#spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

spring.thymeleaf.cache=false

spring.thymeleaf.servlet.content-type=text/html

spring.thymeleaf.enabled=true

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.mode=HTML

spring.thymeleaf.check-template-location=true

# 静态文件请求匹配方式

#spring.mvc.static-path-pattern=/**

# 修改默认的静态寻址资源目录

#spring.resources.static-locations = classpath:/templates/,classpath:/resources/,classpath:/static/,classpath:/public/

#热部署生效

spring.devtools.restart.enabled=true

LoginController中:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package com.java.seckill.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping("/login")

public class LoginController {

    @RequestMapping("/index")

    public String Index() {

        return "login";

    }

    @RequestMapping("/login")

    @ResponseBody

    public Boolean login(){

        return true;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值