springbooot2 thymeleaf 配置以及加载资源文件。Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

最近在学习springbooot2 和 thymeleaf

程序文件

application.properties文件配置:

#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

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

 

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

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

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

下面是正确的方案:

在pom.xml中引入如下配置

<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文件中可注释文件路径配置。

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中:

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;
    }
}

  

直接访问:http://localhost:9099/login/index

原文地址:https://www.cnblogs.com/wuxiang12580/p/10337198.html

时间: 2024-07-30 08:08:48

springbooot2 thymeleaf 配置以及加载资源文件。Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)的相关文章

Spring boot 国际化自动加载资源文件问题

Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义正确的资源文件路径,Spring boot就启动时就会自动加载资源. spring.messages.basename=i18n/message 但是我的项目修改后获取消息时系统报错,找不到对应语言的资源配置.于是试图找到原因.Google好久都没找到,简直好像就我一个人遇到这鬼问题一样??.只好自

通过类加载器加载资源文件

/*******************************************第一种方法***************************************************************/ public class Demo {    //资源文件可以通过类加载器的方式加载到内存中,这种方式的好处是程序不用明确制定配置文件的具体所在目录.程序可以自动的在    //src目录下搜索该文件,并加载    //采用下面这种方法还有一种弊端,就是,通过类加载器加载

java加载资源文件

className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件config.properties 那么,应该有如下代码: //前面没有"/"代表当前类的目录 InputStream is1 = Test.class.getResourceAsStream("config.properties"); System.out.printl

动态加载资源文件(ResourceDictionary)

原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<Application.Resources>里添加相应的样式内容,当然也可以在控件所在的控件的资源(如:<UserControl.Resources>)中添加相应样式内容 2.通过后台代码向当前程序的资源中动态添加,代码如下:(TextBlockStyle.xaml是一个ResourceDicti

PyQt5(5)——加载资源文件

在实际中我们需要美化界面,就需要许多的自定义图片. 但是我们发现直接导入图像使用,等程序运行时会报错.???? 这就需要建立资源文件并且加载它们,程序就可以顺利运行了. 设计界面是如何加载资源文件呢? 不能直接加载,需要在开发目录下编写.qrc文件.新建qrc文件,内容为下: <rc version="1.0"> <qresource> </qresource></rcc> 然后回到qtdesigner 界面: 点击小铅笔的图标,选择刚才

NHibernate动态加载资源文件

最近做项目,又用到了以前做过的ORM框架--NHibernate. 此次想要实现的目标: 1.简单SQL用NHibernate的Session的CRUD方法实现 2.复杂SQL用Native SQL实现 3.数据库可能有多个,且有可能是不同的数据库类型,如A系统用Oracle,B系统用MSSQL (第一点很简单,可以参照其他博客:本节主要记录第二点第三点,关于如何配置多数据库下次记录) 复杂SQL的实现,且针对不同数据库,那么需要加载不同的资源文件. 关于资源文件,请参考http://blog.

Java加载资源文件几种方法

from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMain { public static void main

J2EE加载资源文件以及Spring加载XML文件

J2EE加载XML文件 Resource接口,是用来加载文件的接口. FileSystemResource和ClassPathResource都是实现了Resource接口,他们都可以用来加载XML文件. 具体代码如下: 1 Resource resource1 = new ClassPathResource("文件.xml"); 2 3 Resource resource2 = new FileSystemResource("盘符:/项目路径/src/文件.xml"

Spring加载资源文件的方式

UrlResource 封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件.HTTP对象.FTP对象等.所有的URL都有个标准的 String表示,这些标准前缀可以标识不同的URL类型,包括file:访问文件系统路径,http: 通过HTTP协议访问的资源,ftp: 通过FTP访问的资源等等. FileSystemResource封装了java.io.File的操作,用来访问File类型的对象. ServletContextResource实现了Servle