当前位置: 首页 > 知识库问答 >
问题:

如何从Spring启动定位Thymeleaf模板

南宫凯康
2023-03-14

我正在努力学习这个教程http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html了解如何向Thymeleaf模板发送响应。但我得到了这个错误:找不到模板位置:classpath:/templates/(请添加一些模板或检查您的Thymeleaf配置)

我把信息。html文件位于其他源目录中,位于src/main/resources下

所以结构看起来像:

SomeProject——其他来源——src/main/resources——

我想知道为什么它显示在

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>            
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

在控制器里我有

@RequestMapping(value = "message", method = RequestMethod.GET)
    public String messages(Model model) {
    model.addAttribute("messages", messageRepository.findAll());
    return "message";
}

在视图中:

<ul th:each="message : ${messages}">
    <li th:text="${message.id}">1</li>
    <li><a href="#" th:text="${message.title}">Title ...</a></li>
    <li th:text="${message.text}">Text ...</li>
</ul>   

共有3个答案

蓬弘
2023-03-14

这里的2件事: 1.如果您使用Maven,我假设没有对文件夹名称的自定义。然后文件夹名称应该是src而不是源。2.一旦文件夹被重命名,将您的模板移动到src/资源中的“模板”文件夹中,这应该运行良好。

於鸿羲
2023-03-14

thymeleaf模板的默认目录位置为:

src/main/resources/templates

其他路径是Spring Boot的标准约定。

松昱
2023-03-14

Spring Boot包括对thymeleaf模板引擎的自动配置支持,您的模板将自动从src/main/resources/templates中获取。

如果要自定义模板位置,请使用Spring Boot中提供的以下thymeleaf属性配置

 spring.thymeleaf.check-template=true # Check that the template exists before rendering it.

 spring.thymeleaf.check-template-location=true # Check that the templates location exists.

 spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.

 spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.

 spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
 类似资料:
  • 我有一个springBoot应用程序,它使用Thymeleaf渲染模板。当我从Eclipse运行应用程序时,所有这些都可以完美地工作。但是,当我尝试构建并运行jar时,有一个模板拒绝加载,出现以下异常: 异常处理模板“/XXX/form/importConfiguration”:错误解析模板“/XXX/form/importConfiguration”,模板可能不存在或无法由任何已配置的模板解析程

  • 我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包 我想将所有应用程序错误重定向到一个公共页面。我已经创建了这个控制器: 我的配置文件: 以及错误模板 但结果是: 这就是提出的解决方案的结果: 检查属性,这些是属性,消息就在那里:

  • 在我的Spring boot应用程序中有以下项目结构,我想在其中使用Thymeleaf 但是Spring引导找不到我的模板目录,显示警告 找不到模板位置:classpath:/templates/(请添加一些模板或检查您的Thymeleaf配置) PS:我正在使用 在我的控制器代码中,我正在执行以下操作: 和文件只打印hello world。 因此,通常它应该查看(我想是同一个Gradle模块),

  • 本文向大家介绍Spring Boot 启动端口如何启动,包括了Spring Boot 启动端口如何启动的使用技巧和注意事项,需要的朋友参考一下 Spring Boot 启动端口 Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动。其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat)。当然你也可以将项目打包成war包,放到

  • 我正在用Spring boot编写一个程序,我的数据库是PostGRE。我对百里香有意见。我尝试输入一个localdate值,但该值仍然为空。我对字符串和int等其他值没有任何问题。

  • 我有一个spring-mvc项目,配置为从WEB-INF/jsp加载模板,我使用Servlet 3.0属性从WEB-INF/lib/someplugins.jar加载一些mvc模板! /META-INF/resources/WEB-INF/jsp/a.jsp,我如何做到这一点? 我拥有的视图解析器的spring配置是