zoukankan      html  css  js  c++  java
  • SpringBoot 返回thymeleaf页面报错

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers

    我的代码:

    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/demo")
    public class IndexController {
    
        @GetMapping(value = "index")
        public String showIndex(Model model) {
            model.addAttribute("h1_text", "this is a index page!");
            return "index";
        }
    }

    解决办法,返回路径前加上 / :

    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/demo")
    public class IndexController {
    
        @GetMapping(value = "index")
        public String showIndex(Model model) {
            model.addAttribute("h1_text", "this is a index page!");
            return "/index";
        }
    }

    遇到类似问题可以加上或去掉 / 试下。

    其他注意问题:

    1、在入口Application不要忘记加入你的包 

    @SpringBootApplication(scanBasePackages = {"com.example.controller"})

    2、@Controller 、@RestController、@ResponseBody 三者使用注意你是返回页面、字符串还是JSON数据

    3、使用thymeleaf作为模板文件,要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错,将其改为非严格的即可:LEGACYHTML5

    application.properties

    #是否开启缓存,开发时可设置为false,默认为true
    spring.thymeleaf.cache=false
    #检查模板是否存在,默认为true
    spring.thymeleaf.check-template=true
    #检查模板位置是否存在,默认为true
    spring.thymeleaf.check-template-location=true
    #模板文件编码,UTF-8
    spring.thymeleaf.encoding=UTF-8
    #模板文件位置
    spring.thymeleaf.prefix=classpath:/templates
    #Content-Type配置
    spring.thymeleaf.servlet.content-type=text/html
    #模板文件后缀
    spring.thymeleaf.suffix=.html
    #启用MVC Thymeleaf视图分辨率
    spring.thymeleaf.enabled=true
    #模板编码
    spring.thymeleaf.mode=LEGACYHTML5
    #应该中解决方案中排除的视图名称的逗号分隔列表
    spring.thymeleaf.excluded-view-names=

    application.yml

    spring.thymeleaf.content-type: text/html 
    spring.thymeleaf.cache: false 
    spring.thymeleaf.mode: LEGACYHTML5

    pom.xml中加入

    1 <dependency>
    2             <groupId>net.sourceforge.nekohtml</groupId>
    3             <artifactId>nekohtml</artifactId>
    4             <version>1.9.22</version>
    5         </dependency>
  • 相关阅读:
    管理页面的类 PageHelper
    接下来打算写一下visual stuido 2013使用git进行远端管理。
    转一下网上找来的tortoise git不用每次都输入邮箱和密码的方法。备查看
    tortoise git使用 git版本库的rsa key来进行ssh连接
    2015年4月1日 14:36:56 EF 主从表更新
    Oop分析方法
    Knative 实战:基于 Knative Serverless 技术实现天气服务-下篇
    超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
    从零开始入门 K8s | 可观测性:你的应用健康吗?
    Knative 暂时不会捐给任何基金会 | 云原生生态周报 Vol. 22
  • 原文地址:https://www.cnblogs.com/mingforyou/p/14610103.html
Copyright © 2011-2022 走看看