template might not exist or might not be accessible by any of the configured Template Resolvers 完美解决

初学者在maven spring boot web项目中使用thymeleaf 模板,经常会遇到  “template might not exist or might not be accessible by any of the configured Template Resolvers”这个问题,让人很头疼。其实这个错误的描述很清楚:

第一、模板不存在 ,第二、模板无法被解析器解析

带着这两个问题来找答案:

首先确定在Maven的资源管理文件中  pom.xml确保引入 spring-boot-starter-thymeleaf这个jar包,如果配置中有,它会自动下载到本地库。

        <!-- 引入 thymeleaf 模板依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

接下来在application.properties 中添加如下配置:

spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=true
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.prefix=/resources/templates/
spring.thymeleaf.suffix=.html    #文件后缀为.html或.jsp都可以,取决于/resources/templates/下对应的文件

有了以上这两步就没问题了,如下是项目的目录结构

Spring 启动类及MVC的 控制器部分代码:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.bean.User;

@Controller
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        System.out.print("app init");
    }

    @RequestMapping("/hello")
    @ResponseBody
    String home() {
        System.out.print("hello");
        return "Hello ,spring boot!";
    }

    @RequestMapping("/")
    public String index() {
        System.out.print("index");
        return "index";
    }

    @RequestMapping("/userLogin")
    public String userLogin(Model model) {
        User user = new User("guozhong",30);
        model.addAttribute("user",user);
        return "userLogin";
    }
}

浏览器访问:

原文地址:https://www.cnblogs.com/fengguozhong/p/12023499.html

时间: 2024-08-29 22:07:13

template might not exist or might not be accessible by any of the configured Template Resolvers 完美解决的相关文章

Spring Boot使用thymeleaf模板时报异常:template might not exist or might not be accessible by any of the configured Template Resolvers

错误如下: template might not exist or might not be accessible by any of the configured Template Resolvers 解决方法: 1.确定模板是否在默认templates文件夹里面,并且路径要和返回的View名字一致. 2.new ModelAndView("/log/loglist");这样写是不对的,应该把开头的斜杠去掉,改成:new ModelAndView("log/loglist&

异常:Error resolving template &quot;xxx&quot;, template might not exist or might not be accessible...解决办法

简单表述:控制台出现了这个异常:Error resolving template "xxx", template might not exist or might not be accessible by any of the configured Template Resolvers 解决办法:在报出这个异常的方法上添加注解@ResponseBody 讲一下个人理解:这个注解就是用来区别 方法的返回值字符串 和 视图解析器解析的页面名字字符串 的冲突的,举个例子:方法A返回的字符串s

thymeleaf在开发环境正常,但用jar运行时报错 Error resolving template template might not exist or might not be accessible

通过打包 jar直接运行的 百度搜索此关键词找到的:“springboot jar error resolving template” 原因是指向模板的路径前有斜杆/,就会出错,去掉就正常了(路径前不能带斜杆,类似相对路径的写法) 网上有人说增加配置spring.thymeleaf.prefix=classpath:/templates也能解决 原文地址:https://www.cnblogs.com/kinome/p/11509248.html

SpringBoot+Thyemleaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible

网上查看了各种解决的思路,总结如下: 1. 在controller层请求处理完了返回时,没有使用@RestController或@ResponseBody而返回了非json格式 这种情况下返回的数据thymeleaf模板无法解析,直接报错,本人正式因为这个原因才报错. 解决方案:可以将@Controller换成@RestController,不过需要注意有没有其他的方法返回了html页面,会导致返回的不是页面而是字符串:最好的方法就是在你所请求的方法上面加一个@ResponseBody即可. 2

SpringBoot+Thyemleaf报错Template might not exist or might not be accessible

第一次搭建SpringBoot+Thyemleaf项目,就遇见这个错误,所以记录一下 出现这个错误大致从以下几个方面排除 1.application.yml的配置问题 2.pom.xml导入,需导入以下依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </depe

Error resolving template [xxx], template might not exist or might not be exist

Springboot+thymeleaf+mybatis 抛Error resolving template [xxx], template might not exist的异常 原因是我们在pom.xml增加的以下内容导致的,以下内容中src/main/resources这项中没有把你的html文件包括进去,懒一点的做法(<include>**/*.*</include>), <resources> <resource> <targetPath>

Thymeleaf 异常:Exception processing template &quot;index&quot;: An error happened during template parsing (template: &quot;class path resource [templates/index.html]&quot;)

Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE 不同,其他基本相同. 运行 IDEA 中的项目,然后访问,出现异常: Exception processing template "index": An error happened during template parsing (template: "class path

Java中@Controller注解和@ResponseBody注解,报错找不到template

报错信息:org.thymeleaf.exceptions.TemplateInputException: Error resolving template "xxxxx", template might not exist or might not be accessible by any of the configured Template Resolvers 运行提示找不到模板时, 在controller类上加注解@Controller 和@RestController都可以在前

spring boot + thymeleaf 报错 org.thymeleaf.exceptions.TemplateInputException

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "admin/verifyPassword", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.TemplateRepository.getTemplate(Tem