zoukankan      html  css  js  c++  java
  • 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";
        }
    }
    View Code

    浏览器访问:

    感谢技术圈的朋友们常来常往,希望我的每一个分享能帮助到大家,如果帮助到了您,请给我留下点点的评论或关注,我也想和您成为朋友,感谢大家一起交流一起进步
  • 相关阅读:
    什么是Spring的IOC和DI
    2021年ACP考试复习笔记-云服务器ECS知识点及练习
    个人站点搭建完成啦,欢迎前来参观~
    准备一台标准服务器(模版机)!
    正则表达式几个常用的特殊字符!
    Shell的几个重点工具
    Shell入门,关于语法的一些简单玩意儿
    Mysql for Linux的安装以及远程访问
    在学到MySQL索引时,涉及到了B树和B+树.聊一聊
    Linux的一些问题的处理(重要目录,/配置静态IP/主机名/hosts映射)
  • 原文地址:https://www.cnblogs.com/fengguozhong/p/12023499.html
Copyright © 2011-2022 走看看